Skip to content

Commit

Permalink
add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Apr 16, 2022
1 parent 6959c39 commit 972cefc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/use-swr-laggy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,38 @@ describe('useSWR - keep previous data', () => {
[key2, [key2]]
])
})

it('should support changing the `keepPreviousData` option', async () => {
const loggedData = []
const fetcher = k => createResponse(k, { delay: 50 })
let keepPreviousData = false
function App() {
const [key, setKey] = useState(createKey())
const { data: laggedData } = useSWR(key, fetcher, {
keepPreviousData
})
loggedData.push([key, laggedData])
return <button onClick={() => setKey(createKey())}>change key</button>
}

renderWithConfig(<App />)
await act(() => sleep(100))
fireEvent.click(screen.getByText('change key'))
await act(() => sleep(100))
keepPreviousData = true
fireEvent.click(screen.getByText('change key'))
await act(() => sleep(100))

const key1 = loggedData[0][0]
const key2 = loggedData[2][0]
const key3 = loggedData[4][0]
expect(loggedData).toEqual([
[key1, undefined],
[key1, key1],
[key2, undefined],
[key2, key2],
[key3, key2],
[key3, key3]
])
})
})

0 comments on commit 972cefc

Please sign in to comment.