1
- import { act , render } from '@testing-library/react' ;
1
+ import { Mock } from 'vitest' ;
2
+ import { useGraphiQL as $useGraphiQL } from '@graphiql/react' ;
3
+ import { render } from '@testing-library/react' ;
2
4
import { GraphQLInt , GraphQLObjectType , GraphQLSchema } from 'graphql' ;
3
5
import { FC , useEffect } from 'react' ;
4
6
import {
@@ -7,7 +9,17 @@ import {
7
9
useDocExplorerActions ,
8
10
} from '../../context' ;
9
11
import { DocExplorer } from '../doc-explorer' ;
10
- import { schemaStore } from '../../../../graphiql-react/dist/stores/schema' ;
12
+
13
+ const useGraphiQL = $useGraphiQL as Mock ;
14
+
15
+ vi . mock ( '@graphiql/react' , async ( ) => {
16
+ const originalModule =
17
+ await vi . importActual < typeof import ( '@graphiql/react' ) > ( '@graphiql/react' ) ;
18
+ return {
19
+ ...originalModule ,
20
+ useGraphiQL : vi . fn ( ) ,
21
+ } ;
22
+ } ) ;
11
23
12
24
function makeSchema ( fieldName = 'field' ) {
13
25
return new GraphQLSchema ( {
@@ -29,15 +41,16 @@ function makeSchema(fieldName = 'field') {
29
41
}
30
42
31
43
const defaultSchemaContext = {
32
- ...schemaStore . getInitialState ( ) ,
33
- async introspect ( ) { } ,
44
+ fetchError : null ,
45
+ introspect ( ) { } ,
46
+ isFetching : false ,
34
47
schema : makeSchema ( ) ,
48
+ validationErrors : [ ] ,
35
49
} ;
36
50
37
51
const withErrorSchemaContext = {
38
- ...schemaStore . getInitialState ( ) ,
52
+ ...defaultSchemaContext ,
39
53
fetchError : 'Error fetching schema' ,
40
- async introspect ( ) { } ,
41
54
schema : new GraphQLSchema ( { description : 'GraphQL Schema for testing' } ) ,
42
55
} ;
43
56
@@ -50,21 +63,29 @@ const DocExplorerWithContext: FC = () => {
50
63
} ;
51
64
52
65
describe ( 'DocExplorer' , ( ) => {
66
+ beforeEach ( ( ) => {
67
+ vi . resetModules ( ) ;
68
+ } ) ;
69
+
53
70
it ( 'renders spinner when the schema is loading' , ( ) => {
54
- schemaStore . setState ( { isFetching : true } ) ;
71
+ useGraphiQL . mockImplementation ( cb =>
72
+ cb ( { ...defaultSchemaContext , isFetching : true } ) ,
73
+ ) ;
55
74
const { container } = render ( < DocExplorerWithContext /> ) ;
56
75
const spinner = container . querySelectorAll ( '.graphiql-spinner' ) ;
57
76
expect ( spinner ) . toHaveLength ( 1 ) ;
58
77
} ) ;
59
78
it ( 'renders with null schema' , ( ) => {
60
- schemaStore . setState ( { ...defaultSchemaContext , schema : null } ) ;
79
+ useGraphiQL . mockImplementation ( cb =>
80
+ cb ( { ...defaultSchemaContext , schema : null } ) ,
81
+ ) ;
61
82
const { container } = render ( < DocExplorerWithContext /> ) ;
62
83
const error = container . querySelectorAll ( '.graphiql-doc-explorer-error' ) ;
63
84
expect ( error ) . toHaveLength ( 1 ) ;
64
85
expect ( error [ 0 ] ) . toHaveTextContent ( 'No GraphQL schema available' ) ;
65
86
} ) ;
66
87
it ( 'renders with schema' , ( ) => {
67
- schemaStore . setState ( defaultSchemaContext ) ;
88
+ useGraphiQL . mockImplementation ( cb => cb ( defaultSchemaContext ) ) ;
68
89
const { container } = render ( < DocExplorerWithContext /> ) ;
69
90
const error = container . querySelectorAll ( '.graphiql-doc-explorer-error' ) ;
70
91
expect ( error ) . toHaveLength ( 0 ) ;
@@ -73,14 +94,11 @@ describe('DocExplorer', () => {
73
94
) . toHaveTextContent ( 'GraphQL Schema for testing' ) ;
74
95
} ) ;
75
96
it ( 'renders correctly with schema error' , ( ) => {
76
- schemaStore . setState ( withErrorSchemaContext ) ;
97
+ useGraphiQL . mockImplementation ( cb => cb ( withErrorSchemaContext ) ) ;
77
98
const { rerender, container } = render ( < DocExplorerWithContext /> ) ;
78
99
const error = container . querySelector ( '.graphiql-doc-explorer-error' ) ;
79
100
expect ( error ) . toHaveTextContent ( 'Error fetching schema' ) ;
80
-
81
- act ( ( ) => {
82
- schemaStore . setState ( defaultSchemaContext ) ;
83
- } ) ;
101
+ useGraphiQL . mockImplementation ( cb => cb ( defaultSchemaContext ) ) ;
84
102
rerender ( < DocExplorerWithContext /> ) ;
85
103
const errors = container . querySelectorAll ( '.graphiql-doc-explorer-error' ) ;
86
104
expect ( errors ) . toHaveLength ( 0 ) ;
@@ -104,45 +122,38 @@ describe('DocExplorer', () => {
104
122
} ;
105
123
106
124
// Initial render, set initial state
107
- schemaStore . setState ( {
108
- ...defaultSchemaContext ,
109
- schema : initialSchema ,
110
- } ) ;
125
+ useGraphiQL . mockImplementation ( cb =>
126
+ cb ( { ...defaultSchemaContext , schema : initialSchema } ) ,
127
+ ) ;
111
128
const { container, rerender } = render (
112
129
< DocExplorerStore >
113
130
< SetInitialStack />
114
131
</ DocExplorerStore > ,
115
132
) ;
116
133
117
134
// First proper render of doc explorer
118
- act ( ( ) => {
119
- schemaStore . setState ( {
120
- ...defaultSchemaContext ,
121
- schema : initialSchema ,
122
- } ) ;
123
- } ) ;
124
135
rerender (
125
136
< DocExplorerStore >
126
137
< DocExplorer />
127
138
</ DocExplorerStore > ,
128
139
) ;
129
140
130
- const [ title ] = container . querySelectorAll ( '.graphiql-doc-explorer-title' ) ;
141
+ const title = container . querySelector ( '.graphiql-doc-explorer-title' ) ! ;
131
142
expect ( title . textContent ) . toEqual ( 'field' ) ;
132
143
133
144
// Second render of doc explorer, this time with a new schema, with _same_ field name
134
- act ( ( ) => {
135
- schemaStore . setState ( {
145
+ useGraphiQL . mockImplementation ( cb =>
146
+ cb ( {
136
147
...defaultSchemaContext ,
137
148
schema : makeSchema ( ) , // <<< New, but equivalent, schema
138
- } ) ;
139
- } ) ;
149
+ } ) ,
150
+ ) ;
140
151
rerender (
141
152
< DocExplorerStore >
142
153
< DocExplorer />
143
154
</ DocExplorerStore > ,
144
155
) ;
145
- const [ title2 ] = container . querySelectorAll ( '.graphiql-doc-explorer-title' ) ;
156
+ const title2 = container . querySelector ( '.graphiql-doc-explorer-title' ) ! ;
146
157
// Because `Query.field` still exists in the new schema, we can still render it
147
158
expect ( title2 . textContent ) . toEqual ( 'field' ) ;
148
159
} ) ;
@@ -166,23 +177,16 @@ describe('DocExplorer', () => {
166
177
} ;
167
178
168
179
// Initial render, set initial state
169
- schemaStore . setState ( {
170
- ...defaultSchemaContext ,
171
- schema : initialSchema ,
172
- } ) ;
180
+ useGraphiQL . mockImplementation ( cb =>
181
+ cb ( { ...defaultSchemaContext , schema : initialSchema } ) ,
182
+ ) ;
173
183
const { container, rerender } = render (
174
184
< DocExplorerStore >
175
185
< SetInitialStack />
176
186
</ DocExplorerStore > ,
177
187
) ;
178
188
179
189
// First proper render of doc explorer
180
- act ( ( ) => {
181
- schemaStore . setState ( {
182
- ...defaultSchemaContext ,
183
- schema : initialSchema ,
184
- } ) ;
185
- } ) ;
186
190
rerender (
187
191
< DocExplorerStore >
188
192
< DocExplorer />
@@ -193,12 +197,12 @@ describe('DocExplorer', () => {
193
197
expect ( title . textContent ) . toEqual ( 'field' ) ;
194
198
195
199
// Second render of doc explorer, this time with a new schema, with a different field name
196
- act ( ( ) => {
197
- schemaStore . setState ( {
200
+ useGraphiQL . mockImplementation ( cb =>
201
+ cb ( {
198
202
...defaultSchemaContext ,
199
203
schema : makeSchema ( 'field2' ) , // <<< New schema with a new field name
200
- } ) ;
201
- } ) ;
204
+ } ) ,
205
+ ) ;
202
206
rerender (
203
207
< DocExplorerStore >
204
208
< DocExplorer />
0 commit comments