|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { experimental_AstroContainer as AstroContainer } from 'astro/container' |
3 | 3 | import SearchBarFixture from '@components/Search/SearchBar/client/__fixtures__/index.fixture.astro' |
| 4 | +import SearchBarHeaderFixture from '@components/Search/SearchBar/client/__fixtures__/header.fixture.astro' |
4 | 5 | import type { SearchBarElement as SearchBarElementInstance } from '../index' |
5 | 6 | import type { WebComponentModule } from '@components/scripts/@types/webComponentModule' |
6 | 7 | import { executeRender } from '@test/unit/helpers/litRuntime' |
@@ -60,6 +61,28 @@ describe('SearchBar web component', () => { |
60 | 61 | }) |
61 | 62 | } |
62 | 63 |
|
| 64 | + const runHeaderComponentRender = async ( |
| 65 | + assertion: (_context: { |
| 66 | + element: SearchBarElementInstance |
| 67 | + window: Window & typeof globalThis |
| 68 | + }) => Promise<void> | void |
| 69 | + ): Promise<void> => { |
| 70 | + await executeRender<SearchBarModule>({ |
| 71 | + container, |
| 72 | + component: SearchBarHeaderFixture, |
| 73 | + moduleSpecifier: '@components/Search/SearchBar/client/index', |
| 74 | + waitForReady: async (element: SearchBarElementInstance) => { |
| 75 | + await element.updateComplete |
| 76 | + }, |
| 77 | + assert: async ({ element, window }) => { |
| 78 | + if (!window) { |
| 79 | + throw new Error('Missing JSDOM window for SearchBar test.') |
| 80 | + } |
| 81 | + await assertion({ element, window }) |
| 82 | + }, |
| 83 | + }) |
| 84 | + } |
| 85 | + |
63 | 86 | it('hides results when query is too short', async () => { |
64 | 87 | await runComponentRender(async ({ element, window }) => { |
65 | 88 | const input = element.querySelector('[data-search-input]') as HTMLInputElement |
@@ -108,4 +131,26 @@ describe('SearchBar web component', () => { |
108 | 131 |
|
109 | 132 | vi.useRealTimers() |
110 | 133 | }) |
| 134 | + |
| 135 | + it('toggles open and closes on Escape in header variant', async () => { |
| 136 | + await runHeaderComponentRender(async ({ element, window }) => { |
| 137 | + const toggleBtn = element.querySelector('[data-search-toggle]') as HTMLButtonElement |
| 138 | + const input = element.querySelector('[data-search-input]') as HTMLInputElement |
| 139 | + |
| 140 | + expect(toggleBtn.getAttribute('aria-expanded')).toBe('false') |
| 141 | + expect(input.classList.contains('hidden')).toBe(true) |
| 142 | + |
| 143 | + toggleBtn.click() |
| 144 | + await flushMicrotasks() |
| 145 | + |
| 146 | + expect(toggleBtn.getAttribute('aria-expanded')).toBe('true') |
| 147 | + expect(input.classList.contains('hidden')).toBe(false) |
| 148 | + |
| 149 | + input.dispatchEvent(new window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true })) |
| 150 | + await flushMicrotasks() |
| 151 | + |
| 152 | + expect(toggleBtn.getAttribute('aria-expanded')).toBe('false') |
| 153 | + expect(input.classList.contains('hidden')).toBe(true) |
| 154 | + }) |
| 155 | + }) |
111 | 156 | }) |
0 commit comments