Skip to content

Commit 2b714df

Browse files
committed
refactor(test/mock): Ts'ify angular2/test/mock
Translates the last test directory -- angular2/test/mock -- to TypeScript.
1 parent c7572ac commit 2b714df

File tree

2 files changed

+77
-79
lines changed

2 files changed

+77
-79
lines changed

modules/angular2/test/mock/template_resolver_mock_spec.js renamed to modules/angular2/test/mock/template_resolver_mock_spec.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@ import {
88
it,
99
} from 'angular2/test_lib';
1010

11+
import {stringify} from 'angular2/src/facade/lang';
12+
1113
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
1214

13-
import {Component} from 'angular2/src/core/annotations_impl/annotations';
14-
import {View} from 'angular2/src/core/annotations_impl/view';
15+
import {Component, View} from 'angular2/angular2';
1516

1617
import {isBlank} from 'angular2/src/facade/lang';
1718

19+
import * as viewImpl from 'angular2/src/core/annotations_impl/view';
20+
1821
export function main() {
1922
describe('MockTemplateResolver', () => {
2023
var resolver;
2124

22-
beforeEach(() => {
23-
resolver = new MockTemplateResolver();
24-
});
25+
beforeEach(() => { resolver = new MockTemplateResolver(); });
2526

2627
describe('View overriding', () => {
27-
it('should fallback to the default TemplateResolver when templates are not overridden', () => {
28-
var template = resolver.resolve(SomeComponent);
29-
expect(template.template).toEqual('template');
30-
expect(template.directives).toEqual([SomeDirective]);
31-
});
28+
it('should fallback to the default TemplateResolver when templates are not overridden',
29+
() => {
30+
var template = resolver.resolve(SomeComponent);
31+
expect(template.template).toEqual('template');
32+
expect(template.directives).toEqual([SomeDirective]);
33+
});
3234

3335
it('should allow overriding the @View', () => {
34-
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
36+
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
3537
var template = resolver.resolve(SomeComponent);
3638
expect(template.template).toEqual('overridden template');
3739
expect(isBlank(template.directives)).toBe(true);
@@ -40,9 +42,13 @@ export function main() {
4042

4143
it('should not allow overriding a template after it has been resolved', () => {
4244
resolver.resolve(SomeComponent);
43-
expect(() => {
44-
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
45-
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
45+
expect(() =>
46+
{
47+
resolver.setView(SomeComponent,
48+
new viewImpl.View({template: 'overridden template'}));
49+
})
50+
.toThrowError(
51+
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
4652
});
4753
});
4854

@@ -55,17 +61,17 @@ export function main() {
5561
});
5662

5763
it('should allow overriding an overriden @View', () => {
58-
resolver.setView(SomeComponent, new View({template: 'overridden template'}));
64+
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
5965
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
6066
var template = resolver.resolve(SomeComponent);
6167
expect(template.template).toEqual('overridden template x 2');
6268
});
6369

6470
it('should not allow overriding a template after it has been resolved', () => {
6571
resolver.resolve(SomeComponent);
66-
expect(() => {
67-
resolver.setInlineTemplate(SomeComponent, 'overridden template');
68-
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
72+
expect(() => { resolver.setInlineTemplate(SomeComponent, 'overridden template'); })
73+
.toThrowError(
74+
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
6975
});
7076
});
7177

@@ -79,7 +85,7 @@ export function main() {
7985
});
8086

8187
it('should allow overriding a directive from an overriden @View', () => {
82-
resolver.setView(SomeComponent, new View({directives: [SomeOtherDirective]}));
88+
resolver.setView(SomeComponent, new viewImpl.View({directives: [SomeOtherDirective]}));
8389
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
8490
var template = resolver.resolve(SomeComponent);
8591
expect(template.directives.length).toEqual(1);
@@ -89,19 +95,24 @@ export function main() {
8995
it('should throw when the overridden directive is not present', () => {
9096
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
9197
expect(() => { resolver.resolve(SomeComponent); })
92-
.toThrowError('Overriden directive SomeOtherDirective not found in the template of SomeComponent');
98+
.toThrowError(
99+
`Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`);
93100
});
94101

95102
it('should not allow overriding a directive after its template has been resolved', () => {
96103
resolver.resolve(SomeComponent);
97-
expect(() => {
98-
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
99-
}).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed');
104+
expect(
105+
() =>
106+
{ resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective); })
107+
.toThrowError(
108+
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
100109
});
101110
});
102111
});
103112
}
104113

114+
class SomeDirective {}
115+
105116
@Component({selector: 'cmp'})
106117
@View({
107118
template: 'template',
@@ -110,8 +121,4 @@ export function main() {
110121
class SomeComponent {
111122
}
112123

113-
class SomeDirective {
114-
}
115-
116-
class SomeOtherDirective {
117-
}
124+
class SomeOtherDirective {}

modules/angular2/test/mock/xhr_mock_spec.js renamed to modules/angular2/test/mock/xhr_mock_spec.ts

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,95 @@ export function main() {
1818
describe('MockXHR', () => {
1919
var xhr;
2020

21-
beforeEach(() => {
22-
xhr = new MockXHR();
23-
});
21+
beforeEach(() => { xhr = new MockXHR(); });
2422

25-
function expectResponse(request: Promise, url: string, response: string, done = null) {
26-
function onResponse(text: string) {
23+
function expectResponse(request: Promise<string>, url: string, response: string, done = null) {
24+
function onResponse(text: string): string {
2725
if (response === null) {
2826
throw `Unexpected response ${url} -> ${text}`;
2927
} else {
3028
expect(text).toEqual(response);
3129
if (isPresent(done)) done();
3230
}
31+
return text;
3332
}
3433

35-
function onError(error: string) {
34+
function onError(error: string): string {
3635
if (response !== null) {
3736
throw `Unexpected error ${url}`;
3837
} else {
3938
expect(error).toEqual(`Failed to load ${url}`);
4039
if (isPresent(done)) done();
4140
}
41+
return error;
4242
}
4343

4444
PromiseWrapper.then(request, onResponse, onError);
4545
}
4646

4747
it('should return a response from the definitions', inject([AsyncTestCompleter], (async) => {
48-
var url = '/foo';
49-
var response = 'bar';
50-
xhr.when(url, response);
51-
expectResponse(xhr.get(url), url, response, () => async.done());
52-
xhr.flush();
53-
}));
48+
var url = '/foo';
49+
var response = 'bar';
50+
xhr.when(url, response);
51+
expectResponse(xhr.get(url), url, response, () => async.done());
52+
xhr.flush();
53+
}));
5454

5555
it('should return an error from the definitions', inject([AsyncTestCompleter], (async) => {
56-
var url = '/foo';
57-
var response = null;
58-
xhr.when(url, response);
59-
expectResponse(xhr.get(url), url, response, () => async.done());
60-
xhr.flush();
61-
}));
56+
var url = '/foo';
57+
var response = null;
58+
xhr.when(url, response);
59+
expectResponse(xhr.get(url), url, response, () => async.done());
60+
xhr.flush();
61+
}));
6262

6363
it('should return a response from the expectations', inject([AsyncTestCompleter], (async) => {
64-
var url = '/foo';
65-
var response = 'bar';
66-
xhr.expect(url, response);
67-
expectResponse(xhr.get(url), url, response, () => async.done());
68-
xhr.flush();
69-
}));
64+
var url = '/foo';
65+
var response = 'bar';
66+
xhr.expect(url, response);
67+
expectResponse(xhr.get(url), url, response, () => async.done());
68+
xhr.flush();
69+
}));
7070

7171
it('should return an error from the expectations', inject([AsyncTestCompleter], (async) => {
72-
var url = '/foo';
73-
var response = null;
74-
xhr.expect(url, response);
75-
expectResponse(xhr.get(url), url, response, () => async.done());
76-
xhr.flush();
77-
}));
72+
var url = '/foo';
73+
var response = null;
74+
xhr.expect(url, response);
75+
expectResponse(xhr.get(url), url, response, () => async.done());
76+
xhr.flush();
77+
}));
7878

7979
it('should not reuse expectations', () => {
8080
var url = '/foo';
8181
var response = 'bar';
8282
xhr.expect(url, response);
8383
xhr.get(url);
8484
xhr.get(url);
85-
expect(() => {
86-
xhr.flush();
87-
}).toThrowError('Unexpected request /foo');
85+
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
8886
});
8987

9088
it('should return expectations before definitions', inject([AsyncTestCompleter], (async) => {
91-
var url = '/foo';
92-
xhr.when(url, 'when');
93-
xhr.expect(url, 'expect');
94-
expectResponse(xhr.get(url), url, 'expect');
95-
expectResponse(xhr.get(url), url, 'when', () => async.done());
96-
xhr.flush();
97-
}));
89+
var url = '/foo';
90+
xhr.when(url, 'when');
91+
xhr.expect(url, 'expect');
92+
expectResponse(xhr.get(url), url, 'expect');
93+
expectResponse(xhr.get(url), url, 'when', () => async.done());
94+
xhr.flush();
95+
}));
9896

9997
it('should throw when there is no definitions or expectations', () => {
10098
xhr.get('/foo');
101-
expect(() => {
102-
xhr.flush();
103-
}).toThrowError('Unexpected request /foo');
99+
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
104100
});
105101

106-
it('should throw when flush is called without any pending requests', () => {
107-
expect(() => {
108-
xhr.flush();
109-
}).toThrowError('No pending requests to flush');
110-
});
102+
it('should throw when flush is called without any pending requests',
103+
() => { expect(() => { xhr.flush(); }).toThrowError('No pending requests to flush'); });
111104

112105
it('should throw on unstatisfied expectations', () => {
113106
xhr.expect('/foo', 'bar');
114107
xhr.when('/bar', 'foo');
115108
xhr.get('/bar');
116-
expect(() => {
117-
xhr.flush();
118-
}).toThrowError('Unsatisfied requests: /foo');
109+
expect(() => { xhr.flush(); }).toThrowError('Unsatisfied requests: /foo');
119110
});
120111
});
121112
}

0 commit comments

Comments
 (0)