docs: fix failing specs in testing example (#29256)
The tests that need the actuallyDone flag are grouped together PR Close #29256
This commit is contained in:
parent
ec8b74da56
commit
1c251e59d7
@ -4,86 +4,103 @@ import { interval, of } from 'rxjs';
|
|||||||
import { delay, take } from 'rxjs/operators';
|
import { delay, take } from 'rxjs/operators';
|
||||||
|
|
||||||
describe('Angular async helper', () => {
|
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) => {
|
it('should run normal test', () => { actuallyDone = true; });
|
||||||
setTimeout(() => {
|
|
||||||
actuallyDone = true;
|
it('should run normal async test', (done: DoneFn) => {
|
||||||
done();
|
setTimeout(() => {
|
||||||
}, 0);
|
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',
|
describe('fakeAsync', () => {
|
||||||
async(() => { setTimeout(() => { actuallyDone = true; }, 0); }));
|
// #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(() => {
|
// #docregion fake-async-test-date
|
||||||
const id = setInterval(() => {
|
it('should get Date diff correctly in fakeAsync', fakeAsync(() => {
|
||||||
actuallyDone = true;
|
const start = Date.now();
|
||||||
clearInterval(id);
|
tick(100);
|
||||||
}, 100);
|
const end = Date.now();
|
||||||
}));
|
expect(end - start).toBe(100);
|
||||||
|
}));
|
||||||
|
// #enddocregion fake-async-test-date
|
||||||
|
|
||||||
it('should run async test with successful promise', async(() => {
|
// #docregion fake-async-test-rxjs
|
||||||
const p = new Promise(resolve => { setTimeout(resolve, 10); });
|
it('should get Date diff correctly in fakeAsync with rxjs scheduler', fakeAsync(() => {
|
||||||
p.then(() => { actuallyDone = true; });
|
// 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 start = new Date().getTime();
|
||||||
const p = new Promise((resolve, reject) => { setTimeout(reject, 10); });
|
let dateDiff = 0;
|
||||||
p.catch(() => { actuallyDone = true; });
|
interval(1000).pipe(take(2)).subscribe(() => dateDiff = (new Date().getTime() - start));
|
||||||
}));
|
|
||||||
|
|
||||||
// Use done. Can also use async or fakeAsync.
|
tick(1000);
|
||||||
it('should run async test with successful delayed Observable', (done: DoneFn) => {
|
expect(dateDiff).toBe(1000);
|
||||||
const source = of (true).pipe(delay(10));
|
tick(1000);
|
||||||
source.subscribe(val => actuallyDone = true, err => fail(err), done);
|
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
|
// #docregion fake-async-test-clock
|
||||||
describe('use jasmine.clock()', () => {
|
describe('use jasmine.clock()', () => {
|
||||||
// need to config __zone_symbol__fakeAsyncPatchLock flag
|
// need to config __zone_symbol__fakeAsyncPatchLock flag
|
||||||
@ -124,16 +141,4 @@ describe('Angular async helper', () => {
|
|||||||
});
|
});
|
||||||
// #enddocregion async-test-promise-then
|
// #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);
|
|
||||||
}));
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user