|
| 1 | +/** |
| 2 | + * Cross-link contract tests (Issue #1022). |
| 3 | + * |
| 4 | + * Validates that the reciprocal half of the two-search-box contract is in |
| 5 | + * place: a `?q=<term>` URL param landing on the home page seeds the |
| 6 | + * AppSearchBox so the user continues their search seamlessly. The forward |
| 7 | + * direction (AppSearchBox results → /docs/search/?q=...) is covered by |
| 8 | + * `appSearchMatcher.test.ts` and `AppSearchBox.test.tsx`. |
| 9 | + */ |
| 10 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 11 | + |
| 12 | +import { AppSearchBox } from '@/components/molecules/AppSearchBox'; |
| 13 | + |
| 14 | +describe('AppSearchBox URL ?q= seed (Issue #1022 reciprocal cross-link)', () => { |
| 15 | + beforeEach(() => { |
| 16 | + // Reset URL between cases so each test owns its query state. |
| 17 | + window.history.replaceState({}, '', '/'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('seeds the input when ?q= is present in the URL', () => { |
| 21 | + window.history.replaceState({}, '', '/?q=architecture'); |
| 22 | + render(<AppSearchBox audience="home" />); |
| 23 | + const input = screen.getByPlaceholderText( |
| 24 | + 'Search retailers + builders + deploy', |
| 25 | + ) as HTMLInputElement; |
| 26 | + expect(input.value).toBe('architecture'); |
| 27 | + // Auto-opens so the user sees results immediately. |
| 28 | + expect(screen.getByRole('listbox')).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not seed when ?q= is empty', () => { |
| 32 | + window.history.replaceState({}, '', '/?q='); |
| 33 | + render(<AppSearchBox audience="home" />); |
| 34 | + const input = screen.getByPlaceholderText( |
| 35 | + 'Search retailers + builders + deploy', |
| 36 | + ) as HTMLInputElement; |
| 37 | + expect(input.value).toBe(''); |
| 38 | + expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not seed when ?q= is absent', () => { |
| 42 | + window.history.replaceState({}, '', '/'); |
| 43 | + render(<AppSearchBox audience="retailer" />); |
| 44 | + const input = screen.getByPlaceholderText('Search retailer pages') as HTMLInputElement; |
| 45 | + expect(input.value).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('user typing overrides the seeded value', () => { |
| 49 | + window.history.replaceState({}, '', '/?q=initial'); |
| 50 | + render(<AppSearchBox audience="home" />); |
| 51 | + const input = screen.getByPlaceholderText( |
| 52 | + 'Search retailers + builders + deploy', |
| 53 | + ) as HTMLInputElement; |
| 54 | + expect(input.value).toBe('initial'); |
| 55 | + fireEvent.change(input, { target: { value: 'overridden' } }); |
| 56 | + expect(input.value).toBe('overridden'); |
| 57 | + }); |
| 58 | +}); |
0 commit comments