1
- import type { Mock } from 'vitest' ;
2
1
import { fireEvent , render } from '@testing-library/react' ;
3
2
import type { ComponentProps } from 'react' ;
4
3
import { formatQuery , HistoryItem } from '../components' ;
5
4
import { HistoryContextProvider } from '../context' ;
6
- import {
7
- useEditorStore ,
8
- Tooltip ,
9
- StorageContextProvider ,
10
- } from '@graphiql/react' ;
11
-
12
- vi . mock ( '@graphiql/react' , async ( ) => {
13
- const originalModule = await vi . importActual ( '@graphiql/react' ) ;
14
- const mockedSetQueryEditor = vi . fn ( ) ;
15
- const mockedSetVariableEditor = vi . fn ( ) ;
16
- const mockedSetHeaderEditor = vi . fn ( ) ;
17
- return {
18
- ...originalModule ,
19
- useEditorStore ( ) {
20
- return {
21
- queryEditor : { setValue : mockedSetQueryEditor } ,
22
- variableEditor : { setValue : mockedSetVariableEditor } ,
23
- headerEditor : { setValue : mockedSetHeaderEditor } ,
24
- tabs : [ ] ,
25
- } ;
26
- } ,
27
- useExecutionStore ( ) {
28
- return { } ;
29
- } ,
30
- } ;
31
- } ) ;
5
+ import { Tooltip , GraphiQLProvider } from '@graphiql/react' ;
6
+ import { editorStore } from '../../../graphiql-react/dist/editor/context' ;
32
7
33
8
const mockQuery = /* GraphQL */ `
34
9
query Test($string: String) {
@@ -49,11 +24,11 @@ type QueryHistoryItemProps = ComponentProps<typeof HistoryItem>;
49
24
const QueryHistoryItemWithContext : typeof HistoryItem = props => {
50
25
return (
51
26
< Tooltip . Provider >
52
- < StorageContextProvider >
27
+ < GraphiQLProvider fetcher = { vi . fn ( ) } >
53
28
< HistoryContextProvider >
54
29
< HistoryItem { ...props } />
55
30
</ HistoryContextProvider >
56
- </ StorageContextProvider >
31
+ </ GraphiQLProvider >
57
32
</ Tooltip . Provider >
58
33
) ;
59
34
} ;
@@ -78,15 +53,6 @@ function getMockProps(
78
53
}
79
54
80
55
describe ( 'QueryHistoryItem' , ( ) => {
81
- const store = useEditorStore ( ) ;
82
- const mockedSetQueryEditor = store . queryEditor ! . setValue as Mock ;
83
- const mockedSetVariableEditor = store . variableEditor ! . setValue as Mock ;
84
- const mockedSetHeaderEditor = store . headerEditor ! . setValue as Mock ;
85
- beforeEach ( ( ) => {
86
- mockedSetQueryEditor . mockClear ( ) ;
87
- mockedSetVariableEditor . mockClear ( ) ;
88
- mockedSetHeaderEditor . mockClear ( ) ;
89
- } ) ;
90
56
it ( 'renders operationName if label is not provided' , ( ) => {
91
57
const otherMockProps = { item : { operationName : mockOperationName } } ;
92
58
const props = getMockProps ( otherMockProps ) ;
@@ -108,6 +74,29 @@ describe('QueryHistoryItem', () => {
108
74
} ) ;
109
75
110
76
it ( 'selects the item when history label button is clicked' , ( ) => {
77
+ const mockedSetQueryEditor = vi . fn ( ) ;
78
+ const mockedSetVariableEditor = vi . fn ( ) ;
79
+ const mockedSetHeaderEditor = vi . fn ( ) ;
80
+ type MonacoEditorWithOperationFacts = NonNullable <
81
+ ReturnType < typeof editorStore . getInitialState > [ 'queryEditor' ]
82
+ > ;
83
+ type MonacoEditor = NonNullable <
84
+ ReturnType < typeof editorStore . getInitialState > [ 'variableEditor' ]
85
+ > ;
86
+
87
+ editorStore . setState ( {
88
+ queryEditor : {
89
+ setValue : mockedSetQueryEditor ,
90
+ } as unknown as MonacoEditorWithOperationFacts ,
91
+ variableEditor : {
92
+ setValue : mockedSetVariableEditor ,
93
+ } as unknown as MonacoEditor ,
94
+ headerEditor : {
95
+ setValue : mockedSetHeaderEditor ,
96
+ getValue : ( ) => '' ,
97
+ } as unknown as MonacoEditor ,
98
+ } ) ;
99
+
111
100
const otherMockProps = { item : { operationName : mockOperationName } } ;
112
101
const mockProps = getMockProps ( otherMockProps ) ;
113
102
const { container } = render (
0 commit comments