1
+ import { IS_REACT_19 } from '@internal/utils/react-is.js'
1
2
import type { PayloadAction } from '@reduxjs/toolkit'
2
3
import { createSlice , createStore } from '@reduxjs/toolkit'
3
4
import * as rtl from '@testing-library/react'
@@ -111,17 +112,18 @@ describe('New v8 serverState behavior', () => {
111
112
112
113
const Spinner = ( ) => < div />
113
114
114
- if ( ! IS_REACT_18 ) {
115
- it ( 'Dummy test for React 17, ignore' , ( ) => { } )
116
- return
117
- }
118
-
119
- const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
115
+ const consoleErrorSpy = vi
116
+ . spyOn ( console , 'error' )
117
+ . mockImplementation ( ( ) => { } )
120
118
121
119
afterEach ( ( ) => {
122
120
vi . clearAllMocks ( )
123
121
} )
124
122
123
+ afterAll ( ( ) => {
124
+ vi . restoreAllMocks ( )
125
+ } )
126
+
125
127
it ( 'Handles hydration correctly' , async ( ) => {
126
128
const ssrStore = createStore ( dataSlice . reducer )
127
129
@@ -154,19 +156,35 @@ describe('New v8 serverState behavior', () => {
154
156
< Provider store = { clientStore } >
155
157
< App />
156
158
</ Provider > ,
159
+ {
160
+ onRecoverableError : IS_REACT_19
161
+ ? ( error , errorInfo ) => {
162
+ console . error ( error )
163
+ }
164
+ : undefined ,
165
+ } ,
157
166
)
158
167
} )
159
168
160
- const [ lastCall = [ ] ] = consoleError . mock . calls . slice ( - 1 )
169
+ const { lastCall = [ ] } = consoleErrorSpy . mock
161
170
const [ errorArg ] = lastCall
162
171
expect ( errorArg ) . toBeInstanceOf ( Error )
163
- expect ( / T h e r e w a s a n e r r o r w h i l e h y d r a t i n g / . test ( errorArg . message ) ) . toBe (
164
- true ,
165
- )
166
172
167
- vi . resetAllMocks ( )
173
+ if ( IS_REACT_19 ) {
174
+ expect ( consoleErrorSpy ) . toHaveBeenCalledOnce ( )
175
+
176
+ expect ( errorArg . message ) . toMatch (
177
+ / H y d r a t i o n f a i l e d b e c a u s e t h e s e r v e r r e n d e r e d H T M L d i d n ' t m a t c h t h e c l i e n t / ,
178
+ )
179
+ } else if ( IS_REACT_18 ) {
180
+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 8 )
181
+
182
+ expect ( errorArg . message ) . toMatch ( / T h e r e w a s a n e r r o r w h i l e h y d r a t i n g / )
183
+ }
184
+
185
+ vi . clearAllMocks ( )
168
186
169
- expect ( consoleError . mock . calls . length ) . toBe ( 0 )
187
+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
170
188
171
189
document . body . removeChild ( rootDiv )
172
190
@@ -187,7 +205,7 @@ describe('New v8 serverState behavior', () => {
187
205
)
188
206
} )
189
207
190
- expect ( consoleError . mock . calls . length ) . toBe ( 0 )
208
+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
191
209
192
210
// Buttons should both exist, and have the updated count due to later render
193
211
const button1 = rtl . screen . getByText ( 'useSelector:Hydrated. Count: 1' )
0 commit comments