feat(test): Implement fakeAsync using the FakeAsyncTestZoneSpec from zone.js.
Update the version of zone.js to @0.6.12 that contains the new FakeAsyncTestZoneSpec. The new fakeAsync zone handles errors better and clearPendingTimers() is no longer required to be called after handling an error and is deprecated. The fakeAsync test zone will now throw an error if an XHR is attemtped within the test since that cannot be controlled synchronously in the test(Need to be mocked out with a service implementation that doesn't involve XHRs). This commit also allows fakeAsync to wrap inject to make it consistent with async test zone. BREAKING CHANGE: inject can no longer wrap fakeAsync while fakeAsync can wrap inject. So the order in existing tests with inject and fakeAsync has to be switched as follows: Before: ``` inject([...], fakeAsync((...) => {...})) ``` After: ``` fakeAsync(inject([...], (...) => {...})) ``` Closes #8142
This commit is contained in:
parent
cc86fee1d1
commit
bab81a9831
|
@ -21,6 +21,7 @@ module.exports = function(config) {
|
|||
'node_modules/zone.js/dist/long-stack-trace-zone.js',
|
||||
'node_modules/zone.js/dist/jasmine-patch.js',
|
||||
'node_modules/zone.js/dist/async-test.js',
|
||||
'node_modules/zone.js/dist/fake-async-test.js',
|
||||
|
||||
// Including systemjs because it defines `__eval`, which produces correct stack traces.
|
||||
'modules/angular2/src/testing/shims_for_IE.js',
|
||||
|
|
|
@ -4,6 +4,8 @@ import 'dart:async' show runZoned, ZoneSpecification;
|
|||
import 'package:quiver/testing/async.dart' as quiver;
|
||||
import 'package:angular2/src/facade/exceptions.dart' show BaseException;
|
||||
|
||||
import 'test_injector.dart' show getTestInjector, FunctionWithParamTokens;
|
||||
|
||||
const _u = const Object();
|
||||
|
||||
quiver.FakeAsync _fakeAsync = null;
|
||||
|
@ -16,15 +18,29 @@ quiver.FakeAsync _fakeAsync = null;
|
|||
* If there are any pending timers at the end of the function, an exception
|
||||
* will be thrown.
|
||||
*
|
||||
* Can be used to wrap inject() calls.
|
||||
*
|
||||
* Returns a `Function` that wraps [fn].
|
||||
*/
|
||||
Function fakeAsync(Function fn) {
|
||||
Function fakeAsync(dynamic /* Function | FunctionWithParamTokens */ fn) {
|
||||
if (_fakeAsync != null) {
|
||||
throw 'fakeAsync() calls can not be nested';
|
||||
}
|
||||
|
||||
return (
|
||||
[a0 = _u,
|
||||
Function innerFn = null;
|
||||
if (fn is FunctionWithParamTokens) {
|
||||
if (fn.isAsync) {
|
||||
throw 'Cannot wrap async test with fakeAsync';
|
||||
}
|
||||
innerFn = () { getTestInjector().execute(fn); };
|
||||
} else if (fn is Function) {
|
||||
innerFn = fn;
|
||||
} else {
|
||||
throw 'fakeAsync can wrap only test functions but got object of type ' +
|
||||
fn.runtimeType.toString();
|
||||
}
|
||||
|
||||
return ([a0 = _u,
|
||||
a1 = _u,
|
||||
a2 = _u,
|
||||
a3 = _u,
|
||||
|
@ -42,7 +58,7 @@ Function fakeAsync(Function fn) {
|
|||
List args = [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
|
||||
.takeWhile((a) => a != _u)
|
||||
.toList();
|
||||
var res = Function.apply(fn, args);
|
||||
var res = Function.apply(innerFn, args);
|
||||
_fakeAsync.flushMicrotasks();
|
||||
|
||||
if (async.periodicTimerCount > 0) {
|
||||
|
|
|
@ -1,59 +1,7 @@
|
|||
import {global} from 'angular2/src/facade/lang';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {getTestInjector, FunctionWithParamTokens} from './test_injector';
|
||||
|
||||
var _scheduler;
|
||||
var _microtasks: Function[] = [];
|
||||
var _pendingPeriodicTimers: number[] = [];
|
||||
var _pendingTimers: number[] = [];
|
||||
|
||||
class FakeAsyncZoneSpec implements ZoneSpec {
|
||||
static assertInZone(): void {
|
||||
if (!Zone.current.get('inFakeAsyncZone')) {
|
||||
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
||||
}
|
||||
}
|
||||
|
||||
name: string = 'fakeAsync';
|
||||
|
||||
properties: {[key: string]: any} = {'inFakeAsyncZone': true};
|
||||
|
||||
onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task {
|
||||
switch (task.type) {
|
||||
case 'microTask':
|
||||
_microtasks.push(task.invoke);
|
||||
break;
|
||||
case 'macroTask':
|
||||
switch (task.source) {
|
||||
case 'setTimeout':
|
||||
task.data['handleId'] = _setTimeout(task.invoke, task.data['delay'], task.data['args']);
|
||||
break;
|
||||
case 'setInterval':
|
||||
task.data['handleId'] =
|
||||
_setInterval(task.invoke, task.data['delay'], task.data['args']);
|
||||
break;
|
||||
default:
|
||||
task = delegate.scheduleTask(target, task);
|
||||
}
|
||||
break;
|
||||
case 'eventTask':
|
||||
task = delegate.scheduleTask(target, task);
|
||||
break;
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
onCancelTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): any {
|
||||
switch (task.source) {
|
||||
case 'setTimeout':
|
||||
return _clearTimeout(task.data['handleId']);
|
||||
case 'setInterval':
|
||||
return _clearInterval(task.data['handleId']);
|
||||
default:
|
||||
return delegate.scheduleTask(target, task);
|
||||
}
|
||||
}
|
||||
}
|
||||
let _FakeAsyncTestZoneSpecType = Zone['FakeAsyncTestZoneSpec'];
|
||||
|
||||
/**
|
||||
* Wraps a function to be executed in the fakeAsync zone:
|
||||
|
@ -62,6 +10,8 @@ class FakeAsyncZoneSpec implements ZoneSpec {
|
|||
*
|
||||
* If there are any pending timers at the end of the function, an exception will be thrown.
|
||||
*
|
||||
* Can be used to wrap inject() calls.
|
||||
*
|
||||
* ## Example
|
||||
*
|
||||
* {@example testing/ts/fake_async.ts region='basic'}
|
||||
|
@ -69,57 +19,63 @@ class FakeAsyncZoneSpec implements ZoneSpec {
|
|||
* @param fn
|
||||
* @returns {Function} The function wrapped to be executed in the fakeAsync zone
|
||||
*/
|
||||
export function fakeAsync(fn: Function): Function {
|
||||
if (Zone.current.get('inFakeAsyncZone')) {
|
||||
throw new Error('fakeAsync() calls can not be nested');
|
||||
export function fakeAsync(fn: Function | FunctionWithParamTokens): Function {
|
||||
if (Zone.current.get('FakeAsyncTestZoneSpec') != null) {
|
||||
throw new BaseException('fakeAsync() calls can not be nested');
|
||||
}
|
||||
|
||||
var fakeAsyncZone = Zone.current.fork(new FakeAsyncZoneSpec());
|
||||
let fakeAsyncTestZoneSpec = new _FakeAsyncTestZoneSpecType();
|
||||
let fakeAsyncZone = Zone.current.fork(fakeAsyncTestZoneSpec);
|
||||
|
||||
let innerTestFn: Function = null;
|
||||
|
||||
if (fn instanceof FunctionWithParamTokens) {
|
||||
if (fn.isAsync) {
|
||||
throw new BaseException('Cannot wrap async test with fakeAsync');
|
||||
}
|
||||
innerTestFn = () => { getTestInjector().execute(fn as FunctionWithParamTokens); };
|
||||
} else {
|
||||
innerTestFn = fn;
|
||||
}
|
||||
|
||||
return function(...args) {
|
||||
// TODO(tbosch): This class should already be part of the jasmine typings but it is not...
|
||||
_scheduler = new (<any>jasmine).DelayedFunctionScheduler();
|
||||
clearPendingTimers();
|
||||
|
||||
let res = fakeAsyncZone.run(() => {
|
||||
let res = fn(...args);
|
||||
let res = innerTestFn(...args);
|
||||
flushMicrotasks();
|
||||
return res;
|
||||
});
|
||||
|
||||
if (_pendingPeriodicTimers.length > 0) {
|
||||
if (fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
||||
throw new BaseException(`${fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
|
||||
`periodic timer(s) still in the queue.`);
|
||||
}
|
||||
|
||||
if (fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
||||
throw new BaseException(
|
||||
`${_pendingPeriodicTimers.length} periodic timer(s) still in the queue.`);
|
||||
`${fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
|
||||
}
|
||||
|
||||
if (_pendingTimers.length > 0) {
|
||||
throw new BaseException(`${_pendingTimers.length} timer(s) still in the queue.`);
|
||||
}
|
||||
|
||||
_scheduler = null;
|
||||
ListWrapper.clear(_microtasks);
|
||||
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
function _getFakeAsyncZoneSpec(): any {
|
||||
let zoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
||||
if (zoneSpec == null) {
|
||||
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
||||
}
|
||||
return zoneSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the queue of pending timers and microtasks.
|
||||
* Tests no longer need to call this explicitly.
|
||||
*
|
||||
* Useful for cleaning up after an asynchronous test passes.
|
||||
*
|
||||
* ## Example
|
||||
*
|
||||
* {@example testing/ts/fake_async.ts region='pending'}
|
||||
* @deprecated
|
||||
*/
|
||||
export function clearPendingTimers(): void {
|
||||
// TODO we should fix tick to dequeue the failed timer instead of relying on clearPendingTimers
|
||||
ListWrapper.clear(_microtasks);
|
||||
ListWrapper.clear(_pendingPeriodicTimers);
|
||||
ListWrapper.clear(_pendingTimers);
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
|
||||
*
|
||||
|
@ -133,54 +89,12 @@ export function clearPendingTimers(): void {
|
|||
* @param {number} millis Number of millisecond, defaults to 0
|
||||
*/
|
||||
export function tick(millis: number = 0): void {
|
||||
FakeAsyncZoneSpec.assertInZone();
|
||||
flushMicrotasks();
|
||||
_scheduler.tick(millis);
|
||||
_getFakeAsyncZoneSpec().tick(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush any pending microtasks.
|
||||
*/
|
||||
export function flushMicrotasks(): void {
|
||||
FakeAsyncZoneSpec.assertInZone();
|
||||
while (_microtasks.length > 0) {
|
||||
var microtask = ListWrapper.removeAt(_microtasks, 0);
|
||||
microtask();
|
||||
}
|
||||
}
|
||||
|
||||
function _setTimeout(fn: Function, delay: number, args: any[]): number {
|
||||
var cb = _fnAndFlush(fn);
|
||||
var id = _scheduler.scheduleFunction(cb, delay, args);
|
||||
_pendingTimers.push(id);
|
||||
_scheduler.scheduleFunction(_dequeueTimer(id), delay);
|
||||
return id;
|
||||
}
|
||||
|
||||
function _clearTimeout(id: number) {
|
||||
_dequeueTimer(id);
|
||||
return _scheduler.removeFunctionWithId(id);
|
||||
}
|
||||
|
||||
function _setInterval(fn: Function, interval: number, ...args) {
|
||||
var cb = _fnAndFlush(fn);
|
||||
var id = _scheduler.scheduleFunction(cb, interval, args, true);
|
||||
_pendingPeriodicTimers.push(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
function _clearInterval(id: number) {
|
||||
ListWrapper.remove(_pendingPeriodicTimers, id);
|
||||
return _scheduler.removeFunctionWithId(id);
|
||||
}
|
||||
|
||||
function _fnAndFlush(fn: Function): Function {
|
||||
return (...args) => {
|
||||
fn.apply(global, args);
|
||||
flushMicrotasks();
|
||||
}
|
||||
}
|
||||
|
||||
function _dequeueTimer(id: number): Function {
|
||||
return function() { ListWrapper.remove(_pendingTimers, id); }
|
||||
_getFakeAsyncZoneSpec().flushMicrotasks();
|
||||
}
|
||||
|
|
|
@ -195,7 +195,7 @@ function emptyArray(): Array<any> {
|
|||
}
|
||||
|
||||
export class FunctionWithParamTokens {
|
||||
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean,
|
||||
constructor(private _tokens: any[], public fn: Function, public isAsync: boolean,
|
||||
public additionalProviders: () => any = emptyArray) {}
|
||||
|
||||
/**
|
||||
|
@ -203,7 +203,7 @@ export class FunctionWithParamTokens {
|
|||
*/
|
||||
execute(injector: ReflectiveInjector): any {
|
||||
var params = this._tokens.map(t => injector.get(t));
|
||||
return FunctionWrapper.apply(this._fn, params);
|
||||
return FunctionWrapper.apply(this.fn, params);
|
||||
}
|
||||
|
||||
hasToken(token: any): boolean { return this._tokens.indexOf(token) > -1; }
|
||||
|
|
|
@ -87,7 +87,7 @@ export type AsyncTestFn = (done: () => void) => void;
|
|||
/**
|
||||
* Signature for any simple testing function.
|
||||
*/
|
||||
export type AnyTestFn = SyncTestFn | AsyncTestFn;
|
||||
export type AnyTestFn = SyncTestFn | AsyncTestFn | Function;
|
||||
|
||||
var jsmBeforeEach = _global.beforeEach;
|
||||
var jsmIt = _global.it;
|
||||
|
|
|
@ -120,7 +120,7 @@ export function main() {
|
|||
}));
|
||||
|
||||
it("should emit ngSubmit event on submit",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<div>
|
||||
<form [ngFormModel]="form" (ngSubmit)="name='updated'"></form>
|
||||
<span>{{name}}</span>
|
||||
|
@ -128,8 +128,7 @@ export function main() {
|
|||
|
||||
var fixture: ComponentFixture;
|
||||
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
|
||||
fixture.debugElement.componentInstance.form = new ControlGroup({});
|
||||
|
@ -494,7 +493,7 @@ export function main() {
|
|||
}));
|
||||
|
||||
it("with a dynamic list of options",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<div [ngFormModel]="form">
|
||||
<select ngControl="city">
|
||||
<option *ngFor="#c of data" [value]="c"></option>
|
||||
|
@ -502,8 +501,8 @@ export function main() {
|
|||
</div>`;
|
||||
|
||||
var fixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(compFixture) => fixture = compFixture);
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((compFixture) => fixture =
|
||||
compFixture);
|
||||
tick();
|
||||
|
||||
fixture.debugElement.componentInstance.form =
|
||||
|
@ -783,7 +782,7 @@ export function main() {
|
|||
}));
|
||||
|
||||
it("should use async validators defined in the html",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var form = new ControlGroup({"login": new Control("")});
|
||||
|
||||
var t = `<div [ngFormModel]="form">
|
||||
|
@ -835,9 +834,8 @@ export function main() {
|
|||
}));
|
||||
|
||||
it("should use async validators defined in the model",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
var control =
|
||||
new Control("", Validators.required, uniqLoginAsyncValidator("expected"));
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var control = new Control("", Validators.required, uniqLoginAsyncValidator("expected"));
|
||||
var form = new ControlGroup({"login": control});
|
||||
|
||||
var t = `<div [ngFormModel]="form">
|
||||
|
@ -845,8 +843,7 @@ export function main() {
|
|||
</div>`;
|
||||
|
||||
var fixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => fixture =
|
||||
root);
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => fixture = root);
|
||||
tick();
|
||||
|
||||
fixture.debugElement.componentInstance.form = form;
|
||||
|
@ -919,16 +916,14 @@ export function main() {
|
|||
});
|
||||
|
||||
it("should support ngModel for complex forms",
|
||||
inject(
|
||||
[TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var form = new ControlGroup({"name": new Control("")});
|
||||
|
||||
var t =
|
||||
`<div [ngFormModel]="form"><input type="text" ngControl="name" [(ngModel)]="name"></div>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
|
||||
fixture.debugElement.componentInstance.name = 'oldValue';
|
||||
|
@ -946,14 +941,13 @@ export function main() {
|
|||
})));
|
||||
|
||||
it("should support ngModel for single fields",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var form = new Control("");
|
||||
|
||||
var t = `<div><input type="text" [ngFormControl]="form" [(ngModel)]="name"></div>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.form = form;
|
||||
fixture.debugElement.componentInstance.name = "oldValue";
|
||||
|
@ -971,7 +965,7 @@ export function main() {
|
|||
|
||||
describe("template-driven forms", () => {
|
||||
it("should add new controls and control groups",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<form>
|
||||
<div ngControlGroup="user">
|
||||
<input type="text" ngControl="login">
|
||||
|
@ -979,8 +973,7 @@ export function main() {
|
|||
</form>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = null;
|
||||
fixture.detectChanges();
|
||||
|
@ -995,12 +988,11 @@ export function main() {
|
|||
})));
|
||||
|
||||
it("should emit ngSubmit event on submit",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<div><form (ngSubmit)="name='updated'"></form></div>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = 'old';
|
||||
var form = fixture.debugElement.query(By.css("form"));
|
||||
|
@ -1026,7 +1018,7 @@ export function main() {
|
|||
}));
|
||||
|
||||
it("should remove controls",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<form>
|
||||
<div *ngIf="name == 'show'">
|
||||
<input type="text" ngControl="login">
|
||||
|
@ -1034,8 +1026,7 @@ export function main() {
|
|||
</form>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = 'show';
|
||||
fixture.detectChanges();
|
||||
|
@ -1053,7 +1044,7 @@ export function main() {
|
|||
})));
|
||||
|
||||
it("should remove control groups",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<form>
|
||||
<div *ngIf="name=='show'" ngControlGroup="user">
|
||||
<input type="text" ngControl="login">
|
||||
|
@ -1062,8 +1053,7 @@ export function main() {
|
|||
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = 'show';
|
||||
fixture.detectChanges();
|
||||
|
@ -1080,14 +1070,13 @@ export function main() {
|
|||
})));
|
||||
|
||||
it("should support ngModel for complex forms",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<form>
|
||||
<input type="text" ngControl="name" [(ngModel)]="name">
|
||||
</form>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = "oldValue";
|
||||
fixture.detectChanges();
|
||||
|
@ -1105,12 +1094,11 @@ export function main() {
|
|||
|
||||
|
||||
it("should support ngModel for single fields",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<div><input type="text" [(ngModel)]="name"></div>`;
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = "oldValue";
|
||||
fixture.detectChanges();
|
||||
|
@ -1127,7 +1115,7 @@ export function main() {
|
|||
|
||||
|
||||
it("should support <type=radio>",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<form>
|
||||
<input type="radio" name="food" ngControl="chicken" [(ngModel)]="data['chicken1']">
|
||||
<input type="radio" name="food" ngControl="fish" [(ngModel)]="data['fish1']">
|
||||
|
@ -1250,14 +1238,12 @@ export function main() {
|
|||
|
||||
describe("ngModel corner cases", () => {
|
||||
it("should not update the view when the value initially came from the view",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var form = new Control("");
|
||||
|
||||
var t =
|
||||
`<div><input type="text" [ngFormControl]="form" [(ngModel)]="name"></div>`;
|
||||
var t = `<div><input type="text" [ngFormControl]="form" [(ngModel)]="name"></div>`;
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.form = form;
|
||||
fixture.detectChanges();
|
||||
|
@ -1282,11 +1268,10 @@ export function main() {
|
|||
})));
|
||||
|
||||
it("should update the view when the model is set back to what used to be in the view",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
var t = `<input type="text" [(ngModel)]="name">`;
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.debugElement.componentInstance.name = "";
|
||||
fixture.detectChanges();
|
||||
|
@ -1314,15 +1299,14 @@ export function main() {
|
|||
expect(input.value).toEqual("aa");
|
||||
})));
|
||||
it("should not crash when validity is checked from a binding",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
// {{x.valid}} used to crash because valid() tried to read a property
|
||||
// from form.control before it was set. This test verifies this bug is
|
||||
// fixed.
|
||||
var t = `<form><div ngControlGroup="x" #x="ngForm">
|
||||
<input type="text" ngControl="test"></div>{{x.valid}}</form>`;
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { fixture = root; });
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((root) => { fixture = root; });
|
||||
tick();
|
||||
fixture.detectChanges();
|
||||
})));
|
||||
|
|
|
@ -140,8 +140,8 @@ main() {
|
|||
describe("ObservableListDiff", () {
|
||||
it(
|
||||
'should be notified of changes',
|
||||
inject([TestComponentBuilder, Log],
|
||||
fakeAsync((TestComponentBuilder tcb, Log log) {
|
||||
fakeAsync(inject([TestComponentBuilder, Log],
|
||||
(TestComponentBuilder tcb, Log log) {
|
||||
tcb
|
||||
.overrideView(
|
||||
Dummy,
|
||||
|
|
|
@ -755,11 +755,10 @@ function declareTests(isJit: boolean) {
|
|||
|
||||
if (DOM.supportsDOMEvents()) {
|
||||
it("should allow to destroy a component from within a host event handler",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: '<push-cmp-with-host-event></push-cmp-with-host-event>',
|
||||
directives: [[[PushCmpWithHostEvent]]]
|
||||
}))
|
||||
|
@ -829,9 +828,9 @@ function declareTests(isJit: boolean) {
|
|||
|
||||
if (DOM.supportsDOMEvents()) {
|
||||
it('should be checked when an async pipe requests a check',
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
tcb = tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
tcb =
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: '<push-cmp-with-async #cmp></push-cmp-with-async>',
|
||||
directives: [[[PushCmpWithAsyncPipe]]]
|
||||
}));
|
||||
|
@ -840,8 +839,7 @@ function declareTests(isJit: boolean) {
|
|||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
|
||||
var cmp: PushCmpWithAsyncPipe =
|
||||
fixture.debugElement.children[0].getLocal('cmp');
|
||||
var cmp: PushCmpWithAsyncPipe = fixture.debugElement.children[0].getLocal('cmp');
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(1);
|
||||
|
||||
|
@ -1462,8 +1460,7 @@ function declareTests(isJit: boolean) {
|
|||
|
||||
if (DOM.supportsDOMEvents()) { // this is required to use fakeAsync
|
||||
it('should provide an error context when an error happens in an event handler',
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
tcb = tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
template: `<span emitter listener (event)="throwError()" #local></span>`,
|
||||
|
@ -1791,9 +1788,9 @@ function declareTests(isJit: boolean) {
|
|||
|
||||
if (DOM.supportsDOMEvents()) {
|
||||
it('should support event decorators',
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
tcb = tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
tcb =
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: `<with-prop-decorators (elEvent)="ctxProp='called'">`,
|
||||
directives: [DirectiveWithPropDecorators]
|
||||
}));
|
||||
|
@ -1802,8 +1799,7 @@ function declareTests(isJit: boolean) {
|
|||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
|
||||
var emitter =
|
||||
fixture.debugElement.children[0].inject(DirectiveWithPropDecorators);
|
||||
var emitter = fixture.debugElement.children[0].inject(DirectiveWithPropDecorators);
|
||||
emitter.fireEvent('fired !');
|
||||
|
||||
tick();
|
||||
|
|
|
@ -58,7 +58,7 @@ export function main() {
|
|||
}));
|
||||
|
||||
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
let fixture: ComponentFixture;
|
||||
tcb.createAsync(TestComponent).then((f) => { fixture = f; });
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ export function main() {
|
|||
});
|
||||
|
||||
it('should work with inject()',
|
||||
inject([Parser], fakeAsync((parser) => { expect(parser).toBeAnInstanceOf(Parser); })));
|
||||
fakeAsync(inject([Parser], (parser) => { expect(parser).toBeAnInstanceOf(Parser); })));
|
||||
|
||||
it('should throw on nested calls', () => {
|
||||
expect(() => { fakeAsync(() => { fakeAsync(() => null)(); })(); })
|
||||
|
@ -259,6 +259,5 @@ export function main() {
|
|||
.toThrowError('The code should be running in the fakeAsync zone to call this function');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
|
|
@ -106,8 +106,7 @@ export function main() {
|
|||
it('provides a real XHR instance',
|
||||
inject([XHR], (xhr) => { expect(xhr).toBeAnInstanceOf(XHRImpl); }));
|
||||
|
||||
it('should allow the use of fakeAsync',
|
||||
inject([FancyService], fakeAsync((service) => {
|
||||
it('should allow the use of fakeAsync', fakeAsync(inject([FancyService], (service) => {
|
||||
var value;
|
||||
service.getAsyncValue().then(function(val) { value = val; });
|
||||
tick();
|
||||
|
|
|
@ -9,6 +9,8 @@ import {
|
|||
beforeEach,
|
||||
inject,
|
||||
async,
|
||||
fakeAsync,
|
||||
tick,
|
||||
withProviders,
|
||||
beforeEachProviders,
|
||||
TestComponentBuilder
|
||||
|
@ -142,6 +144,13 @@ export function main() {
|
|||
service.getTimeoutValue().then((value) => { expect(value).toEqual('timeout value'); });
|
||||
})));
|
||||
|
||||
it('should allow the use of fakeAsync', fakeAsync(inject([FancyService], (service) => {
|
||||
var value;
|
||||
service.getAsyncValue().then(function(val) { value = val; });
|
||||
tick();
|
||||
expect(value).toEqual('async value');
|
||||
})));
|
||||
|
||||
describe('using beforeEach', () => {
|
||||
beforeEach(inject([FancyService],
|
||||
(service) => { service.value = 'value modified in beforeEach'; }));
|
||||
|
|
|
@ -2231,6 +2231,9 @@
|
|||
"glob": {
|
||||
"version": "4.3.5",
|
||||
"dependencies": {
|
||||
"inflight": {
|
||||
"version": "1.0.4"
|
||||
},
|
||||
"minimatch": {
|
||||
"version": "2.0.10"
|
||||
}
|
||||
|
@ -5819,7 +5822,7 @@
|
|||
}
|
||||
},
|
||||
"zone.js": {
|
||||
"version": "0.6.11"
|
||||
"version": "0.6.12"
|
||||
}
|
||||
},
|
||||
"name": "angular-srcs",
|
||||
|
|
|
@ -3519,6 +3519,11 @@
|
|||
"from": "glob@>=4.3.0 <4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-4.3.5.tgz",
|
||||
"dependencies": {
|
||||
"inflight": {
|
||||
"version": "1.0.4",
|
||||
"from": "inflight@>=1.0.4 <2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.4.tgz"
|
||||
},
|
||||
"minimatch": {
|
||||
"version": "2.0.10",
|
||||
"from": "minimatch@>=2.0.1 <3.0.0",
|
||||
|
@ -9285,8 +9290,9 @@
|
|||
}
|
||||
},
|
||||
"zone.js": {
|
||||
"version": "0.6.11",
|
||||
"from": "zone.js@0.6.11"
|
||||
"version": "0.6.12",
|
||||
"from": "zone.js@0.6.12",
|
||||
"resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.6.12.tgz"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ var path = require('path');
|
|||
require('zone.js/dist/zone-node.js');
|
||||
require('zone.js/dist/long-stack-trace-zone.js');
|
||||
require('zone.js/dist/async-test.js');
|
||||
require('zone.js/dist/fake-async-test.js');
|
||||
require('reflect-metadata/Reflect');
|
||||
|
||||
var jrunner = new JasmineRunner();
|
||||
|
|
Loading…
Reference in New Issue