Skip to content

Commit 73eb53b

Browse files
authored
fix(www): tweak docsearch to init algolia when tabbed into (#23040)
* fix(www): tweak docsearch to init algolia when tabbed into * test(www): create test for seach-form
1 parent 645a5b5 commit 73eb53b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from "react"
2+
import { render, waitFor, fireEvent } from "@testing-library/react"
3+
import { ThemeProvider } from "theme-ui"
4+
import { I18nProvider } from "../../I18nContext"
5+
6+
import theme from "../../../gatsby-plugin-theme-ui"
7+
import SearchForm from "../search-form"
8+
9+
test(`it initializes algola docsearch on form click, and keeps focus`, async () => {
10+
const { container } = render(
11+
<I18nProvider>
12+
<ThemeProvider theme={theme}>
13+
<SearchForm />
14+
</ThemeProvider>
15+
</I18nProvider>
16+
)
17+
18+
const input = container.querySelector(`#doc-search`)
19+
20+
// triggering docsearch initialization
21+
fireEvent.click(input)
22+
23+
// docseach is lazy loaded, so we have to wait for it
24+
await waitFor(() => expect(window.docsearch).not.toBeUndefined())
25+
26+
// checking if docsearch has been initialized, we can confirm it if html
27+
// with class `ds-dropdown-menu` is injected
28+
expect(container.querySelector(".ds-dropdown-menu")).toBeInTheDocument()
29+
30+
// checking that input still has a focus
31+
expect(document.activeElement).toEqual(input)
32+
})
33+
34+
test(`it initializes algola docsearch on form mouse hover, and keeps focus`, async () => {
35+
const { container, getByRole } = render(
36+
<I18nProvider>
37+
<ThemeProvider theme={theme}>
38+
<SearchForm />
39+
</ThemeProvider>
40+
</I18nProvider>
41+
)
42+
43+
const input = container.querySelector(`#doc-search`)
44+
45+
// triggering docsearch initialization
46+
fireEvent.mouseOver(input)
47+
48+
// docseach is lazy loaded, so we have to wait for it
49+
await waitFor(() => expect(window.docsearch).not.toBeUndefined())
50+
51+
// checking if docsearch has been initialized, we can confirm it if html
52+
// with class `ds-dropdown-menu` is injected
53+
expect(container.querySelector(".ds-dropdown-menu")).toBeInTheDocument()
54+
55+
// checking that input still has a focus
56+
expect(document.activeElement).toEqual(input)
57+
})
58+
59+
test(`it initializes algola docsearch on form focus, and keeps focus`, async () => {
60+
const { container, getByRole } = render(
61+
<I18nProvider>
62+
<ThemeProvider theme={theme}>
63+
<SearchForm />
64+
</ThemeProvider>
65+
</I18nProvider>
66+
)
67+
68+
const input = container.querySelector(`#doc-search`)
69+
70+
// triggering docsearch initialization
71+
input.focus()
72+
73+
// docseach is lazy loaded, so we have to wait for it
74+
await waitFor(() => expect(window.docsearch).not.toBeUndefined())
75+
76+
// checking if docsearch has been initialized, we can confirm it if html
77+
// with class `ds-dropdown-menu` is injected
78+
expect(container.querySelector(".ds-dropdown-menu")).toBeInTheDocument()
79+
80+
// checking that input still has a focus
81+
expect(document.activeElement).toEqual(input)
82+
})

www/src/components/search-form/search-form.js

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function SearchForm({ i18n }) {
7070
keyboardShortcuts: [`s`],
7171
},
7272
})
73+
searchInput.current.focus()
7374
}
7475

7576
return () => {
@@ -118,6 +119,7 @@ function SearchForm({ i18n }) {
118119
className="searchWrap"
119120
onMouseOver={() => loadAlgoliaJS()}
120121
onClick={() => loadAlgoliaJS()}
122+
onFocus={() => loadAlgoliaJS()}
121123
onSubmit={e => e.preventDefault()}
122124
>
123125
<Global styles={algoliaStyles} />

0 commit comments

Comments
 (0)