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:
committed by
vikerman
parent
cc86fee1d1
commit
bab81a9831
@@ -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,26 +755,25 @@ 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({
|
||||
template: '<push-cmp-with-host-event></push-cmp-with-host-event>',
|
||||
directives: [[[PushCmpWithHostEvent]]]
|
||||
}))
|
||||
var fixture: ComponentFixture;
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: '<push-cmp-with-host-event></push-cmp-with-host-event>',
|
||||
directives: [[[PushCmpWithHostEvent]]]
|
||||
}))
|
||||
|
||||
.createAsync(MyComp)
|
||||
.then(root => { fixture = root; });
|
||||
tick();
|
||||
fixture.detectChanges();
|
||||
.createAsync(MyComp)
|
||||
.then(root => { fixture = root; });
|
||||
tick();
|
||||
fixture.detectChanges();
|
||||
|
||||
var cmpEl = fixture.debugElement.children[0];
|
||||
var cmp: PushCmpWithHostEvent = cmpEl.inject(PushCmpWithHostEvent);
|
||||
cmp.ctxCallback = (_) => fixture.destroy();
|
||||
var cmpEl = fixture.debugElement.children[0];
|
||||
var cmp: PushCmpWithHostEvent = cmpEl.inject(PushCmpWithHostEvent);
|
||||
cmp.ctxCallback = (_) => fixture.destroy();
|
||||
|
||||
expect(() => cmpEl.triggerEventHandler('click', <Event>{})).not.toThrow();
|
||||
})));
|
||||
expect(() => cmpEl.triggerEventHandler('click', <Event>{})).not.toThrow();
|
||||
})));
|
||||
}
|
||||
|
||||
it("should be checked when an event is fired",
|
||||
@@ -829,32 +828,31 @@ 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({
|
||||
template: '<push-cmp-with-async #cmp></push-cmp-with-async>',
|
||||
directives: [[[PushCmpWithAsyncPipe]]]
|
||||
}));
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
tcb =
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: '<push-cmp-with-async #cmp></push-cmp-with-async>',
|
||||
directives: [[[PushCmpWithAsyncPipe]]]
|
||||
}));
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
|
||||
var cmp: PushCmpWithAsyncPipe =
|
||||
fixture.debugElement.children[0].getLocal('cmp');
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(1);
|
||||
var cmp: PushCmpWithAsyncPipe = fixture.debugElement.children[0].getLocal('cmp');
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(1);
|
||||
|
||||
fixture.detectChanges();
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(1);
|
||||
fixture.detectChanges();
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(1);
|
||||
|
||||
cmp.resolve(2);
|
||||
tick();
|
||||
cmp.resolve(2);
|
||||
tick();
|
||||
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(2);
|
||||
})));
|
||||
fixture.detectChanges();
|
||||
expect(cmp.numberOfChecks).toEqual(2);
|
||||
})));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1462,35 +1460,34 @@ 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>`,
|
||||
directives: [DirectiveEmittingEvent, DirectiveListeningEvent]
|
||||
}));
|
||||
|
||||
tcb = tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
template: `<span emitter listener (event)="throwError()" #local></span>`,
|
||||
directives: [DirectiveEmittingEvent, DirectiveListeningEvent]
|
||||
}));
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
var tc = fixture.debugElement.children[0];
|
||||
tc.inject(DirectiveEmittingEvent).fireEvent("boom");
|
||||
|
||||
var tc = fixture.debugElement.children[0];
|
||||
tc.inject(DirectiveEmittingEvent).fireEvent("boom");
|
||||
try {
|
||||
tick();
|
||||
throw "Should throw";
|
||||
} catch (e) {
|
||||
clearPendingTimers();
|
||||
|
||||
try {
|
||||
tick();
|
||||
throw "Should throw";
|
||||
} catch (e) {
|
||||
clearPendingTimers();
|
||||
|
||||
var c = e.context;
|
||||
expect(DOM.nodeName(c.renderNode).toUpperCase()).toEqual("SPAN");
|
||||
expect(DOM.nodeName(c.componentRenderElement).toUpperCase()).toEqual("DIV");
|
||||
expect((<Injector>c.injector).get).toBeTruthy();
|
||||
expect(c.context).toBe(fixture.debugElement.componentInstance);
|
||||
expect(c.locals["local"]).toBeDefined();
|
||||
}
|
||||
})));
|
||||
var c = e.context;
|
||||
expect(DOM.nodeName(c.renderNode).toUpperCase()).toEqual("SPAN");
|
||||
expect(DOM.nodeName(c.componentRenderElement).toUpperCase()).toEqual("DIV");
|
||||
expect((<Injector>c.injector).get).toBeTruthy();
|
||||
expect(c.context).toBe(fixture.debugElement.componentInstance);
|
||||
expect(c.locals["local"]).toBeDefined();
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
if (!IS_DART) {
|
||||
@@ -1791,25 +1788,24 @@ function declareTests(isJit: boolean) {
|
||||
|
||||
if (DOM.supportsDOMEvents()) {
|
||||
it('should support event decorators',
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
tcb = tcb.overrideView(
|
||||
MyComp, new ViewMetadata({
|
||||
template: `<with-prop-decorators (elEvent)="ctxProp='called'">`,
|
||||
directives: [DirectiveWithPropDecorators]
|
||||
}));
|
||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
tcb =
|
||||
tcb.overrideView(MyComp, new ViewMetadata({
|
||||
template: `<with-prop-decorators (elEvent)="ctxProp='called'">`,
|
||||
directives: [DirectiveWithPropDecorators]
|
||||
}));
|
||||
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
var fixture: ComponentFixture;
|
||||
tcb.createAsync(MyComp).then(root => { fixture = root; });
|
||||
tick();
|
||||
|
||||
var emitter =
|
||||
fixture.debugElement.children[0].inject(DirectiveWithPropDecorators);
|
||||
emitter.fireEvent('fired !');
|
||||
var emitter = fixture.debugElement.children[0].inject(DirectiveWithPropDecorators);
|
||||
emitter.fireEvent('fired !');
|
||||
|
||||
tick();
|
||||
tick();
|
||||
|
||||
expect(fixture.debugElement.componentInstance.ctxProp).toEqual("called");
|
||||
})));
|
||||
expect(fixture.debugElement.componentInstance.ctxProp).toEqual("called");
|
||||
})));
|
||||
|
||||
|
||||
it('should support host listener decorators',
|
||||
|
||||
Reference in New Issue
Block a user