|
| 1 | +import React, { ReactElement } from 'react'; |
| 2 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 3 | +import usePresentation, { TFrameOptions } from './index'; |
| 4 | + |
| 5 | +// Mock for requestAnimationFrame |
| 6 | +let rafCallbacks: Array<(time: number) => void> = []; |
| 7 | +let mockRafIdCounter = 0; |
| 8 | +let currentMockTime = 0; |
| 9 | + |
| 10 | +const mockPerformanceNow = jest.fn().mockImplementation(() => currentMockTime); |
| 11 | + |
| 12 | +const mockRequestAnimationFrame = (callback: (time: number) => void): number => { |
| 13 | + rafCallbacks.push(callback); |
| 14 | + mockRafIdCounter++; |
| 15 | + return mockRafIdCounter; |
| 16 | +}; |
| 17 | + |
| 18 | +const mockCancelAnimationFrame = (id: number) => { |
| 19 | + // Basic mock. |
| 20 | +}; |
| 21 | + |
| 22 | +// Helper to advance RAF frames |
| 23 | +const advanceRaf = (frames: number = 1) => { |
| 24 | + for (let i = 0; i < frames; i++) { |
| 25 | + const callback = rafCallbacks.shift(); |
| 26 | + if (callback) { |
| 27 | + currentMockTime += 16; // Simulate 16ms passing |
| 28 | + callback(currentMockTime); |
| 29 | + } else { |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +describe('usePresentation Hook', () => { |
| 36 | + let originalRaf: (callback: FrameRequestCallback) => number; |
| 37 | + let originalCancelRaf: (handle: number) => void; |
| 38 | + let originalPerformanceNow: () => number; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + originalRaf = window.requestAnimationFrame; |
| 42 | + originalCancelRaf = window.cancelAnimationFrame; |
| 43 | + originalPerformanceNow = performance.now; |
| 44 | + |
| 45 | + window.requestAnimationFrame = mockRequestAnimationFrame as any; |
| 46 | + window.cancelAnimationFrame = mockCancelAnimationFrame; |
| 47 | + performance.now = mockPerformanceNow; |
| 48 | + |
| 49 | + currentMockTime = 0; |
| 50 | + rafCallbacks = []; |
| 51 | + mockRafIdCounter = 0; |
| 52 | + |
| 53 | + jest.useFakeTimers(); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + window.requestAnimationFrame = originalRaf; |
| 58 | + window.cancelAnimationFrame = originalCancelRaf; |
| 59 | + performance.now = originalPerformanceNow; |
| 60 | + jest.clearAllMocks(); |
| 61 | + jest.useRealTimers(); |
| 62 | + rafCallbacks = []; |
| 63 | + }); |
| 64 | + |
| 65 | + const getFrameComponent = (id: string): ReactElement => <div data-testid={id} />; |
| 66 | + |
| 67 | + it('should return correct initial state', () => { |
| 68 | + const framesOptions: TFrameOptions[] = [ |
| 69 | + { component: getFrameComponent('1'), time: 100 }, |
| 70 | + ]; |
| 71 | + const { result } = renderHook(() => usePresentation({ framesOptions, startTrigger: false })); |
| 72 | + const [, currentFrame, framesQuantity] = result.current; |
| 73 | + expect(currentFrame).toBe(0); |
| 74 | + expect(framesQuantity).toBe(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should start animation after startDelay and cycle frames with callbacks', () => { |
| 78 | + const mockCallback = jest.fn(); |
| 79 | + const framesOptions: TFrameOptions[] = [ |
| 80 | + { component: getFrameComponent('frame1'), time: 100 }, |
| 81 | + { component: getFrameComponent('frame2'), time: 200 }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const { result, rerender } = renderHook( |
| 85 | + (props) => usePresentation(props), |
| 86 | + { |
| 87 | + initialProps: { |
| 88 | + framesOptions, |
| 89 | + startTrigger: false, |
| 90 | + startDelay: 50, |
| 91 | + callback: mockCallback, |
| 92 | + isLoop: false, |
| 93 | + }, |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + expect(result.current[1]).toBe(0); |
| 98 | + |
| 99 | + rerender({ framesOptions, startTrigger: true, startDelay: 50, callback: mockCallback, isLoop: false }); |
| 100 | + |
| 101 | + act(() => { |
| 102 | + jest.advanceTimersByTime(50); |
| 103 | + }); |
| 104 | + |
| 105 | + act(() => { advanceRaf(1); }); |
| 106 | + expect(result.current[1]).toBe(1); |
| 107 | + |
| 108 | + for(let i = 0; i < Math.ceil(100/16) + 1; i++) { |
| 109 | + act(() => { advanceRaf(1); }); |
| 110 | + } |
| 111 | + expect(result.current[1]).toBe(1); |
| 112 | + expect(mockCallback).toHaveBeenCalledTimes(1); |
| 113 | + |
| 114 | + act(() => { advanceRaf(1); }); |
| 115 | + expect(result.current[1]).toBe(2); |
| 116 | + |
| 117 | + for(let i = 0; i < Math.ceil(200/16) + 1; i++) { |
| 118 | + act(() => { advanceRaf(1); }); |
| 119 | + } |
| 120 | + expect(result.current[1]).toBe(2); |
| 121 | + expect(mockCallback).toHaveBeenCalledTimes(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should cancel animation if startTrigger becomes false', () => { |
| 125 | + const mockCallback = jest.fn(); |
| 126 | + const framesOptions: TFrameOptions[] = [ |
| 127 | + { component: getFrameComponent('frame1'), time: 100 }, |
| 128 | + { component: getFrameComponent('frame2'), time: 200 }, |
| 129 | + ]; |
| 130 | + |
| 131 | + const { result, rerender } = renderHook( |
| 132 | + (props) => usePresentation(props), |
| 133 | + { |
| 134 | + initialProps: { |
| 135 | + framesOptions, |
| 136 | + startTrigger: true, |
| 137 | + startDelay: 0, |
| 138 | + callback: mockCallback, |
| 139 | + isLoop: false, |
| 140 | + }, |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + act(() => { advanceRaf(1); }); |
| 145 | + expect(result.current[1]).toBe(1); |
| 146 | + |
| 147 | + act(() => { advanceRaf(2); }); |
| 148 | + |
| 149 | + rerender({ framesOptions, startTrigger: false, startDelay: 0, callback: mockCallback, isLoop: false }); |
| 150 | + |
| 151 | + act(() => { advanceRaf(10); }); |
| 152 | + |
| 153 | + expect(result.current[1]).toBe(1); |
| 154 | + expect(mockCallback).not.toHaveBeenCalled(); |
| 155 | + }); |
| 156 | +}); |
0 commit comments