Skip to content

Commit

Permalink
test: refactor use-swr-focus.test.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Mar 14, 2021
1 parent 766718a commit 149192f
Showing 1 changed file with 20 additions and 16 deletions.
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 149192f

Please sign in to comment.