-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: do not depend on navigator.onLine; code optimizations (#1004)
* fix: do not depend on navigator.onLine * chore: use anonymous functions * micro optimization * micro optimization * add test
- Loading branch information
Showing
4 changed files
with
138 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { act, fireEvent, render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import useSWR from '../src' | ||
import { sleep } from './utils' | ||
|
||
const waitForNextTick = () => act(() => sleep(1)) | ||
const focusWindow = () => | ||
act(async () => { | ||
fireEvent.focus(window) | ||
}) | ||
const dispatchWindowEvent = event => | ||
act(async () => { | ||
window.dispatchEvent(new Event(event)) | ||
}) | ||
|
||
describe('useSWR - offline', () => { | ||
it('should not revalidate when offline', async () => { | ||
let value = 0 | ||
|
||
function Page() { | ||
const { data } = useSWR('offline-1', () => value++, { | ||
dedupingInterval: 0 | ||
}) | ||
return <div>data: {data}</div> | ||
} | ||
const { container } = render(<Page />) | ||
|
||
// hydration | ||
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) | ||
// mount | ||
await screen.findByText('data: 0') | ||
|
||
// simulate offline | ||
await waitForNextTick() | ||
await dispatchWindowEvent('offline') | ||
|
||
// trigger focus revalidation | ||
await focusWindow() | ||
|
||
// should not be revalidated | ||
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 0"`) | ||
}) | ||
|
||
it('should revalidate immediately when becoming online', async () => { | ||
let value = 0 | ||
|
||
function Page() { | ||
const { data } = useSWR('offline-2', () => value++, { | ||
dedupingInterval: 0 | ||
}) | ||
return <div>data: {data}</div> | ||
} | ||
const { container } = render(<Page />) | ||
|
||
// hydration | ||
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`) | ||
// mount | ||
await screen.findByText('data: 0') | ||
|
||
// simulate online | ||
await waitForNextTick() | ||
await dispatchWindowEvent('online') | ||
|
||
// should be revalidated | ||
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 1"`) | ||
}) | ||
}) |