|
| 1 | +import { act, renderHook } from 'react-hooks-testing-library'; |
| 2 | +import useRafLoop from '../useRafLoop'; |
| 3 | + |
| 4 | +describe('useRafLoop', () => { |
| 5 | + it('should be defined', () => { |
| 6 | + expect(useRafLoop).toBeDefined(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should call a callback constantly inside the raf loop', done => { |
| 10 | + let calls = 0; |
| 11 | + const spy = () => calls++; |
| 12 | + renderHook(() => useRafLoop(spy), { initialProps: false }); |
| 13 | + |
| 14 | + expect(calls).toEqual(0); |
| 15 | + |
| 16 | + setTimeout(() => { |
| 17 | + expect(calls).toBeGreaterThanOrEqual(5); |
| 18 | + expect(calls).toBeLessThan(10); |
| 19 | + |
| 20 | + done(); |
| 21 | + }, 100); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return stop function, start function and loop state', () => { |
| 25 | + const hook = renderHook(() => useRafLoop(() => false), { initialProps: false }); |
| 26 | + |
| 27 | + expect(typeof hook.result.current[0]).toEqual('function'); |
| 28 | + expect(typeof hook.result.current[1]).toEqual('boolean'); |
| 29 | + expect(typeof hook.result.current[2]).toEqual('function'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('first element call should stop the loop', done => { |
| 33 | + let calls = 0; |
| 34 | + const spy = () => calls++; |
| 35 | + const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); |
| 36 | + |
| 37 | + // stop the loop |
| 38 | + act(() => { |
| 39 | + hook.result.current[0](); |
| 40 | + }); |
| 41 | + |
| 42 | + setTimeout(() => { |
| 43 | + expect(calls).toEqual(0); |
| 44 | + |
| 45 | + done(); |
| 46 | + }, 100); |
| 47 | + }); |
| 48 | + |
| 49 | + it('second element should represent loop state', done => { |
| 50 | + let calls = 0; |
| 51 | + const spy = () => calls++; |
| 52 | + const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); |
| 53 | + |
| 54 | + expect(hook.result.current[1]).toBe(true); |
| 55 | + |
| 56 | + // stop the loop |
| 57 | + act(() => { |
| 58 | + hook.result.current[0](); |
| 59 | + }); |
| 60 | + |
| 61 | + expect(hook.result.current[1]).toBe(false); |
| 62 | + setTimeout(() => { |
| 63 | + expect(calls).toEqual(0); |
| 64 | + |
| 65 | + done(); |
| 66 | + }, 100); |
| 67 | + }); |
| 68 | + |
| 69 | + it('third element call should restart loop', done => { |
| 70 | + let calls = 0; |
| 71 | + const spy = () => calls++; |
| 72 | + const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); |
| 73 | + |
| 74 | + expect(hook.result.current[1]).toBe(true); |
| 75 | + |
| 76 | + // stop the loop |
| 77 | + act(() => { |
| 78 | + hook.result.current[0](); |
| 79 | + }); |
| 80 | + |
| 81 | + setTimeout(() => { |
| 82 | + expect(hook.result.current[1]).toBe(false); |
| 83 | + expect(calls).toEqual(0); |
| 84 | + |
| 85 | + // start the loop |
| 86 | + act(() => { |
| 87 | + hook.result.current[2](); |
| 88 | + }); |
| 89 | + |
| 90 | + setTimeout(() => { |
| 91 | + expect(hook.result.current[1]).toBe(true); |
| 92 | + expect(calls).toBeGreaterThanOrEqual(5); |
| 93 | + expect(calls).toBeLessThan(10); |
| 94 | + |
| 95 | + done(); |
| 96 | + }, 100); |
| 97 | + }, 100); |
| 98 | + }); |
| 99 | + |
| 100 | + it('loop should stop itself on unmount', done => { |
| 101 | + let calls = 0; |
| 102 | + const spy = () => calls++; |
| 103 | + const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); |
| 104 | + |
| 105 | + hook.unmount(); |
| 106 | + |
| 107 | + setTimeout(() => { |
| 108 | + expect(calls).toEqual(0); |
| 109 | + |
| 110 | + done(); |
| 111 | + }, 100); |
| 112 | + }); |
| 113 | +}); |
0 commit comments