|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import { useState } from 'react'; |
| 5 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 6 | +import { mockFetchResponse } from '../../utils/mockFetchResponse'; |
| 7 | +import { useFetcher } from '../../../src/react/index'; |
| 8 | + |
| 9 | +describe('POST body cache key update and refetch', () => { |
| 10 | + it('uses new body on refetch, keeps static cache key, and updates UI state', async () => { |
| 11 | + mockFetchResponse('/api/user', { |
| 12 | + ok: true, |
| 13 | + status: 200, |
| 14 | + body: { echoed: { name: 'Alice' } }, |
| 15 | + }); |
| 16 | + |
| 17 | + function TestComponent() { |
| 18 | + const [name, setName] = useState('Alice'); |
| 19 | + const { data, refetch, isLoading, isFetching, config } = useFetcher( |
| 20 | + '/api/user', |
| 21 | + { |
| 22 | + method: 'POST', |
| 23 | + body: { name }, |
| 24 | + cacheKey: '/api/user', |
| 25 | + immediate: true, |
| 26 | + retry: { retries: 5, delay: 1000, backoff: 2 }, |
| 27 | + }, |
| 28 | + ); |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + <div data-testid="result">{data?.echoed?.name}</div> |
| 32 | + <div data-testid="config">{JSON.stringify(config)}</div> |
| 33 | + <div data-testid="loading">{isLoading ? 'loading' : 'idle'}</div> |
| 34 | + <div data-testid="fetching">{isFetching ? 'fetching' : 'idle'}</div> |
| 35 | + <button onClick={() => setName('Bob')}>Change Name</button> |
| 36 | + <button onClick={() => refetch()}>Refetch</button> |
| 37 | + </div> |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + render(<TestComponent />); |
| 42 | + await waitFor(() => |
| 43 | + expect(screen.getByTestId('result').textContent).toBe('Alice'), |
| 44 | + ); |
| 45 | + |
| 46 | + mockFetchResponse('/api/user', { |
| 47 | + ok: true, |
| 48 | + status: 200, |
| 49 | + body: { echoed: { name: 'Bob' } }, |
| 50 | + }); |
| 51 | + fireEvent.click(screen.getByText('Change Name')); |
| 52 | + fireEvent.click(screen.getByText('Refetch')); |
| 53 | + expect(screen.getByTestId('loading').textContent).toBe('loading'); |
| 54 | + expect(screen.getByTestId('fetching').textContent).toBe('fetching'); |
| 55 | + await waitFor(() => |
| 56 | + expect(screen.getByTestId('result').textContent).toBe('Bob'), |
| 57 | + ); |
| 58 | + expect(screen.getByTestId('loading').textContent).toBe('idle'); |
| 59 | + expect(screen.getByTestId('fetching').textContent).toBe('idle'); |
| 60 | + expect(screen.getByTestId('config').textContent).toContain( |
| 61 | + '"body":"{\\"name\\":\\"Bob\\"}"', |
| 62 | + ); |
| 63 | + expect(screen.getByTestId('config').textContent).toContain( |
| 64 | + '"cacheKey":"/api/user"', |
| 65 | + ); |
| 66 | + }); |
| 67 | +}); |
0 commit comments