From 0b048f3dd5415bdd7ab61cf4eab5ea3b43003bd8 Mon Sep 17 00:00:00 2001
From: Yixuan Xu
Date: Sat, 22 Jul 2023 15:25:11 +0800
Subject: [PATCH 1/3] aggregate stale test into single group
---
test/old-tests.test.tsx | 178 +++++++++++++++++++++
test/use-swr-concurrent-rendering.test.tsx | 61 +------
test/use-swr-error.test.tsx | 78 ---------
test/use-swr-local-mutation.test.tsx | 36 -----
test/use-swr-streaming-ssr.test.tsx | 85 ----------
5 files changed, 179 insertions(+), 259 deletions(-)
create mode 100644 test/old-tests.test.tsx
delete mode 100644 test/use-swr-streaming-ssr.test.tsx
diff --git a/test/old-tests.test.tsx b/test/old-tests.test.tsx
new file mode 100644
index 000000000..ed9c94c8e
--- /dev/null
+++ b/test/old-tests.test.tsx
@@ -0,0 +1,178 @@
+import React, { useEffect, useState } from 'react'
+import {
+ createKey,
+ createResponse,
+ executeWithoutBatching,
+ renderWithConfig,
+ sleep
+} from './utils'
+import useSWR from 'swr'
+import { act, fireEvent, screen } from '@testing-library/react'
+
+describe('old tests - only kept as reference', () => {
+ // https://codesandbox.io/s/concurrent-swr-case-ii-lr6x4u
+ it.skip('should do state updates in transitions', async () => {
+ const key1 = createKey()
+ const key2 = createKey()
+
+ const log = []
+
+ function Counter() {
+ const [count, setCount] = React.useState(0)
+
+ React.useEffect(() => {
+ const interval = setInterval(() => {
+ setCount(x => x + 1)
+ }, 20)
+ return () => clearInterval(interval)
+ }, [])
+
+ log.push(count)
+
+ return <>{count}>
+ }
+
+ function Body() {
+ useSWR(key2, () => createResponse(true, { delay: 1000 }), {
+ revalidateOnFocus: false,
+ revalidateOnReconnect: false,
+ dedupingInterval: 0,
+ suspense: true
+ })
+ return null
+ }
+
+ function Page() {
+ const { data } = useSWR(key1, () => createResponse(true, { delay: 50 }), {
+ revalidateOnFocus: false,
+ revalidateOnReconnect: false,
+ dedupingInterval: 0
+ })
+
+ return (
+ <>
+
+ {data ? : null}
+ >
+ )
+ }
+
+ await executeWithoutBatching(async () => {
+ renderWithConfig()
+ await sleep(500)
+ })
+ })
+
+ it.skip('should not trigger the onLoadingSlow and onSuccess event after component unmount', async () => {
+ const key = createKey()
+ let loadingSlow = null,
+ success = null
+ function Page() {
+ const { data } = useSWR(key, () => createResponse('SWR'), {
+ onLoadingSlow: loadingKey => {
+ loadingSlow = loadingKey
+ },
+ onSuccess: (_, successKey) => {
+ success = successKey
+ },
+ loadingTimeout: 100
+ })
+ return
hello, {data}
+ }
+
+ function App() {
+ const [on, toggle] = useState(true)
+ return (
+ toggle(s => !s)}>
+ {on &&
}
+
+ )
+ }
+
+ renderWithConfig()
+ screen.getByText('hello,')
+ expect(loadingSlow).toEqual(null)
+ expect(success).toEqual(null)
+
+ fireEvent.click(screen.getByText('hello,'))
+ await act(() => sleep(200))
+ expect(success).toEqual(null)
+ expect(loadingSlow).toEqual(null)
+ })
+
+ it.skip('should not trigger the onError and onErrorRetry event after component unmount', async () => {
+ const key = createKey()
+ let retry = null,
+ failed = null
+ function Page() {
+ const { data } = useSWR(key, () => createResponse(new Error('error!')), {
+ onError: (_, errorKey) => {
+ failed = errorKey
+ },
+ onErrorRetry: (_, errorKey) => {
+ retry = errorKey
+ },
+ dedupingInterval: 0
+ })
+ return (
+
+ <>hello, {data}>
+
+ )
+ }
+
+ function App() {
+ const [on, toggle] = useState(true)
+ return (
+ toggle(s => !s)}>
+ {on &&
}
+
+ )
+ }
+
+ renderWithConfig()
+ screen.getByText('hello,')
+ expect(retry).toEqual(null)
+ expect(failed).toEqual(null)
+
+ fireEvent.click(screen.getByText('hello,'))
+ await act(() => sleep(200))
+ expect(retry).toEqual(null)
+ expect(failed).toEqual(null)
+ })
+ // https://github.com/vercel/swr/pull/1003
+ it.skip('should not dedupe synchronous mutations', async () => {
+ const mutationRecivedValues = []
+ const renderRecivedValues = []
+
+ const key = createKey()
+ function Component() {
+ const { data, mutate: boundMutate } = useSWR(key, () => 0)
+
+ useEffect(() => {
+ setTimeout(() => {
+ // let's mutate twice, synchronously
+ boundMutate(v => {
+ mutationRecivedValues.push(v) // should be 0
+ return 1
+ }, false)
+ boundMutate(v => {
+ mutationRecivedValues.push(v) // should be 1
+ return 2
+ }, false)
+ }, 1)
+ // the mutate function is guaranteed to be the same reference
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [])
+
+ renderRecivedValues.push(data) // should be 0 -> 2, never render 1 in between
+ return null
+ }
+
+ renderWithConfig()
+
+ await executeWithoutBatching(() => sleep(50))
+ expect(mutationRecivedValues).toEqual([0, 1])
+ expect(renderRecivedValues).toEqual([undefined, 0, 1, 2])
+ })
+})
diff --git a/test/use-swr-concurrent-rendering.test.tsx b/test/use-swr-concurrent-rendering.test.tsx
index 30ecf76a1..52c99c78f 100644
--- a/test/use-swr-concurrent-rendering.test.tsx
+++ b/test/use-swr-concurrent-rendering.test.tsx
@@ -1,11 +1,5 @@
import { screen, fireEvent, act } from '@testing-library/react'
-import {
- createKey,
- createResponse,
- sleep,
- executeWithoutBatching,
- renderWithConfig
-} from './utils'
+import { createKey, createResponse, sleep, renderWithConfig } from './utils'
import React from 'react'
import useSWR from 'swr'
@@ -69,57 +63,4 @@ describe('useSWR - concurrent rendering', () => {
await act(() => sleep(120))
screen.getByText(`isPending:0,data:${newKey}`)
})
-
- // https://codesandbox.io/s/concurrent-swr-case-ii-lr6x4u
- it.skip('should do state updates in transitions', async () => {
- const key1 = createKey()
- const key2 = createKey()
-
- const log = []
-
- function Counter() {
- const [count, setCount] = React.useState(0)
-
- React.useEffect(() => {
- const interval = setInterval(() => {
- setCount(x => x + 1)
- }, 20)
- return () => clearInterval(interval)
- }, [])
-
- log.push(count)
-
- return <>{count}>
- }
-
- function Body() {
- useSWR(key2, () => createResponse(true, { delay: 1000 }), {
- revalidateOnFocus: false,
- revalidateOnReconnect: false,
- dedupingInterval: 0,
- suspense: true
- })
- return null
- }
-
- function Page() {
- const { data } = useSWR(key1, () => createResponse(true, { delay: 50 }), {
- revalidateOnFocus: false,
- revalidateOnReconnect: false,
- dedupingInterval: 0
- })
-
- return (
- <>
-
- {data ?