|
| 1 | +import { act, render, screen, userEvent } from '__tests__/_setup/testing-utils'; |
| 2 | +import { RefreshButton } from 'app/components/Refresh/RefreshButton'; |
| 3 | +import { liveRefreshOptions } from 'app/hooks/useLiveRefresh'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +vi.mock('react-hook-form', async () => { |
| 7 | + const actual = |
| 8 | + await vi.importActual<typeof import('react-hook-form')>('react-hook-form'); |
| 9 | + |
| 10 | + return { ...actual, useWatch: () => 'page' }; |
| 11 | +}); |
| 12 | + |
| 13 | +const headers = () => { |
| 14 | + const head = new Headers(); |
| 15 | + head.append('authorization', 'test'); |
| 16 | + return head; |
| 17 | +}; |
| 18 | +vi.mock('../../../app/hooks/useAuthProperties', () => ({ |
| 19 | + useAuthProperties: () => ({ |
| 20 | + fetchInfo: { |
| 21 | + headers: { headers: headers() }, |
| 22 | + routePrefix: '', |
| 23 | + }, |
| 24 | + isHeaderReady: true, |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +describe('RefreshButton', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 31 | + }); |
| 32 | + afterEach(vi.useRealTimers); |
| 33 | + |
| 34 | + it('renders', async () => { |
| 35 | + render(<RefreshButton />); |
| 36 | + |
| 37 | + const durationSelector = screen.getByLabelText('refresh-duration-selector'); |
| 38 | + expect(durationSelector).toBeInTheDocument(); |
| 39 | + |
| 40 | + await userEvent.click(durationSelector); |
| 41 | + await act(vi.advanceTimersToNextTimerAsync); |
| 42 | + |
| 43 | + const times = liveRefreshOptions.map((lro) => lro.value); |
| 44 | + times.forEach((time) => { |
| 45 | + expect(screen.getByLabelText('refresh-duration-' + time)).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('runs live feed activies with refetch sample', async () => { |
| 50 | + const spy = vi.spyOn(global, 'fetch'); |
| 51 | + render(<RefreshButton />); |
| 52 | + |
| 53 | + const durationSelector = screen.getByLabelText('refresh-duration-selector'); |
| 54 | + await userEvent.click(durationSelector); |
| 55 | + await act(vi.advanceTimersToNextTimerAsync); |
| 56 | + |
| 57 | + const sampleOpt = liveRefreshOptions[5]; |
| 58 | + const timeSelector = screen.getByLabelText('refresh-duration-' + sampleOpt.value); |
| 59 | + await userEvent.click(timeSelector); |
| 60 | + await act(vi.advanceTimersToNextTimerAsync); |
| 61 | + |
| 62 | + expect(spy).toHaveBeenCalledOnce(); |
| 63 | + |
| 64 | + await act(async () => { |
| 65 | + await vi.advanceTimersByTimeAsync(1000 * 300 + 1); |
| 66 | + }); |
| 67 | + expect(spy).toHaveBeenCalledTimes(2); |
| 68 | + |
| 69 | + const durationStopper = screen.getByLabelText('refresh-duration-cancel-button'); |
| 70 | + expect(screen.queryByLabelText('refresh-duration-selector')).not.toBeInTheDocument(); |
| 71 | + expect(durationStopper).toBeInTheDocument(); |
| 72 | + |
| 73 | + await userEvent.click(durationStopper); |
| 74 | + await act(vi.advanceTimersToNextTimerAsync); |
| 75 | + |
| 76 | + expect( |
| 77 | + screen.queryByLabelText('refresh-duration-cancel-button'), |
| 78 | + ).not.toBeInTheDocument(); |
| 79 | + expect(screen.getByLabelText('refresh-duration-selector')).toBeInTheDocument(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments