|
| 1 | +import { act, renderHook } from '__tests__/_setup/testing-utils'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { useLiveRefresh } from '../../app/hooks/useLiveRefresh'; |
| 4 | + |
| 5 | +describe('useLiveRefresh', () => { |
| 6 | + it('returns base properties', () => { |
| 7 | + const { result } = renderHook(() => useLiveRefresh()); |
| 8 | + |
| 9 | + expect(result.current.isLiveRefreshRunning).toBeFalsy(); |
| 10 | + expect(result.current.liveRefreshLabel).toBe(''); |
| 11 | + expect(result.current.refetchInterval).toBe(0); |
| 12 | + }); |
| 13 | + |
| 14 | + it.each([ |
| 15 | + { refetch: 5000, label: '5s', time: 'five' }, |
| 16 | + { refetch: 15000, label: '15s', time: 'fifteen' }, |
| 17 | + { refetch: 30000, label: '30s', time: 'thirty' }, |
| 18 | + { refetch: 60000, label: '1m', time: 'sixty' }, |
| 19 | + { refetch: 120000, label: '2m', time: 'onehundredtwenty' }, |
| 20 | + { refetch: 300000, label: '5m', time: 'threehundred' }, |
| 21 | + { refetch: 900000, label: '15m', time: 'ninehundred' }, |
| 22 | + ])('starts fetch interval', ({ label, refetch, time }) => { |
| 23 | + const { result } = renderHook(() => useLiveRefresh()); |
| 24 | + |
| 25 | + act(() => { |
| 26 | + result.current.startLiveRefresh(time); |
| 27 | + }); |
| 28 | + |
| 29 | + expect(result.current.isLiveRefreshRunning).toBeTruthy(); |
| 30 | + expect(result.current.liveRefreshLabel).toBe(label); |
| 31 | + expect(result.current.refetchInterval).toBe(refetch); |
| 32 | + }); |
| 33 | + |
| 34 | + it('stops fetch interval', () => { |
| 35 | + const { result } = renderHook(() => useLiveRefresh()); |
| 36 | + |
| 37 | + act(() => { |
| 38 | + result.current.startLiveRefresh('five'); |
| 39 | + }); |
| 40 | + expect(result.current.refetchInterval).toBe(5000); |
| 41 | + |
| 42 | + act(() => { |
| 43 | + result.current.stopLiveRefresh(); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result.current.isLiveRefreshRunning).toBeFalsy(); |
| 47 | + expect(result.current.refetchInterval).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not activate on invalid time', () => { |
| 51 | + const { result } = renderHook(() => useLiveRefresh()); |
| 52 | + |
| 53 | + act(() => { |
| 54 | + result.current.startLiveRefresh('five'); |
| 55 | + }); |
| 56 | + expect(result.current.refetchInterval).toBe(5000); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + result.current.startLiveRefresh(null); |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result.current.isLiveRefreshRunning).toBeFalsy(); |
| 63 | + expect(result.current.refetchInterval).toBe(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it('set activation time to 0 on unexpected time', () => { |
| 67 | + const { result } = renderHook(() => useLiveRefresh()); |
| 68 | + |
| 69 | + act(() => { |
| 70 | + result.current.startLiveRefresh('uhm'); |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result.current.isLiveRefreshRunning).toBeFalsy(); |
| 74 | + expect(result.current.refetchInterval).toBe(0); |
| 75 | + }); |
| 76 | +}); |
0 commit comments