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: refactor use-swr-focus.test.tsx #1036

Merged
merged 2 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion test/use-swr-config-callbacks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('useSWR - config callbacks', () => {
})

it('should trigger the onLoadingSlow and onSuccess event with the lastest version of the callbacks', async () => {
const LOADING_TIMEOUT = 5
const LOADING_TIMEOUT = 100
let state = null
let count = 0

Expand Down
36 changes: 20 additions & 16 deletions test/use-swr-focus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe('useSWR - focus', () => {
})
return <div>data: {data}</div>
}
const { container } = render(<Page />)

render(<Page />)
// hydration
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
screen.getByText('data:')
// mount
await screen.findByText('data: 0')

Expand All @@ -43,18 +43,19 @@ describe('useSWR - focus', () => {
})
return <div>data: {data}</div>
}
const { container } = render(<Page />)

render(<Page />)
// hydration
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
screen.getByText('data:')

// mount
await screen.findByText('data: 0')

await waitForNextTick()
// trigger revalidation
await focusWindow()
// should not be revalidated
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 0"`)
screen.getByText('data: 0')
})
it('revalidateOnFocus shoule be stateful', async () => {
let value = 0
Expand All @@ -68,21 +69,22 @@ describe('useSWR - focus', () => {
})
return <div onClick={() => toggle(s => !s)}>data: {data}</div>
}
const { container } = render(<Page />)

render(<Page />)
// hydration
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
screen.getByText('data:')

// mount
await screen.findByText('data: 0')

await waitForNextTick()
// trigger revalidation
await focusWindow()
// data should not change
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 0"`)
screen.getByText('data: 0')

// change revalidateOnFocus to true
fireEvent.click(container.firstElementChild)
fireEvent.click(screen.getByText('data: 0'))
// trigger revalidation
await focusWindow()
// data should update
Expand All @@ -96,11 +98,11 @@ describe('useSWR - focus', () => {

await waitForNextTick()
// change revalidateOnFocus to false
fireEvent.click(container.firstElementChild)
fireEvent.click(screen.getByText('data: 2'))
// trigger revalidation
await focusWindow()
// data should not change
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 2"`)
screen.getByText('data: 2')
})

it('focusThrottleInterval should work', async () => {
Expand All @@ -118,10 +120,11 @@ describe('useSWR - focus', () => {
)
return <div>data: {data}</div>
}
const { container } = render(<Page />)

render(<Page />)
// hydration
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
screen.getByText('data:')

// mount
await screen.findByText('data: 0')

Expand Down Expand Up @@ -157,10 +160,11 @@ describe('useSWR - focus', () => {
)
return <div onClick={() => setInterval(s => s + 100)}>data: {data}</div>
}
const { container } = render(<Page />)

render(<Page />)
// hydration
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
screen.getByText('data:')

// mount
await screen.findByText('data: 0')

Expand All @@ -175,7 +179,7 @@ describe('useSWR - focus', () => {

await waitForNextTick()
// increase focusThrottleInterval
fireEvent.click(container.firstElementChild)
fireEvent.click(screen.getByText('data: 2'))
// wait for throttle interval
await act(() => sleep(100))
// trigger revalidation
Expand Down