|
| 1 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { PropsWithChildren } from 'react'; |
| 4 | +import { fetchUserWithCompetitions } from '@/lib/api'; |
| 5 | +import { useMyCompetitionsQuery } from './MyCompetitions.query'; |
| 6 | + |
| 7 | +jest.mock('@/hooks/UsePinnedCompetitions', () => ({ |
| 8 | + usePinnedCompetitions: () => ({ |
| 9 | + pinnedCompetitions: [], |
| 10 | + }), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@/lib/api', () => ({ |
| 14 | + fetchUserWithCompetitions: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('@/lib/localStorage', () => { |
| 18 | + const storage = new Map<string, string>(); |
| 19 | + |
| 20 | + return { |
| 21 | + getLocalStorage: jest.fn((key: string) => storage.get(key) ?? null), |
| 22 | + setLocalStorage: jest.fn((key: string, value: string) => { |
| 23 | + storage.set(key, value); |
| 24 | + }), |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +const user = { id: 13, name: 'Cailyn Sinclair' } as User; |
| 29 | +const upcomingCompetition = { |
| 30 | + id: 'KentSpring2026', |
| 31 | + name: 'Kent Spring 2026', |
| 32 | + city: 'Kent, Washington', |
| 33 | + country_iso2: 'US', |
| 34 | + start_date: '2026-05-30', |
| 35 | + end_date: '2026-05-30', |
| 36 | +} as ApiCompetition; |
| 37 | +const ongoingCompetition = { |
| 38 | + id: 'OngoingOpen2026', |
| 39 | + name: 'Ongoing Open 2026', |
| 40 | + city: 'Seattle, Washington', |
| 41 | + country_iso2: 'US', |
| 42 | + start_date: '2026-05-29', |
| 43 | + end_date: '2026-05-30', |
| 44 | +} as ApiCompetition; |
| 45 | + |
| 46 | +const createWrapper = () => { |
| 47 | + const queryClient = new QueryClient({ |
| 48 | + defaultOptions: { |
| 49 | + queries: { |
| 50 | + retry: false, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + return function Wrapper({ children }: PropsWithChildren) { |
| 56 | + return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>; |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +beforeEach(() => { |
| 61 | + jest.clearAllMocks(); |
| 62 | +}); |
| 63 | + |
| 64 | +it('loads my competitions from the public user competitions endpoint', async () => { |
| 65 | + jest.mocked(fetchUserWithCompetitions).mockResolvedValue({ |
| 66 | + user, |
| 67 | + upcoming_competitions: [upcomingCompetition], |
| 68 | + ongoing_competitions: [ongoingCompetition], |
| 69 | + }); |
| 70 | + |
| 71 | + const { result } = renderHook(() => useMyCompetitionsQuery(user.id), { |
| 72 | + wrapper: createWrapper(), |
| 73 | + }); |
| 74 | + |
| 75 | + await waitFor(() => |
| 76 | + expect(result.current.competitions).toEqual([ongoingCompetition, upcomingCompetition]), |
| 77 | + ); |
| 78 | + |
| 79 | + expect(fetchUserWithCompetitions).toHaveBeenCalledWith(String(user.id)); |
| 80 | +}); |
| 81 | + |
| 82 | +it('uses cached competitions as stale startup data and still refetches', async () => { |
| 83 | + const { setLocalStorage } = jest.requireMock('@/lib/localStorage') as { |
| 84 | + setLocalStorage: (key: string, value: string) => void; |
| 85 | + }; |
| 86 | + |
| 87 | + setLocalStorage('user', JSON.stringify(user)); |
| 88 | + setLocalStorage('my.upcoming_competitions', JSON.stringify([upcomingCompetition])); |
| 89 | + setLocalStorage('my.ongoing_competitions', JSON.stringify([])); |
| 90 | + jest.mocked(fetchUserWithCompetitions).mockResolvedValue({ |
| 91 | + user, |
| 92 | + upcoming_competitions: [], |
| 93 | + ongoing_competitions: [ongoingCompetition], |
| 94 | + }); |
| 95 | + |
| 96 | + const { result } = renderHook(() => useMyCompetitionsQuery(user.id), { |
| 97 | + wrapper: createWrapper(), |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.current.competitions).toEqual([upcomingCompetition]); |
| 101 | + |
| 102 | + await waitFor(() => expect(result.current.competitions).toEqual([ongoingCompetition])); |
| 103 | + expect(fetchUserWithCompetitions).toHaveBeenCalledWith(String(user.id)); |
| 104 | +}); |
0 commit comments