|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { requestReversibleAnimationFrame } from '..'; |
| 6 | + |
| 7 | +beforeAll(() => { |
| 8 | + jest.useFakeTimers(); |
| 9 | +}); |
| 10 | +afterAll(() => { |
| 11 | + jest.useRealTimers(); |
| 12 | +}); |
| 13 | +afterEach(() => { |
| 14 | + jest.resetAllMocks(); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('requestReversibleAnimationFrame', () => { |
| 18 | + test('triggers', () => { |
| 19 | + const callback = jest.fn(); |
| 20 | + jest.spyOn(window, 'requestAnimationFrame'); |
| 21 | + requestReversibleAnimationFrame(callback); |
| 22 | + expect(requestAnimationFrame).toHaveBeenCalledTimes(1); |
| 23 | + expect(requestAnimationFrame).toHaveBeenLastCalledWith(callback); |
| 24 | + expect(callback).not.toHaveBeenCalled(); |
| 25 | + jest.runOnlyPendingTimers(); |
| 26 | + expect(callback).toHaveBeenCalled(); |
| 27 | + jest.runOnlyPendingTimers(); |
| 28 | + jest.runOnlyPendingTimers(); |
| 29 | + jest.runOnlyPendingTimers(); |
| 30 | + expect(callback).toHaveBeenCalledTimes(1); // runs only once |
| 31 | + }); |
| 32 | + test('cancels', () => { |
| 33 | + const callback = jest.fn(); |
| 34 | + jest.spyOn(window, 'cancelAnimationFrame'); |
| 35 | + const cancel = requestReversibleAnimationFrame(callback); |
| 36 | + expect(callback).not.toHaveBeenCalled(); |
| 37 | + expect(cancelAnimationFrame).not.toHaveBeenCalled(); |
| 38 | + cancel(); |
| 39 | + expect(cancelAnimationFrame).toHaveBeenCalledTimes(1); |
| 40 | + jest.runOnlyPendingTimers(); |
| 41 | + expect(callback).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | +}); |
0 commit comments