Skip to content
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

test: add a test for the behavior of revalidateOnMount when the key has been changed #1847

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
22 changes: 22 additions & 0 deletions test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ describe('useSWR', () => {
expect(fetch).not.toHaveBeenCalled()
})

it('should call fetch function when revalidateOnMount is false and key has been changed', async () => {
const fetch = jest.fn(() => 'SWR')

function Page() {
const [key, setKey] = useState(createKey())
const { data } = useSWR(key, fetch, {
revalidateOnMount: false
})
return <div onClick={() => setKey(createKey)}>hello,{data}</div>
}

renderWithConfig(<Page />)

await screen.findByText('hello,')
expect(fetch).not.toHaveBeenCalled()

// the key has been changed
fireEvent.click(screen.getByText('hello,'))

await screen.findByText('hello,SWR')
})

it('should call fetch function when revalidateOnMount is true even if fallbackData is set', async () => {
const fetch = jest.fn(() => 'SWR')

Expand Down