posts.test.tsx92 lines, 2.9 KB
| 1 | import { |
| 2 | QueryClient, |
| 3 | QueryClientProvider, |
| 4 | useInfiniteQuery, |
| 5 | type InfiniteData, |
| 6 | } from '@tanstack/react-query' |
| 7 | import { act, renderHook, waitFor } from '@testing-library/react' |
| 8 | import type { ReactNode } from 'react' |
| 9 | import { afterEach, expect, test, vi } from 'vitest' |
| 10 | import type { Page, Post } from '../../lib/api' |
| 11 | import { postsOptions, useLike } from './posts' |
| 12 | |
| 13 | afterEach(() => vi.unstubAllGlobals()) |
| 14 | |
| 15 | const wrap = |
| 16 | (client: QueryClient) => |
| 17 | ({ children }: { children: ReactNode }) => ( |
| 18 | <QueryClientProvider client={client}> |
| 19 | {children} |
| 20 | </QueryClientProvider> |
| 21 | ) |
| 22 | |
| 23 | const post = (id: number, likes = 0): Post => ({ |
| 24 | id, |
| 25 | likes, |
| 26 | liked: false, |
| 27 | handle: 'alice', |
| 28 | body: 'hello', |
| 29 | created_at: '2026-01-01T00:00:00Z', |
| 30 | }) |
| 31 | |
| 32 | test('pages follow next_cursor until it is null', async () => { |
| 33 | const fetch = vi |
| 34 | .fn() |
| 35 | .mockResolvedValueOnce( |
| 36 | Response.json({ posts: [post(9), post(8)], next_cursor: 8 }), |
| 37 | ) |
| 38 | .mockResolvedValueOnce( |
| 39 | Response.json({ posts: [post(7)], next_cursor: null }), |
| 40 | ) |
| 41 | vi.stubGlobal('fetch', fetch) |
| 42 | const { result } = renderHook( |
| 43 | () => useInfiniteQuery(postsOptions('/feed', 'home')), |
| 44 | { wrapper: wrap(new QueryClient()) }, |
| 45 | ) |
| 46 | await waitFor(() => expect(result.current.hasNextPage).toBe(true)) |
| 47 | await act(() => result.current.fetchNextPage()) |
| 48 | |
| 49 | expect(fetch.mock.calls.map((c) => c[0])).toEqual([ |
| 50 | '/api/feed?cursor=', |
| 51 | '/api/feed?cursor=8', |
| 52 | ]) |
| 53 | await waitFor(() => expect(result.current.hasNextPage).toBe(false)) |
| 54 | const pages = result.current.data?.pages |
| 55 | expect(pages?.flatMap((p) => p.posts.map((x) => x.id))).toEqual([ |
| 56 | 9, 8, 7, |
| 57 | ]) |
| 58 | }) |
| 59 | |
| 60 | // The same post in the feed and on a profile: a like changes both at |
| 61 | // once, and a failed like puts both back. |
| 62 | test.each([ |
| 63 | ['it works', 204, { liked: true, likes: 4 }], |
| 64 | ['it fails', 500, { liked: false, likes: 3 }], |
| 65 | ])('a like shows at once, and when %s', async (_name, status, want) => { |
| 66 | let answer: (r: Response) => void = () => {} |
| 67 | vi.stubGlobal( |
| 68 | 'fetch', |
| 69 | vi.fn(() => new Promise<Response>((r) => (answer = r))), |
| 70 | ) |
| 71 | const client = new QueryClient() |
| 72 | const page = { |
| 73 | pages: [{ posts: [post(1, 3)], next_cursor: null }], |
| 74 | pageParams: [0], |
| 75 | } |
| 76 | client.setQueryData(['posts', 'home'], page) |
| 77 | client.setQueryData(['posts', '@alice'], page) |
| 78 | const { result } = renderHook(useLike, { wrapper: wrap(client) }) |
| 79 | const seen = (key: string) => |
| 80 | client.getQueryData<InfiniteData<Page>>(['posts', key])?.pages[0] |
| 81 | .posts[0] |
| 82 | |
| 83 | act(() => result.current.mutate(post(1, 3))) |
| 84 | await waitFor(() => expect(seen('home')?.liked).toBe(true)) |
| 85 | expect(seen('@alice')).toMatchObject({ liked: true, likes: 4 }) |
| 86 | |
| 87 | answer(new Response(status === 204 ? null : '{}', { status })) |
| 88 | await waitFor(() => expect(result.current.isIdle).toBe(false)) |
| 89 | await waitFor(() => expect(result.current.isPending).toBe(false)) |
| 90 | expect(seen('home')).toMatchObject(want) |
| 91 | expect(seen('@alice')).toMatchObject(want) |
| 92 | }) |