Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/angular2/examples/testing/ts/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
beforeEach,
afterEach,
beforeEachProviders,
Done,
inject
} from 'angular2/testing';
import {provide} from 'angular2/core';
Expand Down Expand Up @@ -88,3 +89,16 @@ describe('some component', () => {
});
});
// #enddocregion

// #docregion Done
describe('some component', () => {
it('uses a periodic timer',
inject([MyService, Done],
(service: MyService, done: any) => {
// This test can call 'done' or 'done.fail' like a normal Jasmine test.
// service.startPeriodicTimer();
// service.onThirdIteration(done);
// service.onError(done.fail);
}));
});
// #enddocregion
21 changes: 18 additions & 3 deletions modules/angular2/src/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import {global} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection';
import {bind} from 'angular2/core';
import {provide} from 'angular2/core';

import {
FunctionWithParamTokens,
Expand Down Expand Up @@ -89,6 +89,15 @@ export type AsyncTestFn = (done: () => void) => void;
*/
export type AnyTestFn = SyncTestFn | AsyncTestFn;

/**
* Injectable completer that allows injection of the Jasmine 'done' function.
*
* ## Example:
*
* {@example testing/ts/testing.ts region='Done'}
*/
export class Done {}

var jsmBeforeEach = _global.beforeEach;
var jsmIt = _global.it;
var jsmIIt = _global.fit;
Expand Down Expand Up @@ -144,8 +153,14 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
if (testFnT.isAsync) {
runInAsyncTestZone(() => testInjector.execute(testFnT), done, done.fail, name);
} else {
testInjector.execute(testFnT);
done();
if (testFnT.hasToken(Done)) {
testInjector.addProviders([provide(Done, {useValue: done})]);

testInjector.execute(testFnT);
} else {
testInjector.execute(testFnT);
done();
}
}
}, timeOut);
} else {
Expand Down
10 changes: 9 additions & 1 deletion modules/angular2/test/testing/testing_public_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
async,
withProviders,
beforeEachProviders,
TestComponentBuilder
TestComponentBuilder,
Done
} from 'angular2/testing';

import {Injectable, bind} from 'angular2/core';
Expand Down Expand Up @@ -137,6 +138,13 @@ export function main() {
it('should use set up providers',
inject([FancyService], (service) => { expect(service.value).toEqual('real value'); }));

it('should allow manual use of the done fn', inject([FancyService, Done], (service, done) => {
service.getTimeoutValue().then((value) => {
expect(value).toEqual('timeout value');
done();
});
}));

it('should wait until returned promises', async(inject([FancyService], (service) => {
service.getAsyncValue().then((value) => { expect(value).toEqual('async value'); });
service.getTimeoutValue().then((value) => { expect(value).toEqual('timeout value'); });
Expand Down