2
2
3
3
import React from 'react' ;
4
4
import { render } from '@testing-library/react' ;
5
- import { vi } from 'vitest' ;
5
+ import { describe , it , beforeEach } from 'vitest' ;
6
+ import { Client , cacheExchange , Operation , Exchange } from '@urql/core' ;
7
+ import { delay , map , pipe } from 'wonka' ;
6
8
9
+ import { Provider } from '../context' ;
7
10
import { useQuery } from './useQuery' ;
8
11
9
- // Mock the client so we can exercise useQuery without a real Client instance
10
- vi . mock ( '../context' , async ( ) => {
11
- const { fromValue } = await vi . importActual < typeof import ( 'wonka' ) > ( 'wonka' ) ;
12
-
13
- const mock = {
14
- // Emit a synchronous result so the parent can quickly flip from loading to not loading
15
- executeQuery : vi . fn ( ( ) => fromValue ( { data : { activeFeatureFlags : [ ] } } ) ) ,
16
- } as const ;
17
-
18
- return {
19
- useClient : ( ) => mock ,
20
- } ;
21
- } ) ;
12
+ const mockExchange : Exchange = ( ) => ops$ => {
13
+ return pipe (
14
+ ops$ ,
15
+ delay ( 100 ) ,
16
+ map ( ( operation : Operation ) => ( {
17
+ operation,
18
+ data : { activeFeatureFlags : [ ] } ,
19
+ stale : false ,
20
+ hasNext : false ,
21
+ } ) )
22
+ ) ;
23
+ } ;
22
24
23
25
function useFeature ( ) {
24
26
const [ result ] = useQuery ( {
@@ -45,12 +47,26 @@ function Repro() {
45
47
return < ReproComponent /> ;
46
48
}
47
49
48
- describe ( 'useQuery cross-component update warning' , ( ) => {
49
- it ( 'does not warn when the same query is used in parent and child' , ( ) => {
50
- // Our global test setup throws when console.error is called.
51
- // If React emits "Cannot update a component ... while rendering a different component ...",
52
- // this render will throw and the test will fail, capturing the regression.
53
- render ( < Repro /> ) ;
50
+ describe ( 'component update warning' , ( ) => {
51
+ let client : Client ;
52
+
53
+ beforeEach ( ( ) => {
54
+ client = new Client ( {
55
+ url : 'https://example.com/graphql' ,
56
+ exchanges : [ cacheExchange , mockExchange ] ,
57
+ } ) ;
54
58
} ) ;
55
- } ) ;
56
59
60
+ it ( 'does not warn when the same query is used in parent and child' , async ( ) => {
61
+ const { findByText, debug } = render (
62
+ < Provider value = { client } >
63
+ < Repro />
64
+ </ Provider >
65
+ ) ;
66
+
67
+ // Wait for the component to finish loading and render the child
68
+ await findByText ( 'Check console' ) ;
69
+
70
+ debug ( ) ;
71
+ } ) ;
72
+ } ) ;
0 commit comments