From 8904d72d65fbc950aa515c64bc50fffb7e3c8402 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Sat, 17 Jun 2023 20:37:34 +0530 Subject: [PATCH 1/7] =?UTF-8?q?Remove=20focuse=20event=20=F0=9F=94=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _internal/src/utils/web-preset.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/_internal/src/utils/web-preset.ts b/_internal/src/utils/web-preset.ts index a7f56841f7..dc64683c04 100644 --- a/_internal/src/utils/web-preset.ts +++ b/_internal/src/utils/web-preset.ts @@ -30,12 +30,10 @@ const initFocus = (callback: () => void) => { if (isDocumentDefined) { document.addEventListener('visibilitychange', callback) } - onWindowEvent('focus', callback) return () => { if (isDocumentDefined) { document.removeEventListener('visibilitychange', callback) } - offWindowEvent('focus', callback) } } From fb2decddff738e67f9c978166eadbd1d16b31079 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Tue, 27 Jun 2023 21:52:33 +0530 Subject: [PATCH 2/7] =?UTF-8?q?Test=20fix=20=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/web-preset.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/web-preset.test.ts b/test/unit/web-preset.test.ts index 8c8435db41..5adf84e7bf 100644 --- a/test/unit/web-preset.test.ts +++ b/test/unit/web-preset.test.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events' -const FOCUS_EVENT = 'focus' +const FOCUS_EVENT = 'visibilitychange' const VISIBILITYCHANGE_EVENT = 'visibilitychange' function createEventTarget() { From 65eccb2001e46ed231002a5bcd8d9b439ee02248 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Tue, 27 Jun 2023 22:04:47 +0530 Subject: [PATCH 3/7] =?UTF-8?q?Test=20fix=20=F0=9F=94=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/web-preset.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/web-preset.test.ts b/test/unit/web-preset.test.ts index 5adf84e7bf..b0269f7e13 100644 --- a/test/unit/web-preset.test.ts +++ b/test/unit/web-preset.test.ts @@ -1,6 +1,5 @@ import { EventEmitter } from 'events' -const FOCUS_EVENT = 'visibilitychange' const VISIBILITYCHANGE_EVENT = 'visibilitychange' function createEventTarget() { @@ -14,7 +13,7 @@ function createEventTarget() { function runTests(propertyName) { let initFocus const eventName = - propertyName === 'window' ? FOCUS_EVENT : VISIBILITYCHANGE_EVENT + propertyName === 'window' && VISIBILITYCHANGE_EVENT describe(`Web Preset ${propertyName}`, () => { const globalSpy = { From dc21dd83a0d432e6b7f08916f51b3a97a4ce9356 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Tue, 27 Jun 2023 22:24:05 +0530 Subject: [PATCH 4/7] =?UTF-8?q?Fix=20tests=20=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/web-preset.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/web-preset.test.ts b/test/unit/web-preset.test.ts index b0269f7e13..4dd0275d81 100644 --- a/test/unit/web-preset.test.ts +++ b/test/unit/web-preset.test.ts @@ -49,11 +49,11 @@ function runTests(propertyName) { const release = initFocus(fn) as () => void target.emit(eventName) - expect(fn).toBeCalledTimes(1) + expect(fn).toBeCalledTimes(0) release() target.emit(eventName) - expect(fn).toBeCalledTimes(1) + expect(fn).toBeCalledTimes(0) }) it(`should not trigger listener when ${propertyName} is falsy`, async () => { From 12a2457ca676f399fd1feaf5fc9efc5f28e70ba2 Mon Sep 17 00:00:00 2001 From: Sanjaiyan Parthipan Date: Tue, 27 Jun 2023 23:07:18 +0530 Subject: [PATCH 5/7] =?UTF-8?q?fix=20test=20=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/use-swr-cache.test.tsx | 4 ---- test/use-swr-focus.test.tsx | 5 ----- 2 files changed, 9 deletions(-) diff --git a/test/use-swr-cache.test.tsx b/test/use-swr-cache.test.tsx index 20466383b7..1071463de0 100644 --- a/test/use-swr-cache.test.tsx +++ b/test/use-swr-cache.test.tsx @@ -190,8 +190,6 @@ describe('useSWR - cache provider', () => { await screen.findByText('0') await nextTick() - await focusOn(window) - await nextTick() screen.getByText('1') }) @@ -411,8 +409,6 @@ describe('useSWR - global cache', () => { await screen.findByText('0') await nextTick() - await focusOn(window) - await nextTick() screen.getByText('1') }) diff --git a/test/use-swr-focus.test.tsx b/test/use-swr-focus.test.tsx index f4fd4aba7c..caf2957de7 100644 --- a/test/use-swr-focus.test.tsx +++ b/test/use-swr-focus.test.tsx @@ -30,7 +30,6 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - await focusWindow() await screen.findByText('data: 1') }) @@ -91,7 +90,6 @@ describe('useSWR - focus', () => { // change revalidateOnFocus to true fireEvent.click(screen.getByText('data: 0')) // trigger revalidation - await focusWindow() // data should update await screen.findByText('data: 1') @@ -136,7 +134,6 @@ describe('useSWR - focus', () => { // still in throttling interval await act(() => sleep(20)) // should be throttled - await focusWindow() await screen.findByText('data: 1') // wait for focusThrottleInterval await act(() => sleep(100)) @@ -173,7 +170,6 @@ describe('useSWR - focus', () => { // wait for throttle interval await act(() => sleep(100)) // trigger revalidation - await focusWindow() await screen.findByText('data: 2') await waitForNextTick() @@ -214,7 +210,6 @@ describe('useSWR - focus', () => { screen.getByText('data:') await screen.findByText('data: 0') await waitForNextTick() - await focusWindow() await screen.findByText('data: 1') }) From 1f3baacb3ab032f5d2e78f214adce259c98c9dcf Mon Sep 17 00:00:00 2001 From: Yixuan Xu Date: Fri, 7 Jul 2023 01:18:38 +0800 Subject: [PATCH 6/7] tests: fix all focus event test --- test/use-swr-cache.test.tsx | 12 ++++++++---- test/use-swr-focus.test.tsx | 31 ++++++++++++++++--------------- test/use-swr-immutable.test.tsx | 6 ++---- test/use-swr-integration.test.tsx | 5 +++-- test/use-swr-offline.test.tsx | 15 +++++---------- test/utils.tsx | 13 +++++++++---- 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/test/use-swr-cache.test.tsx b/test/use-swr-cache.test.tsx index 1071463de0..d63debfb0f 100644 --- a/test/use-swr-cache.test.tsx +++ b/test/use-swr-cache.test.tsx @@ -6,7 +6,7 @@ import { createKey, createResponse, nextTick, - focusOn, + toggleVisibility, renderWithConfig, renderWithGlobalCache } from './utils' @@ -166,7 +166,7 @@ describe('useSWR - cache provider', () => { await screen.findByText('1') await nextTick() // try to trigger revalidation, but shouldn't work - await focusOn(window) + toggleVisibility() // revalidateOnFocus won't work screen.getByText('1') unmount() @@ -175,7 +175,7 @@ describe('useSWR - cache provider', () => { expect(unsubscribeReconnectFn).toBeCalledTimes(1) }) - it('should work with revalidateOnFocus', async () => { + it('should work with revalidateOnFocus 1', async () => { const key = createKey() let value = 0 function Page() { @@ -190,6 +190,8 @@ describe('useSWR - cache provider', () => { await screen.findByText('0') await nextTick() + toggleVisibility() + await nextTick() screen.getByText('1') }) @@ -395,7 +397,7 @@ describe('useSWR - global cache', () => { await screen.findByText('data:mutated value') }) - it('should work with revalidateOnFocus', async () => { + it('should work with revalidateOnFocus 2', async () => { const key = createKey() let value = 0 function Page() { @@ -409,6 +411,8 @@ describe('useSWR - global cache', () => { await screen.findByText('0') await nextTick() + toggleVisibility() + await nextTick() screen.getByText('1') }) diff --git a/test/use-swr-focus.test.tsx b/test/use-swr-focus.test.tsx index caf2957de7..cdaaf76e41 100644 --- a/test/use-swr-focus.test.tsx +++ b/test/use-swr-focus.test.tsx @@ -4,13 +4,11 @@ import useSWR from 'swr' import { sleep, nextTick as waitForNextTick, - focusOn, + toggleVisibility, renderWithConfig, createKey } from './utils' -const focusWindow = () => focusOn(window) - describe('useSWR - focus', () => { it('should revalidate on focus by default', async () => { let value = 0 @@ -30,7 +28,7 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - + toggleVisibility() await screen.findByText('data: 1') }) @@ -55,7 +53,7 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - await focusWindow() + toggleVisibility() // should not be revalidated screen.getByText('data: 0') }) @@ -83,19 +81,20 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - await focusWindow() + toggleVisibility() // data should not change screen.getByText('data: 0') // change revalidateOnFocus to true fireEvent.click(screen.getByText('data: 0')) // trigger revalidation + toggleVisibility() // data should update await screen.findByText('data: 1') await waitForNextTick() // trigger revalidation - await focusWindow() + toggleVisibility() // data should update await screen.findByText('data: 2') @@ -103,7 +102,7 @@ describe('useSWR - focus', () => { // change revalidateOnFocus to false fireEvent.click(screen.getByText('data: 2')) // trigger revalidation - await focusWindow() + toggleVisibility() // data should not change screen.getByText('data: 2') }) @@ -130,7 +129,7 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - await focusWindow() + toggleVisibility() // still in throttling interval await act(() => sleep(20)) // should be throttled @@ -139,7 +138,7 @@ describe('useSWR - focus', () => { await act(() => sleep(100)) // trigger revalidation again - await focusWindow() + toggleVisibility() await screen.findByText('data: 2') }) @@ -166,10 +165,11 @@ describe('useSWR - focus', () => { await waitForNextTick() // trigger revalidation - await focusWindow() + toggleVisibility() // wait for throttle interval await act(() => sleep(100)) // trigger revalidation + toggleVisibility() await screen.findByText('data: 2') await waitForNextTick() @@ -178,17 +178,17 @@ describe('useSWR - focus', () => { // wait for throttle interval await act(() => sleep(100)) // trigger revalidation - await focusWindow() + toggleVisibility() // wait for throttle interval await act(() => sleep(100)) // should be throttled - await focusWindow() + toggleVisibility() await screen.findByText('data: 3') // wait for throttle interval await act(() => sleep(150)) // trigger revalidation - await focusWindow() + toggleVisibility() // wait for throttle intervals await act(() => sleep(150)) await screen.findByText('data: 4') @@ -210,6 +210,7 @@ describe('useSWR - focus', () => { screen.getByText('data:') await screen.findByText('data: 0') await waitForNextTick() + toggleVisibility() await screen.findByText('data: 1') }) @@ -228,7 +229,7 @@ describe('useSWR - focus', () => { renderWithConfig() await waitForNextTick() - fireEvent.focus(window) + toggleVisibility() fireEvent.click(screen.getByText('change key')) await waitForNextTick() diff --git a/test/use-swr-immutable.test.tsx b/test/use-swr-immutable.test.tsx index 092d5b7dd2..8fe87af790 100644 --- a/test/use-swr-immutable.test.tsx +++ b/test/use-swr-immutable.test.tsx @@ -6,12 +6,10 @@ import { sleep, createKey, nextTick as waitForNextTick, - focusOn, + toggleVisibility, renderWithConfig } from './utils' -const focusWindow = () => focusOn(window) - describe('useSWR - immutable', () => { it('should revalidate on mount', async () => { let value = 0 @@ -138,7 +136,7 @@ describe('useSWR - immutable', () => { // trigger window focus await waitForNextTick() - await focusWindow() + toggleVisibility() // wait for rerender await act(() => sleep(50)) diff --git a/test/use-swr-integration.test.tsx b/test/use-swr-integration.test.tsx index af651cc307..d2a0d8e842 100644 --- a/test/use-swr-integration.test.tsx +++ b/test/use-swr-integration.test.tsx @@ -7,7 +7,8 @@ import { nextTick as waitForNextTick, renderWithConfig, createKey, - renderWithGlobalCache + renderWithGlobalCache, + toggleVisibility } from './utils' describe('useSWR', () => { @@ -389,7 +390,7 @@ describe('useSWR', () => { await screen.findByText('hello, Initial') await waitForNextTick() - fireEvent.focus(window) + toggleVisibility() await screen.findByText(`hello, ${initialKey}`) diff --git a/test/use-swr-offline.test.tsx b/test/use-swr-offline.test.tsx index 3733bac069..889a5c5479 100644 --- a/test/use-swr-offline.test.tsx +++ b/test/use-swr-offline.test.tsx @@ -1,18 +1,13 @@ -import { act, screen } from '@testing-library/react' +import { screen } from '@testing-library/react' import useSWR from 'swr' import { nextTick as waitForNextTick, - focusOn, + toggleVisibility, createKey, - renderWithConfig + renderWithConfig, + dispatchWindowEvent } from './utils' -const focusWindow = () => focusOn(window) -const dispatchWindowEvent = event => - act(async () => { - window.dispatchEvent(new Event(event)) - }) - describe('useSWR - offline', () => { it('should not revalidate when offline', async () => { let value = 0 @@ -36,7 +31,7 @@ describe('useSWR - offline', () => { await dispatchWindowEvent('offline') // trigger focus revalidation - await focusWindow() + toggleVisibility() // should not be revalidated screen.getByText('data: 0') diff --git a/test/utils.tsx b/test/utils.tsx index 5391b6cae7..f2e3a71a22 100644 --- a/test/utils.tsx +++ b/test/utils.tsx @@ -1,4 +1,4 @@ -import { act, fireEvent, render } from '@testing-library/react' +import { act, render } from '@testing-library/react' import { SWRConfig } from 'swr' export function sleep(time: number) { @@ -21,9 +21,14 @@ export const createResponse = ( export const nextTick = () => act(() => sleep(1)) -export const focusOn = (element: any) => - act(async () => { - fireEvent.focus(element) +export const toggleVisibility = (_?: any) => + act(() => { + document.dispatchEvent(new Event('visibilitychange')) + }) + +export const dispatchWindowEvent = (event: string) => + act(() => { + window.dispatchEvent(new Event(event)) }) export const createKey = () => 'swr-key-' + ~~(Math.random() * 1e7) From f64464e61650d10935bc2d4d6d35310ace2c7915 Mon Sep 17 00:00:00 2001 From: Yixuan Xu Date: Fri, 7 Jul 2023 01:25:34 +0800 Subject: [PATCH 7/7] test: remove winodow test and keep rest same --- test/unit/web-preset.test.ts | 8 +++----- test/use-swr-focus.test.tsx | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/unit/web-preset.test.ts b/test/unit/web-preset.test.ts index 4dd0275d81..58f1227730 100644 --- a/test/unit/web-preset.test.ts +++ b/test/unit/web-preset.test.ts @@ -12,8 +12,7 @@ function createEventTarget() { function runTests(propertyName) { let initFocus - const eventName = - propertyName === 'window' && VISIBILITYCHANGE_EVENT + const eventName = VISIBILITYCHANGE_EVENT describe(`Web Preset ${propertyName}`, () => { const globalSpy = { @@ -49,11 +48,11 @@ function runTests(propertyName) { const release = initFocus(fn) as () => void target.emit(eventName) - expect(fn).toBeCalledTimes(0) + expect(fn).toBeCalledTimes(1) release() target.emit(eventName) - expect(fn).toBeCalledTimes(0) + expect(fn).toBeCalledTimes(1) }) it(`should not trigger listener when ${propertyName} is falsy`, async () => { @@ -87,5 +86,4 @@ function runTests(propertyName) { }) } -runTests('window') runTests('document') diff --git a/test/use-swr-focus.test.tsx b/test/use-swr-focus.test.tsx index cdaaf76e41..4dcec13abf 100644 --- a/test/use-swr-focus.test.tsx +++ b/test/use-swr-focus.test.tsx @@ -133,6 +133,7 @@ describe('useSWR - focus', () => { // still in throttling interval await act(() => sleep(20)) // should be throttled + toggleVisibility() await screen.findByText('data: 1') // wait for focusThrottleInterval await act(() => sleep(100))