@@ -2,7 +2,7 @@ import { act, render, screen, fireEvent } from '@testing-library/react'
2
2
import React , { useEffect , useState } from 'react'
3
3
import useSWR , { mutate , createCache , SWRConfig } from 'swr'
4
4
import { serialize } from '../src/utils/serialize'
5
- import { createResponse , sleep , nextTick as waitForNextTick } from './utils'
5
+ import { createResponse , sleep , nextTick } from './utils'
6
6
7
7
describe ( 'useSWR - local mutation' , ( ) => {
8
8
it ( 'should trigger revalidation programmatically' , async ( ) => {
@@ -29,6 +29,33 @@ describe('useSWR - local mutation', () => {
29
29
await screen . findByText ( 'data: 1' )
30
30
} )
31
31
32
+ it ( 'should share local state when no fetcher is specified' , async ( ) => {
33
+ const useSharedState = ( key , initialData ) => {
34
+ const { data : state , mutate : setState } = useSWR ( key , { initialData } )
35
+ return [ state , setState ]
36
+ }
37
+
38
+ function Page ( ) {
39
+ const [ name , setName ] = useSharedState ( 'name' , 'huozhi' )
40
+ const [ job , setJob ] = useSharedState ( 'job' , 'gardener' )
41
+
42
+ return (
43
+ < span
44
+ onClick = { ( ) => {
45
+ setName ( '@huozhi' )
46
+ setJob ( 'chef' )
47
+ } }
48
+ >
49
+ { name } :{ job }
50
+ </ span >
51
+ )
52
+ }
53
+ render ( < Page /> )
54
+ const root = screen . getByText ( 'huozhi:gardener' )
55
+ fireEvent . click ( root )
56
+ await screen . findByText ( '@huozhi:chef' )
57
+ } )
58
+
32
59
it ( 'should trigger revalidation programmatically within a dedupingInterval' , async ( ) => {
33
60
let value = 0
34
61
@@ -144,7 +171,7 @@ describe('useSWR - local mutation', () => {
144
171
//mount
145
172
await screen . findByText ( 'data: 0' )
146
173
147
- await waitForNextTick ( )
174
+ await nextTick ( )
148
175
await act ( ( ) => {
149
176
// mutate and revalidate
150
177
return mutate ( 'mutate-promise' , createResponse ( 999 ) , false )
@@ -167,7 +194,7 @@ describe('useSWR - local mutation', () => {
167
194
//mount
168
195
await screen . findByText ( 'data: 0' )
169
196
170
- await waitForNextTick ( )
197
+ await nextTick ( )
171
198
await act ( ( ) => {
172
199
// mutate and revalidate
173
200
return mutate ( 'mutate-async-fn' , async ( ) => createResponse ( 999 ) , false )
0 commit comments