|
7 | 7 | * LICENSE file in the root directory of this source tree.
|
8 | 8 | */
|
9 | 9 | import { act, render, waitFor, fireEvent } from '@testing-library/react';
|
10 |
| -import { Component } from 'react'; |
| 10 | +import { Component, FC, useEffect } from 'react'; |
11 | 11 | import { GraphiQL } from './GraphiQL';
|
12 | 12 | import type { Fetcher } from '@graphiql/toolkit';
|
13 |
| -import { ToolbarButton } from '@graphiql/react'; |
| 13 | +import { |
| 14 | + ToolbarButton, |
| 15 | + useGraphiQL, |
| 16 | + useOperationsEditorState, |
| 17 | + MonacoEditor, |
| 18 | +} from '@graphiql/react'; |
14 | 19 | import '@graphiql/react/setup-workers/vite';
|
15 | 20 |
|
16 | 21 | // The smallest possible introspection result that builds a schema.
|
@@ -155,46 +160,31 @@ describe('GraphiQL', () => {
|
155 | 160 | }); // schema
|
156 | 161 |
|
157 | 162 | describe('default query', () => {
|
158 |
| - const timeout = 8_000; |
159 |
| - it( |
160 |
| - 'defaults to the built-in default query', |
161 |
| - async () => { |
162 |
| - const { container } = render(<GraphiQL fetcher={noOpFetcher} />); |
163 |
| - |
164 |
| - await waitFor( |
165 |
| - () => { |
166 |
| - const queryEditor = container.querySelector<HTMLDivElement>( |
167 |
| - '.graphiql-editor .monaco-scrollable-element', |
168 |
| - ); |
169 |
| - expect(queryEditor).toBeVisible(); |
170 |
| - expect(queryEditor!.textContent).toBe('# Welcome to GraphiQL'); |
171 |
| - }, |
172 |
| - { timeout }, |
173 |
| - ); |
174 |
| - }, |
175 |
| - timeout, |
176 |
| - ); |
| 163 | + it('defaults to the built-in default query', async () => { |
| 164 | + const { container } = render(<GraphiQL fetcher={noOpFetcher} />); |
177 | 165 |
|
178 |
| - it( |
179 |
| - 'accepts a custom default query', |
180 |
| - async () => { |
181 |
| - const { container } = render( |
182 |
| - <GraphiQL fetcher={noOpFetcher} defaultQuery="GraphQL Party!!" />, |
| 166 | + await waitFor(() => { |
| 167 | + const queryEditor = container.querySelector<HTMLDivElement>( |
| 168 | + '.graphiql-editor .monaco-scrollable-element', |
183 | 169 | );
|
| 170 | + expect(queryEditor).toBeVisible(); |
| 171 | + expect(queryEditor!.textContent).toBe('# Welcome to GraphiQL'); |
| 172 | + }); |
| 173 | + }); |
184 | 174 |
|
185 |
| - await waitFor( |
186 |
| - () => { |
187 |
| - const queryEditor = container.querySelector<HTMLDivElement>( |
188 |
| - '.graphiql-editor .monaco-scrollable-element', |
189 |
| - ); |
190 |
| - expect(queryEditor).toBeVisible(); |
191 |
| - expect(queryEditor!.textContent).toBe('GraphQL Party!!'); |
192 |
| - }, |
193 |
| - { timeout }, |
| 175 | + it('accepts a custom default query', async () => { |
| 176 | + const { container } = render( |
| 177 | + <GraphiQL fetcher={noOpFetcher} defaultQuery="GraphQL Party!!" />, |
| 178 | + ); |
| 179 | + |
| 180 | + await waitFor(() => { |
| 181 | + const queryEditor = container.querySelector<HTMLDivElement>( |
| 182 | + '.graphiql-editor .monaco-scrollable-element', |
194 | 183 | );
|
195 |
| - }, |
196 |
| - timeout, |
197 |
| - ); |
| 184 | + expect(queryEditor).toBeVisible(); |
| 185 | + expect(queryEditor!.textContent).toBe('GraphQL Party!!'); |
| 186 | + }); |
| 187 | + }); |
198 | 188 | }); // default query
|
199 | 189 |
|
200 | 190 | // TODO: rewrite these plugin tests after plugin API has more structure
|
@@ -697,4 +687,50 @@ describe('GraphiQL', () => {
|
697 | 687 | expect(secondEl!.querySelectorAll('.graphiql-tab').length).toBe(1);
|
698 | 688 | });
|
699 | 689 | });
|
| 690 | + |
| 691 | + it('`useOperationsEditorState` hook', async () => { |
| 692 | + let hookResult: ReturnType<typeof useOperationsEditorState>; |
| 693 | + let queryEditor: MonacoEditor; |
| 694 | + |
| 695 | + const HookConsumer: FC = () => { |
| 696 | + const $hookResult = useOperationsEditorState(); |
| 697 | + const $queryEditor = useGraphiQL(state => state.queryEditor); |
| 698 | + useEffect(() => { |
| 699 | + hookResult = $hookResult; |
| 700 | + queryEditor = $queryEditor!; |
| 701 | + }, [$hookResult, $queryEditor]); |
| 702 | + return null; |
| 703 | + }; |
| 704 | + |
| 705 | + const { container } = render( |
| 706 | + <GraphiQL fetcher={noOpFetcher} initialQuery="query { hello }"> |
| 707 | + <HookConsumer /> |
| 708 | + </GraphiQL>, |
| 709 | + ); |
| 710 | + let editor: HTMLDivElement = null!; |
| 711 | + |
| 712 | + // Assert initial values |
| 713 | + await waitFor(() => { |
| 714 | + editor = container.querySelector<HTMLDivElement>( |
| 715 | + '.graphiql-editor .monaco-scrollable-element', |
| 716 | + )!; |
| 717 | + expect(editor.textContent).toBe('query { hello }'); |
| 718 | + expect(hookResult[0]).toBe('query { hello }'); |
| 719 | + }); |
| 720 | + // Assert value was changed after UI editing |
| 721 | + await waitFor(() => { |
| 722 | + const newValue = 'bar'; |
| 723 | + queryEditor.setValue(newValue); |
| 724 | + expect(editor.textContent).toBe(newValue); |
| 725 | + expect(hookResult[0]).toBe(newValue); |
| 726 | + }); |
| 727 | + |
| 728 | + // Assert using hook handler |
| 729 | + await waitFor(() => { |
| 730 | + const newValue = 'foo'; |
| 731 | + hookResult[1](newValue); |
| 732 | + expect(editor.textContent).toBe(newValue); |
| 733 | + expect(hookResult[0]).toBe(newValue); |
| 734 | + }); |
| 735 | + }); |
700 | 736 | });
|
0 commit comments