Skip to content

Commit

Permalink
test: refactor use-swr-focus.test.tsx (#1036)
Browse files Browse the repository at this point in the history
* test: refactor use-swr-focus.test.tsx

* test: make a config callback test more reliable
  • Loading branch information
koba04 authored Mar 16, 2021
1 parent 5070f82 commit d40ba0e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
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

0 comments on commit d40ba0e

Please sign in to comment.