Skip to content

Commit c4ac206

Browse files
committed
Fix tests
1 parent 7e85efc commit c4ac206

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/InfoBanner.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const InfoBanner = () => {
2222
`}
2323
>
2424
<EuiBetaBadge
25+
tabIndex={-1}
2526
css={css`
2627
display: inherit;
2728
`}

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/Search/Search.test.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ describe('Search Component', () => {
318318
expect(searchStore.getState().selectedIndex).toBe(0)
319319
})
320320

321-
it('should move focus to second result on ArrowDown from input (first is already visually selected)', async () => {
321+
it('should move selection to second result on ArrowDown from input (focus stays on input)', async () => {
322322
// Arrange
323323
const user = userEvent.setup()
324324

@@ -337,9 +337,9 @@ describe('Search Component', () => {
337337

338338
await user.keyboard('{ArrowDown}')
339339

340-
// Assert - focus moved to second result (first is already visually selected)
341-
const secondResult = screen.getByText('Test Result 2').closest('a')
342-
expect(secondResult).toHaveFocus()
340+
// Assert - selection moved to second result, focus stays on input (Pattern B)
341+
expect(searchStore.getState().selectedIndex).toBe(1)
342+
expect(input).toHaveFocus()
343343
})
344344

345345
it('should move focus between results with ArrowDown/ArrowUp', async () => {
@@ -372,7 +372,7 @@ describe('Search Component', () => {
372372
expect(searchStore.getState().selectedIndex).toBe(0)
373373
})
374374

375-
it('should clear selection when ArrowUp from first item goes to input', async () => {
375+
it('should stay at first item when ArrowUp from first item (no wrap)', async () => {
376376
// Arrange
377377
const user = userEvent.setup()
378378

@@ -394,12 +394,12 @@ describe('Search Component', () => {
394394

395395
await user.keyboard('{ArrowUp}')
396396

397-
// Assert - focus goes to input, selection is cleared
398-
expect(input).toHaveFocus()
399-
expect(searchStore.getState().selectedIndex).toBe(NO_SELECTION)
397+
// Assert - stays at first item (no wrap around)
398+
expect(firstResult).toHaveFocus()
399+
expect(searchStore.getState().selectedIndex).toBe(0)
400400
})
401401

402-
it('should clear selection when ArrowDown from last item goes to button', async () => {
402+
it('should stay at last item when ArrowDown from last item (no wrap)', async () => {
403403
// Arrange
404404
const user = userEvent.setup()
405405

@@ -422,12 +422,9 @@ describe('Search Component', () => {
422422
// Try to go down from last item
423423
await user.keyboard('{ArrowDown}')
424424

425-
// Assert - focus moves to button, selection is cleared
426-
const button = screen.getByRole('button', {
427-
name: /tell me more about/i,
428-
})
429-
expect(button).toHaveFocus()
430-
expect(searchStore.getState().selectedIndex).toBe(NO_SELECTION)
425+
// Assert - stays at last item (no wrap around)
426+
expect(lastResult).toHaveFocus()
427+
expect(searchStore.getState().selectedIndex).toBe(2)
431428
})
432429

433430
it('should render isSelected prop on the selected item', async () => {

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/Search/useSearchKeyboardNavigation.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,29 @@ export const useSearchKeyboardNavigation = (
4444
? 0
4545
: Math.min(selectedIndex + 1, resultsCount - 1)
4646
setSelectedIndex(nextIndex)
47-
// Scroll into view
48-
itemRefs.current[nextIndex]?.scrollIntoView({
49-
block: 'nearest',
50-
})
47+
// Scroll into view (guard for JSDOM)
48+
const element = itemRefs.current[nextIndex]
49+
if (
50+
element &&
51+
typeof element.scrollIntoView === 'function'
52+
) {
53+
element.scrollIntoView({ block: 'nearest' })
54+
}
5155
}
5256
} else if (e.key === 'ArrowUp') {
5357
e.preventDefault()
5458
if (resultsCount > 0 && selectedIndex > 0) {
5559
// Move selection up
5660
const prevIndex = selectedIndex - 1
5761
setSelectedIndex(prevIndex)
58-
// Scroll into view
59-
itemRefs.current[prevIndex]?.scrollIntoView({
60-
block: 'nearest',
61-
})
62+
// Scroll into view (guard for JSDOM)
63+
const element = itemRefs.current[prevIndex]
64+
if (
65+
element &&
66+
typeof element.scrollIntoView === 'function'
67+
) {
68+
element.scrollIntoView({ block: 'nearest' })
69+
}
6270
}
6371
}
6472
// Tab works naturally - goes to filters, then button

0 commit comments

Comments
 (0)