Skip to content

Commit e64af12

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

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/use-swr-infinite.test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,36 @@ 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+
function Comp() {
614+
const { data, size, setSize } = useSWRInfinite<string, string>(
615+
index => [`pagetest-13`, index],
616+
(_, index) => createResponse(`page ${index}, `)
617+
)
618+
useEffect(() => {
619+
setSize(size + 1)
620+
// eslint-disable-next-line react-hooks/exhaustive-deps
621+
}, [])
622+
return <div>data:{data}</div>
623+
}
624+
625+
render(<Comp />)
626+
627+
screen.getByText('data:')
628+
await screen.findByText('data:page 0, page 1,')
629+
630+
await act(() =>
631+
mutate(
632+
getInfiniteKey(i => [`pagetest-13`, i]),
633+
data => data.map(d => `(edited)${d}`),
634+
false
635+
)
636+
)
637+
await screen.findByText('data:(edited)page 0, (edited)page 1,')
638+
})
639+
612640
it('should be able to use getInfiniteKey with a custom cache', async () => {
613-
const key = 'page-test-13;'
641+
const key = 'page-test-14;'
614642
const customCache1 = new Map([[key, 'initial-cache']])
615643
const { cache, mutate: mutateCustomCache } = createCache(customCache1)
616644
function Page() {

0 commit comments

Comments
 (0)