|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'; |
| 4 | +import { type Mock } from 'vitest'; |
| 5 | +import { |
| 6 | + useGraphiQL as $useGraphiQL, |
| 7 | + useGraphiQLActions as $useGraphiQLActions, |
| 8 | + isMacOs, |
| 9 | +} from '@graphiql/react'; |
| 10 | +import { DOC_EXPLORER_PLUGIN, DocExplorerStore } from './context'; |
| 11 | + |
| 12 | +const useGraphiQL = $useGraphiQL as Mock; |
| 13 | +const useGraphiQLActions = $useGraphiQLActions 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 | + useGraphiQLActions: vi.fn(), |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +const schema = new GraphQLSchema({ |
| 26 | + query: new GraphQLObjectType({ |
| 27 | + name: 'Query', |
| 28 | + fields: { field: { type: GraphQLString } }, |
| 29 | + }), |
| 30 | +}); |
| 31 | + |
| 32 | +const setVisiblePlugin = vi.fn(); |
| 33 | + |
| 34 | +function setup() { |
| 35 | + useGraphiQL.mockImplementation(cb => |
| 36 | + cb({ schema, validationErrors: [], schemaReference: null }), |
| 37 | + ); |
| 38 | + useGraphiQLActions.mockReturnValue({ setVisiblePlugin }); |
| 39 | + return render( |
| 40 | + <DocExplorerStore> |
| 41 | + <div /> |
| 42 | + </DocExplorerStore>, |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +// `isMacOs` is derived from the test environment's user agent, so build the |
| 47 | +// keydown event using whichever modifier this handler actually expects. |
| 48 | +function dispatchSearchShortcut(extra: Partial<KeyboardEventInit> = {}) { |
| 49 | + const event = new KeyboardEvent('keydown', { |
| 50 | + code: 'KeyK', |
| 51 | + metaKey: isMacOs, |
| 52 | + ctrlKey: !isMacOs, |
| 53 | + bubbles: true, |
| 54 | + cancelable: true, |
| 55 | + ...extra, |
| 56 | + }); |
| 57 | + const preventDefaultSpy = vi.spyOn(event, 'preventDefault'); |
| 58 | + window.dispatchEvent(event); |
| 59 | + return { event, preventDefaultSpy }; |
| 60 | +} |
| 61 | + |
| 62 | +describe('DocExplorerStore keyboard shortcut', () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.clearAllMocks(); |
| 65 | + document.body.innerHTML = ''; |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + document.body.innerHTML = ''; |
| 70 | + }); |
| 71 | + |
| 72 | + it('opens the doc explorer on plain Cmd/Ctrl+K, without requiring Alt', () => { |
| 73 | + setup(); |
| 74 | + |
| 75 | + dispatchSearchShortcut(); |
| 76 | + |
| 77 | + expect(setVisiblePlugin).toHaveBeenCalledWith(DOC_EXPLORER_PLUGIN); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not open the doc explorer for Alt+Cmd/Ctrl+K (old binding)', () => { |
| 81 | + setup(); |
| 82 | + |
| 83 | + dispatchSearchShortcut({ altKey: true }); |
| 84 | + |
| 85 | + // The old binding required Alt; the new one doesn't care either way, |
| 86 | + // so this should still fire. Guard against a regression back to |
| 87 | + // requiring Alt by asserting it fires with Alt held too. |
| 88 | + expect(setVisiblePlugin).toHaveBeenCalledWith(DOC_EXPLORER_PLUGIN); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does nothing when the modifier key is missing', () => { |
| 92 | + setup(); |
| 93 | + |
| 94 | + dispatchSearchShortcut({ metaKey: false, ctrlKey: false }); |
| 95 | + |
| 96 | + expect(setVisiblePlugin).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('focuses the search input after opening via Cmd/Ctrl+K', async () => { |
| 100 | + setup(); |
| 101 | + |
| 102 | + const searchInput = document.createElement('div'); |
| 103 | + searchInput.className = 'graphiql-doc-explorer-search-row-input'; |
| 104 | + const clickSpy = vi.fn(); |
| 105 | + searchInput.addEventListener('click', clickSpy); |
| 106 | + document.body.append(searchInput); |
| 107 | + |
| 108 | + dispatchSearchShortcut(); |
| 109 | + |
| 110 | + await vi.waitFor(() => { |
| 111 | + expect(clickSpy).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('prevents default so Monaco does not swallow the shortcut even with an editor focused', () => { |
| 116 | + setup(); |
| 117 | + |
| 118 | + const { preventDefaultSpy } = dispatchSearchShortcut(); |
| 119 | + |
| 120 | + expect(preventDefaultSpy).toHaveBeenCalled(); |
| 121 | + expect(setVisiblePlugin).toHaveBeenCalledWith(DOC_EXPLORER_PLUGIN); |
| 122 | + }); |
| 123 | +}); |
0 commit comments