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
155 changes: 155 additions & 0 deletions packages/core/test/async_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {async} from '@angular/core/testing';

function testAsync(fn: Function, doneFn?: Function) {
const asyncWrapper = async(fn);
return function(done: Function) {
return asyncWrapper.apply(this, [function() {
if (doneFn) {
doneFn();
}
return done.apply(this, arguments);
}]);
};
}

describe('async', () => {
describe('test without beforeEach', () => {
const logs: string[] = [];
it('should automatically done after async tasks finished',
testAsync(
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
() => {
expect(logs).toEqual(['timeout']);
logs.splice(0);
}));

it('should automatically done after all nested async tasks finished',
testAsync(
() => {
setTimeout(() => {
logs.push('timeout');
setTimeout(() => { logs.push('nested timeout'); }, 100);
}, 100);
},
() => {
expect(logs).toEqual(['timeout', 'nested timeout']);
logs.splice(0);
}));

it('should automatically done after multiple async tasks finished',
testAsync(
() => {
setTimeout(() => { logs.push('1st timeout'); }, 100);

setTimeout(() => { logs.push('2nd timeout'); }, 100);
},
() => {
expect(logs).toEqual(['1st timeout', '2nd timeout']);
logs.splice(0);
}));
});

describe('test with sync beforeEach', () => {
const logs: string[] = [];

beforeEach(() => {
logs.splice(0);
logs.push('beforeEach');
});

it('should automatically done after async tasks finished',
testAsync(
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
() => {
expect(logs).toEqual(['beforeEach', 'timeout']);
}));
});

describe('test with async beforeEach', () => {
const logs: string[] = [];

beforeEach(testAsync(() => {
setTimeout(() => {
logs.splice(0);
logs.push('beforeEach');
}, 100);
}));

it('should automatically done after async tasks finished',
testAsync(
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
() => {
expect(logs).toEqual(['beforeEach', 'timeout']);
}));

it('should automatically done after all nested async tasks finished',
testAsync(
() => {
setTimeout(() => {
logs.push('timeout');
setTimeout(() => { logs.push('nested timeout'); }, 100);
}, 100);
},
() => {
expect(logs).toEqual(['beforeEach', 'timeout', 'nested timeout']);
}));

it('should automatically done after multiple async tasks finished',
testAsync(
() => {
setTimeout(() => { logs.push('1st timeout'); }, 100);

setTimeout(() => { logs.push('2nd timeout'); }, 100);
},
() => {
expect(logs).toEqual(['beforeEach', '1st timeout', '2nd timeout']);
}));
});

describe('test with async beforeEach and sync afterEach', () => {
const logs: string[] = [];

beforeEach(testAsync(() => {
setTimeout(() => {
expect(logs).toEqual([]);
logs.push('beforeEach');
}, 100);
}));

afterEach(() => { logs.splice(0); });

it('should automatically done after async tasks finished',
testAsync(
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
() => {
expect(logs).toEqual(['beforeEach', 'timeout']);
}));
});

describe('test with async beforeEach and async afterEach', () => {
const logs: string[] = [];

beforeEach(testAsync(() => {
setTimeout(() => {
expect(logs).toEqual([]);
logs.push('beforeEach');
}, 100);
}));

afterEach(testAsync(() => { setTimeout(() => { logs.splice(0); }, 100); }));

it('should automatically done after async tasks finished',
testAsync(
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
() => {
expect(logs).toEqual(['beforeEach', 'timeout']);
}));
});
});
26 changes: 11 additions & 15 deletions packages/core/testing/src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function runInTestZone(
'Please make sure that your environment includes zone.js/dist/async-test.js');
}
const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'] as {
get(): {setDelegate(spec: ZoneSpec): void; getDelegate(): ZoneSpec;};
get(): {setDelegate(spec: ZoneSpec): void; getDelegate(): ZoneSpec};
assertPresent: () => void;
};
if (ProxyZoneSpec === undefined) {
Expand All @@ -87,23 +87,19 @@ function runInTestZone(
const testZoneSpec: ZoneSpec = new AsyncTestZoneSpec(
() => {
// Need to restore the original zone.
currentZone.run(() => {
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
proxyZoneSpec.setDelegate(previousDelegate);
}
finishCallback();
});
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
proxyZoneSpec.setDelegate(previousDelegate);
}
currentZone.run(() => { finishCallback(); });
},
(error: any) => {
// Need to restore the original zone.
currentZone.run(() => {
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
proxyZoneSpec.setDelegate(previousDelegate);
}
failCallback(error);
});
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
proxyZoneSpec.setDelegate(previousDelegate);
}
currentZone.run(() => { failCallback(error); });
},
'test');
proxyZoneSpec.setDelegate(testZoneSpec);
Expand Down