|
| 1 | +import { |
| 2 | + fakeAsync, |
| 3 | + flushMicrotasks, |
| 4 | + TestBed, |
| 5 | + tick, |
| 6 | +} from '@angular/core/testing'; |
| 7 | +import { ApplicationRef, NgZone } from '@angular/core'; |
| 8 | +import { |
| 9 | + AnimationFrameTickScheduler, |
| 10 | + NoopTickScheduler, |
| 11 | + TickScheduler, |
| 12 | +} from '../../src/core/tick-scheduler'; |
| 13 | +import { ngZoneMock, noopNgZoneMock } from '../fixtures/fixtures'; |
| 14 | + |
| 15 | +describe('TickScheduler', () => { |
| 16 | + function setup(ngZone: unknown) { |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + providers: [{ provide: NgZone, useValue: ngZone }], |
| 19 | + }); |
| 20 | + const tickScheduler = TestBed.inject(TickScheduler); |
| 21 | + const appRef = TestBed.inject(ApplicationRef); |
| 22 | + jest.spyOn(appRef, 'tick'); |
| 23 | + |
| 24 | + return { tickScheduler, appRef }; |
| 25 | + } |
| 26 | + |
| 27 | + describe('when NgZone is provided', () => { |
| 28 | + it('should initialize NoopTickScheduler', () => { |
| 29 | + const { tickScheduler } = setup(ngZoneMock); |
| 30 | + expect(tickScheduler instanceof NoopTickScheduler).toBe(true); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('when NgZone is not provided', () => { |
| 35 | + // `fakeAsync` uses 16ms as `requestAnimationFrame` delay |
| 36 | + const animationFrameDelay = 16; |
| 37 | + |
| 38 | + it('should initialize AnimationFrameTickScheduler', () => { |
| 39 | + const { tickScheduler } = setup(noopNgZoneMock); |
| 40 | + expect(tickScheduler instanceof AnimationFrameTickScheduler).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should schedule tick using the animationFrameScheduler', fakeAsync(() => { |
| 44 | + const { tickScheduler, appRef } = setup(noopNgZoneMock); |
| 45 | + |
| 46 | + tickScheduler.schedule(); |
| 47 | + |
| 48 | + expect(appRef.tick).toHaveBeenCalledTimes(0); |
| 49 | + tick(animationFrameDelay / 2); |
| 50 | + expect(appRef.tick).toHaveBeenCalledTimes(0); |
| 51 | + tick(animationFrameDelay / 2); |
| 52 | + expect(appRef.tick).toHaveBeenCalledTimes(1); |
| 53 | + })); |
| 54 | + |
| 55 | + it('should coalesce multiple synchronous schedule calls', fakeAsync(() => { |
| 56 | + const { tickScheduler, appRef } = setup(noopNgZoneMock); |
| 57 | + |
| 58 | + tickScheduler.schedule(); |
| 59 | + tickScheduler.schedule(); |
| 60 | + tickScheduler.schedule(); |
| 61 | + |
| 62 | + tick(animationFrameDelay); |
| 63 | + expect(appRef.tick).toHaveBeenCalledTimes(1); |
| 64 | + })); |
| 65 | + |
| 66 | + it('should coalesce multiple schedule calls that are queued to the microtask queue', fakeAsync(() => { |
| 67 | + const { tickScheduler, appRef } = setup(noopNgZoneMock); |
| 68 | + |
| 69 | + queueMicrotask(() => tickScheduler.schedule()); |
| 70 | + queueMicrotask(() => tickScheduler.schedule()); |
| 71 | + queueMicrotask(() => tickScheduler.schedule()); |
| 72 | + |
| 73 | + flushMicrotasks(); |
| 74 | + expect(appRef.tick).toHaveBeenCalledTimes(0); |
| 75 | + tick(animationFrameDelay); |
| 76 | + expect(appRef.tick).toHaveBeenCalledTimes(1); |
| 77 | + })); |
| 78 | + |
| 79 | + it('should schedule multiple ticks for multiple asynchronous schedule calls', fakeAsync(() => { |
| 80 | + const { tickScheduler, appRef } = setup(noopNgZoneMock); |
| 81 | + |
| 82 | + setTimeout(() => tickScheduler.schedule(), 100); |
| 83 | + setTimeout(() => tickScheduler.schedule(), 200); |
| 84 | + setTimeout(() => tickScheduler.schedule(), 300); |
| 85 | + |
| 86 | + tick(300 + animationFrameDelay); |
| 87 | + expect(appRef.tick).toHaveBeenCalledTimes(3); |
| 88 | + })); |
| 89 | + }); |
| 90 | +}); |
0 commit comments