refactor(test/mock): Ts'ify angular2/test/mock
Translates the last test directory -- angular2/test/mock -- to TypeScript.
This commit is contained in:
parent
c7572ac1f9
commit
2b714df64e
@ -8,30 +8,32 @@ import {
|
|||||||
it,
|
it,
|
||||||
} from 'angular2/test_lib';
|
} from 'angular2/test_lib';
|
||||||
|
|
||||||
|
import {stringify} from 'angular2/src/facade/lang';
|
||||||
|
|
||||||
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
|
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
|
||||||
|
|
||||||
import {Component} from 'angular2/src/core/annotations_impl/annotations';
|
import {Component, View} from 'angular2/angular2';
|
||||||
import {View} from 'angular2/src/core/annotations_impl/view';
|
|
||||||
|
|
||||||
import {isBlank} from 'angular2/src/facade/lang';
|
import {isBlank} from 'angular2/src/facade/lang';
|
||||||
|
|
||||||
|
import * as viewImpl from 'angular2/src/core/annotations_impl/view';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('MockTemplateResolver', () => {
|
describe('MockTemplateResolver', () => {
|
||||||
var resolver;
|
var resolver;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => { resolver = new MockTemplateResolver(); });
|
||||||
resolver = new MockTemplateResolver();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('View overriding', () => {
|
describe('View overriding', () => {
|
||||||
it('should fallback to the default TemplateResolver when templates are not overridden', () => {
|
it('should fallback to the default TemplateResolver when templates are not overridden',
|
||||||
|
() => {
|
||||||
var template = resolver.resolve(SomeComponent);
|
var template = resolver.resolve(SomeComponent);
|
||||||
expect(template.template).toEqual('template');
|
expect(template.template).toEqual('template');
|
||||||
expect(template.directives).toEqual([SomeDirective]);
|
expect(template.directives).toEqual([SomeDirective]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow overriding the @View', () => {
|
it('should allow overriding the @View', () => {
|
||||||
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
|
||||||
var template = resolver.resolve(SomeComponent);
|
var template = resolver.resolve(SomeComponent);
|
||||||
expect(template.template).toEqual('overridden template');
|
expect(template.template).toEqual('overridden template');
|
||||||
expect(isBlank(template.directives)).toBe(true);
|
expect(isBlank(template.directives)).toBe(true);
|
||||||
@ -40,9 +42,13 @@ export function main() {
|
|||||||
|
|
||||||
it('should not allow overriding a template after it has been resolved', () => {
|
it('should not allow overriding a template after it has been resolved', () => {
|
||||||
resolver.resolve(SomeComponent);
|
resolver.resolve(SomeComponent);
|
||||||
expect(() => {
|
expect(() =>
|
||||||
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
{
|
||||||
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
resolver.setView(SomeComponent,
|
||||||
|
new viewImpl.View({template: 'overridden template'}));
|
||||||
|
})
|
||||||
|
.toThrowError(
|
||||||
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -55,7 +61,7 @@ export function main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should allow overriding an overriden @View', () => {
|
it('should allow overriding an overriden @View', () => {
|
||||||
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
|
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
|
||||||
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
|
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
|
||||||
var template = resolver.resolve(SomeComponent);
|
var template = resolver.resolve(SomeComponent);
|
||||||
expect(template.template).toEqual('overridden template x 2');
|
expect(template.template).toEqual('overridden template x 2');
|
||||||
@ -63,9 +69,9 @@ export function main() {
|
|||||||
|
|
||||||
it('should not allow overriding a template after it has been resolved', () => {
|
it('should not allow overriding a template after it has been resolved', () => {
|
||||||
resolver.resolve(SomeComponent);
|
resolver.resolve(SomeComponent);
|
||||||
expect(() => {
|
expect(() => { resolver.setInlineTemplate(SomeComponent, 'overridden template'); })
|
||||||
resolver.setInlineTemplate(SomeComponent, 'overridden template');
|
.toThrowError(
|
||||||
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -79,7 +85,7 @@ export function main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should allow overriding a directive from an overriden @View', () => {
|
it('should allow overriding a directive from an overriden @View', () => {
|
||||||
resolver.setView(SomeComponent, new View({directives: [SomeOtherDirective]}));
|
resolver.setView(SomeComponent, new viewImpl.View({directives: [SomeOtherDirective]}));
|
||||||
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
|
||||||
var template = resolver.resolve(SomeComponent);
|
var template = resolver.resolve(SomeComponent);
|
||||||
expect(template.directives.length).toEqual(1);
|
expect(template.directives.length).toEqual(1);
|
||||||
@ -89,19 +95,24 @@ export function main() {
|
|||||||
it('should throw when the overridden directive is not present', () => {
|
it('should throw when the overridden directive is not present', () => {
|
||||||
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
|
||||||
expect(() => { resolver.resolve(SomeComponent); })
|
expect(() => { resolver.resolve(SomeComponent); })
|
||||||
.toThrowError('Overriden directive SomeOtherDirective not found in the template of SomeComponent');
|
.toThrowError(
|
||||||
|
`Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not allow overriding a directive after its template has been resolved', () => {
|
it('should not allow overriding a directive after its template has been resolved', () => {
|
||||||
resolver.resolve(SomeComponent);
|
resolver.resolve(SomeComponent);
|
||||||
expect(() => {
|
expect(
|
||||||
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
|
() =>
|
||||||
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
|
{ resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective); })
|
||||||
|
.toThrowError(
|
||||||
|
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SomeDirective {}
|
||||||
|
|
||||||
@Component({selector: 'cmp'})
|
@Component({selector: 'cmp'})
|
||||||
@View({
|
@View({
|
||||||
template: 'template',
|
template: 'template',
|
||||||
@ -110,8 +121,4 @@ export function main() {
|
|||||||
class SomeComponent {
|
class SomeComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
class SomeDirective {
|
class SomeOtherDirective {}
|
||||||
}
|
|
||||||
|
|
||||||
class SomeOtherDirective {
|
|
||||||
}
|
|
@ -18,27 +18,27 @@ export function main() {
|
|||||||
describe('MockXHR', () => {
|
describe('MockXHR', () => {
|
||||||
var xhr;
|
var xhr;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => { xhr = new MockXHR(); });
|
||||||
xhr = new MockXHR();
|
|
||||||
});
|
|
||||||
|
|
||||||
function expectResponse(request: Promise, url: string, response: string, done = null) {
|
function expectResponse(request: Promise<string>, url: string, response: string, done = null) {
|
||||||
function onResponse(text: string) {
|
function onResponse(text: string): string {
|
||||||
if (response === null) {
|
if (response === null) {
|
||||||
throw `Unexpected response ${url} -> ${text}`;
|
throw `Unexpected response ${url} -> ${text}`;
|
||||||
} else {
|
} else {
|
||||||
expect(text).toEqual(response);
|
expect(text).toEqual(response);
|
||||||
if (isPresent(done)) done();
|
if (isPresent(done)) done();
|
||||||
}
|
}
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(error: string) {
|
function onError(error: string): string {
|
||||||
if (response !== null) {
|
if (response !== null) {
|
||||||
throw `Unexpected error ${url}`;
|
throw `Unexpected error ${url}`;
|
||||||
} else {
|
} else {
|
||||||
expect(error).toEqual(`Failed to load ${url}`);
|
expect(error).toEqual(`Failed to load ${url}`);
|
||||||
if (isPresent(done)) done();
|
if (isPresent(done)) done();
|
||||||
}
|
}
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
PromiseWrapper.then(request, onResponse, onError);
|
PromiseWrapper.then(request, onResponse, onError);
|
||||||
@ -82,9 +82,7 @@ export function main() {
|
|||||||
xhr.expect(url, response);
|
xhr.expect(url, response);
|
||||||
xhr.get(url);
|
xhr.get(url);
|
||||||
xhr.get(url);
|
xhr.get(url);
|
||||||
expect(() => {
|
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
|
||||||
xhr.flush();
|
|
||||||
}).toThrowError('Unexpected request /foo');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return expectations before definitions', inject([AsyncTestCompleter], (async) => {
|
it('should return expectations before definitions', inject([AsyncTestCompleter], (async) => {
|
||||||
@ -98,24 +96,17 @@ export function main() {
|
|||||||
|
|
||||||
it('should throw when there is no definitions or expectations', () => {
|
it('should throw when there is no definitions or expectations', () => {
|
||||||
xhr.get('/foo');
|
xhr.get('/foo');
|
||||||
expect(() => {
|
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
|
||||||
xhr.flush();
|
|
||||||
}).toThrowError('Unexpected request /foo');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when flush is called without any pending requests', () => {
|
it('should throw when flush is called without any pending requests',
|
||||||
expect(() => {
|
() => { expect(() => { xhr.flush(); }).toThrowError('No pending requests to flush'); });
|
||||||
xhr.flush();
|
|
||||||
}).toThrowError('No pending requests to flush');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw on unstatisfied expectations', () => {
|
it('should throw on unstatisfied expectations', () => {
|
||||||
xhr.expect('/foo', 'bar');
|
xhr.expect('/foo', 'bar');
|
||||||
xhr.when('/bar', 'foo');
|
xhr.when('/bar', 'foo');
|
||||||
xhr.get('/bar');
|
xhr.get('/bar');
|
||||||
expect(() => {
|
expect(() => { xhr.flush(); }).toThrowError('Unsatisfied requests: /foo');
|
||||||
xhr.flush();
|
|
||||||
}).toThrowError('Unsatisfied requests: /foo');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user