Skip to content

Commit f32cfdb

Browse files
committed
test: add a test for getInfiniteKey with current data
1 parent dc1444f commit f32cfdb

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

test/use-swr-infinite.test.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,37 @@ describe('useSWRInfinite', () => {
609609
await screen.findByText('data:local-mutation')
610610
})
611611

612+
it('should mutate a cache with getInfiniteKey based on a current data', async () => {
613+
const getKey = index => [`pagetest-13`, index]
614+
function Comp() {
615+
const { data, size, setSize } = useSWRInfinite<string, string>(
616+
getKey,
617+
(_, index) => createResponse(`page ${index}, `)
618+
)
619+
useEffect(() => {
620+
setSize(size + 1)
621+
// eslint-disable-next-line react-hooks/exhaustive-deps
622+
}, [])
623+
return <div>data:{data}</div>
624+
}
625+
626+
render(<Comp />)
627+
628+
screen.getByText('data:')
629+
await screen.findByText('data:page 0, page 1,')
630+
631+
await act(() =>
632+
mutate(
633+
getInfiniteKey(getKey),
634+
data => data.map(value => `(edited)${value}`),
635+
false
636+
)
637+
)
638+
await screen.findByText('data:(edited)page 0, (edited)page 1,')
639+
})
640+
612641
it('should be able to use getInfiniteKey with a custom cache', async () => {
613-
const key = 'page-test-13;'
642+
const key = 'page-test-14;'
614643
const customCache1 = new Map([[key, 'initial-cache']])
615644
const { cache, mutate: mutateCustomCache } = createCache(customCache1)
616645
function Page() {

0 commit comments

Comments
 (0)