Skip to content

Improve accuracy in determining visibility state #4120

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions src/_internal/utils/web-preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ProviderConfiguration } from '../types'
import { isWindowDefined, isDocumentDefined } from './helper'
import { isUndefined, noop } from './shared'
import { noop } from './shared'

/**
* Due to the bug https://bugs.chromium.org/p/chromium/issues/detail?id=678075,
@@ -22,8 +22,18 @@ const [onWindowEvent, offWindowEvent] =
: [noop, noop]

const isVisible = () => {
const visibilityState = isDocumentDefined && document.visibilityState
return isUndefined(visibilityState) || visibilityState !== 'hidden'
if (!isDocumentDefined) return true

try {
const isDocVisible = document.visibilityState !== 'hidden'

const isDocFocused =
typeof document.hasFocus === 'function' ? document.hasFocus() : true

return isDocVisible && isDocFocused
} catch (err) {
return true
}
}

const initFocus = (callback: () => void) => {
56 changes: 56 additions & 0 deletions test/unit/web-preset-visible.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it, beforeEach, afterEach } from 'vitest'

Check failure on line 1 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot find module 'vitest' or its corresponding type declarations.
import { preset } from '../../src/_internal/utils/web-preset'

describe('web-preset isVisible', () => {
let originalDocument: any

beforeEach(() => {
originalDocument = global.document
global.document = {

Check failure on line 9 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Type '{ visibilityState: "visible"; hasFocus: () => true; }' is missing the following properties from type 'Document': URL, alinkColor, all, anchors, and 248 more.
visibilityState: 'visible',
hasFocus: () => true
}
})

afterEach(() => {
global.document = originalDocument
})

it('returns true when document is visible and focused', () => {
global.document.visibilityState = 'visible'

Check failure on line 20 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot assign to 'visibilityState' because it is a read-only property.
global.document.hasFocus = () => true
expect(preset.isVisible()).toBe(true)
})

it('returns false when document is hidden and not focused', () => {
global.document.visibilityState = 'hidden'

Check failure on line 26 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot assign to 'visibilityState' because it is a read-only property.
global.document.hasFocus = () => false
expect(preset.isVisible()).toBe(false)
})

it('returns false when document is hidden but focused', () => {
global.document.visibilityState = 'hidden'

Check failure on line 32 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot assign to 'visibilityState' because it is a read-only property.
global.document.hasFocus = () => true
expect(preset.isVisible()).toBe(false)
})

it('returns false when document is visible but not focused', () => {
global.document.visibilityState = 'visible'

Check failure on line 38 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot assign to 'visibilityState' because it is a read-only property.
global.document.hasFocus = () => false
expect(preset.isVisible()).toBe(false)
})

it('returns true if hasFocus is not a function', () => {
global.document.visibilityState = 'visible'

Check failure on line 44 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Cannot assign to 'visibilityState' because it is a read-only property.
// @ts-expect-error

Check failure on line 45 in test/unit/web-preset-visible.test.ts

GitHub Actions / test

Unused '@ts-expect-error' directive.
global.document.hasFocus = undefined
expect(preset.isVisible()).toBe(true)
})

it('returns true if there is an exception', () => {
Object.defineProperty(global, 'document', {
get() { throw new Error('Simulated error') }
})
expect(preset.isVisible()).toBe(true)
})
})

Unchanged files with check annotations Beta

// after 300ms the rendered result should be 3
await executeWithoutBatching(() => sleep(350))
screen.getByText('data:3')

Check failure on line 580 in test/use-swr-infinite.test.tsx

GitHub Actions / test

useSWRInfinite › should not break refreshInterval

TestingLibraryElementError: Unable to find an element with the text: data:3. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> data: 0 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-infinite.test.tsx:580:12)
})
it('should re-use fallbackData', async () => {
await screen.findByText('count: 0')
await act(() => advanceTimers(200)) // update
screen.getByText('count: 1')

Check failure on line 39 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should rerender automatically on interval

TestingLibraryElementError: Unable to find an element with the text: count: 1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 0 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:39:12)
await act(() => advanceTimers(50)) // no update
screen.getByText('count: 1')
await act(() => advanceTimers(150)) // update
screen.getByText('count: 0')
await act(() => advanceTimers(400)) // reach dudupingInterval
await act(() => advanceTimers(100)) // update
screen.getByText('count: 1')

Check failure on line 70 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should dedupe requests combined with intervals

TestingLibraryElementError: Unable to find an element with the text: count: 1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 0 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:70:12)
await act(() => advanceTimers(100)) // no update (deduped)
screen.getByText('count: 1')
await act(() => advanceTimers(400)) // reach dudupingInterval
await screen.findByText('count: 0')
await act(() => advanceTimers(100))
screen.getByText('count: 1')

Check failure on line 101 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should update data upon interval changes

TestingLibraryElementError: Unable to find an element with the text: count: 1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 0 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:101:12)
await act(() => advanceTimers(50))
screen.getByText('count: 1')
await act(() => advanceTimers(50))
await act(() => advanceTimers(100))
screen.getByText('count: 1 2')

Check failure on line 164 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should update data upon interval changes -- changes happened during revalidate

TestingLibraryElementError: Unable to find an element with the text: count: 1 2. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 0 1 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:164:12)
await act(() => advanceTimers(100))
screen.getByText('count: 1 2')
// first refresh
await act(() => advanceTimers(100))
expect(fetcherWithToken).toBeCalledTimes(2)

Check failure on line 531 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should not let the previous interval timer to set new timer if key changes too fast

expect(jest.fn()).toBeCalledTimes(expected) Expected number of calls: 2 Received number of calls: 1 at Object.toBeCalledTimes (test/use-swr-refresh.test.tsx:531:30)
expect(fetcherWithToken).toHaveBeenLastCalledWith(`${key}-0`)
await act(() => advanceTimers(200))
expect(onSuccess).toHaveLastReturnedWith(`0-${key} 0-${key}`)
// first refresh
await act(() => advanceTimers(50))
expect(fetcherWithToken).toBeCalledTimes(2)

Check failure on line 587 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should not call onSuccess from the previous interval if key has changed

expect(jest.fn()).toBeCalledTimes(expected) Expected number of calls: 2 Received number of calls: 1 at Object.toBeCalledTimes (test/use-swr-refresh.test.tsx:587:30)
expect(fetcherWithToken).toHaveBeenLastCalledWith(`0-${key}`)
await act(() => advanceTimers(100))
expect(onSuccess).toBeCalledTimes(2)
await screen.findByText('count: 0')
await act(() => advanceTimers(200)) // update
screen.getByText('count: 1')

Check failure on line 636 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should allow using function as an interval

TestingLibraryElementError: Unable to find an element with the text: count: 1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 0 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:636:12)
await act(() => advanceTimers(50)) // no update
screen.getByText('count: 1')
await act(() => advanceTimers(150)) // update
await screen.findByText('count: 1')
await act(() => advanceTimers(1000)) // updated after 1s
screen.getByText('count: 2')

Check failure on line 664 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should pass updated data to refreshInterval

TestingLibraryElementError: Unable to find an element with the text: count: 2. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 1 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:664:12)
await act(() => advanceTimers(1000)) // no update
screen.getByText('count: 2')
await act(() => advanceTimers(1000)) // updated after 2s
await screen.findByText('count: 1')
await act(() => advanceTimers(1000))
screen.getByText('count: 2')

Check failure on line 706 in test/use-swr-refresh.test.tsx

GitHub Actions / test

useSWR - refresh › should pass updated data to refreshInterval

TestingLibraryElementError: Unable to find an element with the text: count: 2. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div> count: 1 </div> </div> </body> at Object.getElementError (node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/config.js:37:19) at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 at node_modules/.pnpm/@testing-library+dom@9.2.0/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 at Object.getByText (test/use-swr-refresh.test.tsx:706:12)
expect(refreshInterval).toHaveBeenLastCalledWith(2)
await act(() => advanceTimers(1000))
screen.getByText('count: 3')