@@ -256,12 +256,11 @@ describe('ReactHooksInspection', () => {
256
256
} ) ;
257
257
258
258
it ( 'should support an injected dispatcher' , ( ) => {
259
- function Foo ( props ) {
260
- const [ state ] = React . useState ( 'hello world' ) ;
261
- return < div > { state } </ div > ;
262
- }
263
-
264
- const initial = { } ;
259
+ const initial = {
260
+ useState ( ) {
261
+ throw new Error ( "Should've been proxied" ) ;
262
+ } ,
263
+ } ;
265
264
let current = initial ;
266
265
let getterCalls = 0 ;
267
266
const setterCalls = [ ] ;
@@ -276,33 +275,14 @@ describe('ReactHooksInspection', () => {
276
275
} ,
277
276
} ;
278
277
279
- let didCatch = false ;
280
- expect ( ( ) => {
281
- // mock the Error constructor to check the internal of the error instance
282
- try {
283
- ReactDebugTools . inspectHooks ( Foo , { } , FakeDispatcherRef ) ;
284
- } catch ( error ) {
285
- expect ( error . message ) . toBe ( 'Error rendering inspected component' ) ;
286
- // error.cause is the original error
287
- expect ( error . cause ) . toBeInstanceOf ( Error ) ;
288
- expect ( error . cause . message ) . toBe (
289
- "Cannot read property 'useState' of null" ,
290
- ) ;
291
- }
292
- didCatch = true ;
293
- } ) . toErrorDev (
294
- 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
295
- ' one of the following reasons:\n' +
296
- '1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
297
- '2. You might be breaking the Rules of Hooks\n' +
298
- '3. You might have more than one copy of React in the same app\n' +
299
- 'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.' ,
300
- { withoutStack : true } ,
301
- ) ;
302
- // avoid false positive if no error was thrown at all
303
- expect ( didCatch ) . toBe ( true ) ;
278
+ function Foo ( props ) {
279
+ const [ state ] = FakeDispatcherRef . current . useState ( 'hello world' ) ;
280
+ return < div > { state } </ div > ;
281
+ }
282
+
283
+ ReactDebugTools . inspectHooks ( Foo , { } , FakeDispatcherRef ) ;
304
284
305
- expect ( getterCalls ) . toBe ( 1 ) ;
285
+ expect ( getterCalls ) . toBe ( 2 ) ;
306
286
expect ( setterCalls ) . toHaveLength ( 2 ) ;
307
287
expect ( setterCalls [ 0 ] ) . not . toBe ( initial ) ;
308
288
expect ( setterCalls [ 1 ] ) . toBe ( initial ) ;
0 commit comments