refactor(test/mock): Ts'ify angular2/test/mock

Translates the last test directory -- angular2/test/mock -- to TypeScript.
This commit is contained in:
Ian Riley 2015-06-02 17:41:44 -07:00
parent c7572ac1f9
commit 2b714df64e
2 changed files with 77 additions and 79 deletions

View File

@ -8,30 +8,32 @@ import {
it,
} from 'angular2/test_lib';
import {stringify} from 'angular2/src/facade/lang';
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
import {Component} from 'angular2/src/core/annotations_impl/annotations';
import {View} from 'angular2/src/core/annotations_impl/view';
import {Component, View} from 'angular2/angular2';
import {isBlank} from 'angular2/src/facade/lang';
import * as viewImpl from 'angular2/src/core/annotations_impl/view';
export function main() {
describe('MockTemplateResolver', () => {
var resolver;
beforeEach(() => {
resolver = new MockTemplateResolver();
});
beforeEach(() => { resolver = new MockTemplateResolver(); });
describe('View overriding', () => {
it('should fallback to the default TemplateResolver when templates are not overridden', () => {
var template = resolver.resolve(SomeComponent);
expect(template.template).toEqual('template');
expect(template.directives).toEqual([SomeDirective]);
});
it('should fallback to the default TemplateResolver when templates are not overridden',
() => {
var template = resolver.resolve(SomeComponent);
expect(template.template).toEqual('template');
expect(template.directives).toEqual([SomeDirective]);
});
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);
expect(template.template).toEqual('overridden template');
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', () => {
resolver.resolve(SomeComponent);
expect(() => {
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
expect(() =>
{
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', () => {
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
var template = resolver.resolve(SomeComponent);
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', () => {
resolver.resolve(SomeComponent);
expect(() => {
resolver.setInlineTemplate(SomeComponent, 'overridden template');
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
expect(() => { resolver.setInlineTemplate(SomeComponent, 'overridden template'); })
.toThrowError(
`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', () => {
resolver.setView(SomeComponent, new View({directives: [SomeOtherDirective]}));
resolver.setView(SomeComponent, new viewImpl.View({directives: [SomeOtherDirective]}));
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
var template = resolver.resolve(SomeComponent);
expect(template.directives.length).toEqual(1);
@ -89,19 +95,24 @@ export function main() {
it('should throw when the overridden directive is not present', () => {
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
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', () => {
resolver.resolve(SomeComponent);
expect(() => {
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
expect(
() =>
{ 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'})
@View({
template: 'template',
@ -110,8 +121,4 @@ export function main() {
class SomeComponent {
}
class SomeDirective {
}
class SomeOtherDirective {
}
class SomeOtherDirective {}

View File

@ -18,63 +18,63 @@ export function main() {
describe('MockXHR', () => {
var xhr;
beforeEach(() => {
xhr = new MockXHR();
});
beforeEach(() => { xhr = new MockXHR(); });
function expectResponse(request: Promise, url: string, response: string, done = null) {
function onResponse(text: string) {
function expectResponse(request: Promise<string>, url: string, response: string, done = null) {
function onResponse(text: string): string {
if (response === null) {
throw `Unexpected response ${url} -> ${text}`;
} else {
expect(text).toEqual(response);
if (isPresent(done)) done();
}
return text;
}
function onError(error: string) {
function onError(error: string): string {
if (response !== null) {
throw `Unexpected error ${url}`;
} else {
expect(error).toEqual(`Failed to load ${url}`);
if (isPresent(done)) done();
}
return error;
}
PromiseWrapper.then(request, onResponse, onError);
}
it('should return a response from the definitions', inject([AsyncTestCompleter], (async) => {
var url = '/foo';
var response = 'bar';
xhr.when(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
var url = '/foo';
var response = 'bar';
xhr.when(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
it('should return an error from the definitions', inject([AsyncTestCompleter], (async) => {
var url = '/foo';
var response = null;
xhr.when(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
var url = '/foo';
var response = null;
xhr.when(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
it('should return a response from the expectations', inject([AsyncTestCompleter], (async) => {
var url = '/foo';
var response = 'bar';
xhr.expect(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
var url = '/foo';
var response = 'bar';
xhr.expect(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
it('should return an error from the expectations', inject([AsyncTestCompleter], (async) => {
var url = '/foo';
var response = null;
xhr.expect(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
var url = '/foo';
var response = null;
xhr.expect(url, response);
expectResponse(xhr.get(url), url, response, () => async.done());
xhr.flush();
}));
it('should not reuse expectations', () => {
var url = '/foo';
@ -82,40 +82,31 @@ export function main() {
xhr.expect(url, response);
xhr.get(url);
xhr.get(url);
expect(() => {
xhr.flush();
}).toThrowError('Unexpected request /foo');
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
});
it('should return expectations before definitions', inject([AsyncTestCompleter], (async) => {
var url = '/foo';
xhr.when(url, 'when');
xhr.expect(url, 'expect');
expectResponse(xhr.get(url), url, 'expect');
expectResponse(xhr.get(url), url, 'when', () => async.done());
xhr.flush();
}));
var url = '/foo';
xhr.when(url, 'when');
xhr.expect(url, 'expect');
expectResponse(xhr.get(url), url, 'expect');
expectResponse(xhr.get(url), url, 'when', () => async.done());
xhr.flush();
}));
it('should throw when there is no definitions or expectations', () => {
xhr.get('/foo');
expect(() => {
xhr.flush();
}).toThrowError('Unexpected request /foo');
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
});
it('should throw when flush is called without any pending requests', () => {
expect(() => {
xhr.flush();
}).toThrowError('No pending requests to flush');
});
it('should throw when flush is called without any pending requests',
() => { expect(() => { xhr.flush(); }).toThrowError('No pending requests to flush'); });
it('should throw on unstatisfied expectations', () => {
xhr.expect('/foo', 'bar');
xhr.when('/bar', 'foo');
xhr.get('/bar');
expect(() => {
xhr.flush();
}).toThrowError('Unsatisfied requests: /foo');
expect(() => { xhr.flush(); }).toThrowError('Unsatisfied requests: /foo');
});
});
}