diff --git a/aio/content/examples/testing/src/app/demo/async-helper.spec.ts b/aio/content/examples/testing/src/app/demo/async-helper.spec.ts index 9c9c3cb7d8..ca0239408d 100644 --- a/aio/content/examples/testing/src/app/demo/async-helper.spec.ts +++ b/aio/content/examples/testing/src/app/demo/async-helper.spec.ts @@ -4,86 +4,103 @@ import { interval, of } from 'rxjs'; import { delay, take } from 'rxjs/operators'; describe('Angular async helper', () => { - let actuallyDone = false; - beforeEach(() => { actuallyDone = false; }); + describe('async', () => { + let actuallyDone = false; - afterEach(() => { expect(actuallyDone).toBe(true, 'actuallyDone should be true'); }); + beforeEach(() => { actuallyDone = false; }); - it('should run normal test', () => { actuallyDone = true; }); + afterEach(() => { expect(actuallyDone).toBe(true, 'actuallyDone should be true'); }); - it('should run normal async test', (done: DoneFn) => { - setTimeout(() => { - actuallyDone = true; - done(); - }, 0); + it('should run normal test', () => { actuallyDone = true; }); + + it('should run normal async test', (done: DoneFn) => { + setTimeout(() => { + actuallyDone = true; + done(); + }, 0); + }); + + it('should run async test with task', + async(() => { setTimeout(() => { actuallyDone = true; }, 0); })); + + it('should run async test with task', async(() => { + const id = setInterval(() => { + actuallyDone = true; + clearInterval(id); + }, 100); + })); + + it('should run async test with successful promise', async(() => { + const p = new Promise(resolve => { setTimeout(resolve, 10); }); + p.then(() => { actuallyDone = true; }); + })); + + it('should run async test with failed promise', async(() => { + const p = new Promise((resolve, reject) => { setTimeout(reject, 10); }); + p.catch(() => { actuallyDone = true; }); + })); + + // Use done. Can also use async or fakeAsync. + it('should run async test with successful delayed Observable', (done: DoneFn) => { + const source = of (true).pipe(delay(10)); + source.subscribe(val => actuallyDone = true, err => fail(err), done); + }); + + it('should run async test with successful delayed Observable', async(() => { + const source = of (true).pipe(delay(10)); + source.subscribe(val => actuallyDone = true, err => fail(err)); + })); + + it('should run async test with successful delayed Observable', fakeAsync(() => { + const source = of (true).pipe(delay(10)); + source.subscribe(val => actuallyDone = true, err => fail(err)); + + tick(10); + })); }); - it('should run async test with task', - async(() => { setTimeout(() => { actuallyDone = true; }, 0); })); + describe('fakeAsync', () => { + // #docregion fake-async-test-tick + it('should run timeout callback with delay after call tick with millis', fakeAsync(() => { + let called = false; + setTimeout(() => { called = true; }, 100); + tick(100); + expect(called).toBe(true); + })); + // #enddocregion fake-async-test-tick - it('should run async test with task', async(() => { - const id = setInterval(() => { - actuallyDone = true; - clearInterval(id); - }, 100); - })); + // #docregion fake-async-test-date + it('should get Date diff correctly in fakeAsync', fakeAsync(() => { + const start = Date.now(); + tick(100); + const end = Date.now(); + expect(end - start).toBe(100); + })); + // #enddocregion fake-async-test-date - it('should run async test with successful promise', async(() => { - const p = new Promise(resolve => { setTimeout(resolve, 10); }); - p.then(() => { actuallyDone = true; }); - })); + // #docregion fake-async-test-rxjs + it('should get Date diff correctly in fakeAsync with rxjs scheduler', fakeAsync(() => { + // need to add `import 'zone.js/dist/zone-patch-rxjs-fake-async' + // to patch rxjs scheduler + let result = null; + of ('hello').pipe(delay(1000)).subscribe(v => { result = v; }); + expect(result).toBeNull(); + tick(1000); + expect(result).toBe('hello'); - it('should run async test with failed promise', async(() => { - const p = new Promise((resolve, reject) => { setTimeout(reject, 10); }); - p.catch(() => { actuallyDone = true; }); - })); + const start = new Date().getTime(); + let dateDiff = 0; + interval(1000).pipe(take(2)).subscribe(() => dateDiff = (new Date().getTime() - start)); - // Use done. Can also use async or fakeAsync. - it('should run async test with successful delayed Observable', (done: DoneFn) => { - const source = of (true).pipe(delay(10)); - source.subscribe(val => actuallyDone = true, err => fail(err), done); + tick(1000); + expect(dateDiff).toBe(1000); + tick(1000); + expect(dateDiff).toBe(2000); + })); + // #enddocregion fake-async-test-rxjs }); - // #docregion fake-async-test-tick - it('should run timeout callback with delay after call tick with millis', fakeAsync(() => { - let called = false; - setTimeout(() => { called = true; }, 100); - tick(100); - expect(called).toBe(true); - })); - // #enddocregion fake-async-test-tick - - // #docregion fake-async-test-date - it('should get Date diff correctly in fakeAsync', fakeAsync(() => { - const start = Date.now(); - tick(100); - const end = Date.now(); - expect(end - start).toBe(100); - })); - // #enddocregion fake-async-test-date - - // #docregion fake-async-test-rxjs - it('should get Date diff correctly in fakeAsync with rxjs scheduler', fakeAsync(() => { - // need to add `import 'zone.js/dist/zone-patch-rxjs-fake-async' - // to patch rxjs scheduler - let result = null; - of ('hello').pipe(delay(1000)).subscribe(v => { result = v; }); - expect(result).toBeNull(); - tick(1000); - expect(result).toBe('hello'); - - const start = new Date().getTime(); - let dateDiff = 0; - interval(1000).pipe(take(2)).subscribe(() => dateDiff = (new Date().getTime() - start)); - - tick(1000); - expect(dateDiff).toBe(1000); - tick(1000); - expect(dateDiff).toBe(2000); - })); - // #enddocregion fake-async-test-rxjs - // #docregion fake-async-test-clock describe('use jasmine.clock()', () => { // need to config __zone_symbol__fakeAsyncPatchLock flag @@ -124,16 +141,4 @@ describe('Angular async helper', () => { }); // #enddocregion async-test-promise-then - it('should run async test with successful delayed Observable', async(() => { - const source = of (true).pipe(delay(10)); - source.subscribe(val => actuallyDone = true, err => fail(err)); - })); - - it('should run async test with successful delayed Observable', fakeAsync(() => { - const source = of (true).pipe(delay(10)); - source.subscribe(val => actuallyDone = true, err => fail(err)); - - tick(10); - })); - });