|
1 |
| -describe |
| 1 | +import {fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing'; |
| 2 | +import {of} from 'rxjs'; |
| 3 | +import {delay} from 'rxjs/operators'; |
| 4 | + |
| 5 | +fdescribe('Async testing examples', () => { |
| 6 | + it('Async test example with Jasmine done()', (done: DoneFn) => { |
| 7 | + let test = false; |
| 8 | + |
| 9 | + setTimeout(() => { |
| 10 | + console.log('running assertions'); |
| 11 | + test = true; |
| 12 | + expect(test).toBeTruthy(); |
| 13 | + done(); |
| 14 | + }, 1000); |
| 15 | + }); |
| 16 | + |
| 17 | + it('Async test example setTimeout', fakeAsync(() => { |
| 18 | + let test = false; |
| 19 | + |
| 20 | + setTimeout(() => { |
| 21 | + }); |
| 22 | + setTimeout(() => { |
| 23 | + console.log('running assertions setTimeout'); |
| 24 | + test = true; |
| 25 | + }, 1000); |
| 26 | + flush(); |
| 27 | + // tick(500); |
| 28 | + // tick(500); |
| 29 | + expect(test).toBeTruthy(); |
| 30 | + })); |
| 31 | + |
| 32 | + it('Async test example plan promise', fakeAsync(() => { |
| 33 | + let test = false; |
| 34 | + console.log('creating promise'); |
| 35 | + |
| 36 | + Promise.resolve().then(() => { |
| 37 | + console.log('Promise resolve1 successfully'); |
| 38 | + return Promise.resolve(); |
| 39 | + }).then(() => { |
| 40 | + test = true; |
| 41 | + console.log('Promise resolve2successfully'); |
| 42 | + }); |
| 43 | + flushMicrotasks(); |
| 44 | + console.log('running assertions plan promise'); |
| 45 | + expect(test).toBeTruthy(); |
| 46 | + })); |
| 47 | + it('Async test example plan promise + timeouts', fakeAsync(() => { |
| 48 | + let counter = 0; |
| 49 | + |
| 50 | + Promise.resolve().then(() => { |
| 51 | + counter += 10; |
| 52 | + |
| 53 | + setTimeout(() => { |
| 54 | + counter += 1; |
| 55 | + }, 1000); |
| 56 | + }); |
| 57 | + |
| 58 | + expect(counter).toBe(0); |
| 59 | + flushMicrotasks(); |
| 60 | + expect(counter).toBe(10); |
| 61 | + tick(500); |
| 62 | + expect(counter).toBe(10); |
| 63 | + tick(500); |
| 64 | + expect(counter).toBe(11); |
| 65 | + })); |
| 66 | + |
| 67 | + it('Async test example plan Observables', fakeAsync(() => { |
| 68 | + let test = false; |
| 69 | + console.log('Creating Observable'); |
| 70 | + const test$ = of(test).pipe(delay(1000)); |
| 71 | + test$.subscribe(() => { |
| 72 | + test = true; |
| 73 | + }); |
| 74 | + |
| 75 | + tick(1000); |
| 76 | + |
| 77 | + console.log('running assertions'); |
| 78 | + expect(test).toBe(true); |
| 79 | + })); |
| 80 | +}); |
0 commit comments