|
| 1 | +import { render, act, fireEvent, screen, cleanup } from '@testing-library/react'; |
| 2 | +import React, { type JSX } from 'react'; |
| 3 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 4 | + |
| 5 | +import '@testing-library/jest-dom/vitest'; |
| 6 | + |
| 7 | +import { DocSearch as DocSearchComponent } from '../DocSearch'; |
| 8 | +import type { DocSearchProps } from '../DocSearch'; |
| 9 | + |
| 10 | +function DocSearch(props: Partial<DocSearchProps>): JSX.Element { |
| 11 | + return <DocSearchComponent appId="woo" apiKey="foo" indexName="bar" {...props} />; |
| 12 | +} |
| 13 | + |
| 14 | +describe('keyboard shortcuts', () => { |
| 15 | + afterEach(() => { |
| 16 | + cleanup(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('default behavior', () => { |
| 20 | + it('shows Ctrl/Cmd+K shortcut hint by default', () => { |
| 21 | + render(<DocSearch />); |
| 22 | + |
| 23 | + const button = document.querySelector('.DocSearch-Button'); |
| 24 | + expect(button).toBeInTheDocument(); |
| 25 | + expect(button?.getAttribute('aria-label')).toMatch(/\(Control\+k\)/); |
| 26 | + expect(document.querySelector('.DocSearch-Button-Keys')).toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('responds to Ctrl+K keyboard shortcut by default', () => { |
| 30 | + render(<DocSearch />); |
| 31 | + |
| 32 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 33 | + |
| 34 | + act(() => { |
| 35 | + fireEvent.keyDown(document, { key: 'k', ctrlKey: true }); |
| 36 | + }); |
| 37 | + |
| 38 | + expect(document.querySelector('.DocSearch-Modal')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('responds to / keyboard shortcut by default', () => { |
| 42 | + render(<DocSearch />); |
| 43 | + |
| 44 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 45 | + |
| 46 | + act(() => { |
| 47 | + fireEvent.keyDown(document, { key: '/' }); |
| 48 | + }); |
| 49 | + |
| 50 | + expect(document.querySelector('.DocSearch-Modal')).toBeInTheDocument(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('custom keyboard shortcuts configuration', () => { |
| 55 | + it('hides shortcut hint when Ctrl/Cmd+K is disabled', () => { |
| 56 | + render(<DocSearch keyboardShortcuts={{ 'Ctrl/Cmd+K': false }} />); |
| 57 | + |
| 58 | + const button = document.querySelector('.DocSearch-Button'); |
| 59 | + expect(button).toBeInTheDocument(); |
| 60 | + expect(button?.getAttribute('aria-label')).toBe('Search'); |
| 61 | + expect(document.querySelector('.DocSearch-Button-Keys')).toBeInTheDocument(); |
| 62 | + expect(document.querySelector('.DocSearch-Button-Key')).not.toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('does not respond to Ctrl+K when disabled', () => { |
| 66 | + render(<DocSearch keyboardShortcuts={{ 'Ctrl/Cmd+K': false }} />); |
| 67 | + |
| 68 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + fireEvent.keyDown(document, { key: 'k', ctrlKey: true }); |
| 72 | + }); |
| 73 | + |
| 74 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not respond to / when disabled', () => { |
| 78 | + render(<DocSearch keyboardShortcuts={{ '/': false }} />); |
| 79 | + |
| 80 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 81 | + |
| 82 | + act(() => { |
| 83 | + fireEvent.keyDown(document, { key: '/' }); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('still shows shortcut hint when only / is disabled', () => { |
| 90 | + render(<DocSearch keyboardShortcuts={{ '/': false }} />); |
| 91 | + |
| 92 | + const button = document.querySelector('.DocSearch-Button'); |
| 93 | + expect(button).toBeInTheDocument(); |
| 94 | + expect(button?.getAttribute('aria-label')).toMatch(/\(Control\+k\)/); |
| 95 | + expect(document.querySelector('.DocSearch-Button-Key')).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('responds to enabled shortcuts when others are disabled', () => { |
| 99 | + render(<DocSearch keyboardShortcuts={{ '/': false }} />); |
| 100 | + |
| 101 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 102 | + |
| 103 | + act(() => { |
| 104 | + fireEvent.keyDown(document, { key: 'k', ctrlKey: true }); |
| 105 | + }); |
| 106 | + |
| 107 | + expect(document.querySelector('.DocSearch-Modal')).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('can disable all shortcuts', () => { |
| 111 | + render(<DocSearch keyboardShortcuts={{ 'Ctrl/Cmd+K': false, '/': false }} />); |
| 112 | + |
| 113 | + const button = document.querySelector('.DocSearch-Button'); |
| 114 | + expect(button?.getAttribute('aria-label')).toBe('Search'); |
| 115 | + expect(document.querySelector('.DocSearch-Button-Key')).not.toBeInTheDocument(); |
| 116 | + |
| 117 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 118 | + |
| 119 | + act(() => { |
| 120 | + fireEvent.keyDown(document, { key: 'k', ctrlKey: true }); |
| 121 | + }); |
| 122 | + |
| 123 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 124 | + |
| 125 | + act(() => { |
| 126 | + fireEvent.keyDown(document, { key: '/' }); |
| 127 | + }); |
| 128 | + |
| 129 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('Escape key behavior', () => { |
| 134 | + it('always responds to Escape to close modal regardless of configuration', async () => { |
| 135 | + render(<DocSearch keyboardShortcuts={{ 'Ctrl/Cmd+K': false, '/': false }} />); |
| 136 | + |
| 137 | + // Open modal via button click |
| 138 | + await act(async () => { |
| 139 | + fireEvent.click(await screen.findByText('Search')); |
| 140 | + }); |
| 141 | + |
| 142 | + expect(document.querySelector('.DocSearch-Modal')).toBeInTheDocument(); |
| 143 | + |
| 144 | + // Close with Escape |
| 145 | + act(() => { |
| 146 | + fireEvent.keyDown(document, { code: 'Escape' }); |
| 147 | + }); |
| 148 | + |
| 149 | + expect(document.querySelector('.DocSearch-Modal')).not.toBeInTheDocument(); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments