|
| 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 | +}) |
0 commit comments