Skip to content

test: add a test for getInfiniteKey with current data #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,37 @@ describe('useSWRInfinite', () => {
await screen.findByText('data:local-mutation')
})

it('should mutate a cache with getInfiniteKey based on a current data', async () => {
const getKey = index => [`pagetest-13`, index]
function Comp() {
const { data, size, setSize } = useSWRInfinite<string, string>(
getKey,
(_, index) => createResponse(`page ${index}, `)
)
useEffect(() => {
setSize(size + 1)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return <div>data:{data}</div>
}

render(<Comp />)

screen.getByText('data:')
await screen.findByText('data:page 0, page 1,')

await act(() =>
mutate(
getInfiniteKey(getKey),
data => data.map(value => `(edited)${value}`),
false
)
)
await screen.findByText('data:(edited)page 0, (edited)page 1,')
})

it('should be able to use getInfiniteKey with a custom cache', async () => {
const key = 'page-test-13;'
const key = 'page-test-14;'
const customCache1 = new Map([[key, 'initial-cache']])
const { cache, mutate: mutateCustomCache } = createCache(customCache1)
function Page() {
Expand Down