From 149192f374e8137bba94bce6c2a3872cad972331 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Sun, 14 Mar 2021 22:30:59 +0900 Subject: [PATCH] test: refactor use-swr-focus.test.tsx --- test/use-swr-focus.test.tsx | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/test/use-swr-focus.test.tsx b/test/use-swr-focus.test.tsx index 8790c2304..25d5c5b45 100644 --- a/test/use-swr-focus.test.tsx +++ b/test/use-swr-focus.test.tsx @@ -19,10 +19,10 @@ describe('useSWR - focus', () => { }) return
data: {data}
} - const { container } = render() + render() // hydration - expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) + screen.getByText('data:') // mount await screen.findByText('data: 0') @@ -43,10 +43,11 @@ describe('useSWR - focus', () => { }) return
data: {data}
} - const { container } = render() + render() // hydration - expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) + screen.getByText('data:') + // mount await screen.findByText('data: 0') @@ -54,7 +55,7 @@ describe('useSWR - focus', () => { // 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 @@ -68,10 +69,11 @@ describe('useSWR - focus', () => { }) return
toggle(s => !s)}>data: {data}
} - const { container } = render() + render() // hydration - expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) + screen.getByText('data:') + // mount await screen.findByText('data: 0') @@ -79,10 +81,10 @@ describe('useSWR - focus', () => { // 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 @@ -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 () => { @@ -118,10 +120,11 @@ describe('useSWR - focus', () => { ) return
data: {data}
} - const { container } = render() + render() // hydration - expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) + screen.getByText('data:') + // mount await screen.findByText('data: 0') @@ -157,10 +160,11 @@ describe('useSWR - focus', () => { ) return
setInterval(s => s + 100)}>data: {data}
} - const { container } = render() + render() // hydration - expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) + screen.getByText('data:') + // mount await screen.findByText('data: 0') @@ -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