1 import React from "react"
2 import { beforeEach, describe, expect, test, vi } from "vitest"
3 import { render } from "@testing-library/react"
4 import { SearchSuggestionType } from "../Suggestions"
5
6 // mock data
7 const defaultUseSearchReturn = {
8 isOpen: true,
9 searchClient: {},
10 modalRef: React.createRef<HTMLDivElement>(),
11 }
12
13 const defaultAlgoliaProps = {
14 appId: "test",
15 apiKey: "test",
16 mainIndexName: "docs",
17 }
18
19 // mock hooks
20 const mockUseSearch = vi.fn(() => defaultUseSearchReturn)
21 const mockUseSearchNavigation = vi.fn()
22
23 vi.mock("@/providers/Search", () => ({
24 useSearch: () => mockUseSearch(),
25 }))
26
27 vi.mock("@/hooks/use-search-navigation", () => ({
28 useSearchNavigation: (options: unknown) => mockUseSearchNavigation(options),
29 }))
30
31 // mock components
32 vi.mock("react-instantsearch", () => ({
33 InstantSearch: ({
34 children,
35 indexName,
36 }: {
37 children: React.ReactNode
38 indexName: string
39 }) => (
40 <div data-testid="instant-search" data-index-name={indexName}>
41 {children}
42 </div>
43 ),
44 SearchBox: ({
45 placeholder,
46 formRef,
47 }: {
48 placeholder?: string
49 formRef?: React.RefObject<HTMLFormElement>
50 }) => (
51 <form ref={formRef} data-testid="search-box">
52 <input placeholder={placeholder} data-testid="search-input" />
53 </form>
54 ),
55 }))
56
57 vi.mock("@/components/Search/EmptyQueryBoundary", () => ({
58 SearchEmptyQueryBoundary: ({
59 children,
60 fallback,
61 }: {
62 children: React.ReactNode
63 fallback: React.ReactNode
64 }) => (
65 <div data-testid="empty-query-boundary">
66 {children}
67 {fallback}
68 </div>
69 ),
70 }))
71
72 vi.mock("@/components/Search/Suggestions", () => ({
73 SearchSuggestions: ({
74 suggestions,
75 }: {
76 suggestions: SearchSuggestionType[]
77 }) => (
78 <div data-testid="search-suggestions">
79 {suggestions.map((s, i) => (
80 <div key={i}>{s.title}</div>
81 ))}
82 </div>
83 ),
84 }))
85
86 vi.mock("@/components/Search/Hits", () => ({
87 SearchHitsWrapper: ({
88 checkInternalPattern,
89 }: {
90 checkInternalPattern?: RegExp
91 }) => (
92 <div
93 data-testid="search-hits-wrapper"
94 data-pattern={checkInternalPattern?.toString()}
95 >
96 Hits
97 </div>
98 ),
99 }))
100
101 vi.mock("@/components/Search/Filters", () => ({
102 SearchFilters: () => <div data-testid="search-filters">Filters</div>,
103 }))
104
105 vi.mock("@/components/Search/Footer", () => ({
106 SearchFooter: () => <div data-testid="search-footer">Footer</div>,
107 }))
108
109 vi.mock("@/components/Loading/Spinner", () => ({
110 SpinnerLoading: () => <div data-testid="spinner-loading">Loading</div>,
111 }))
112 vi.mock("@/components/Search/Callout", () => ({
113 SearchCallout: () => <div data-testid="search-callout">Callout</div>,
114 }))
115
116 import { Search } from "../../Search"
117
118 beforeEach(() => {
119 vi.clearAllMocks()
120 mockUseSearch.mockReturnValue(defaultUseSearchReturn)
121 })
122
123 describe("rendering", () => {
124 test("renders search component", () => {
125 const suggestions: SearchSuggestionType[] = []
126 const { container } = render(
127 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
128 )
129 const instantSearch = container.querySelector(
130 "[data-testid='instant-search']"
131 )
132 expect(instantSearch).toBeInTheDocument()
133 })
134
135 test("renders InstantSearch with correct index name", () => {
136 const suggestions: SearchSuggestionType[] = []
137 const { container } = render(
138 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
139 )
140 const instantSearch = container.querySelector(
141 "[data-testid='instant-search']"
142 )
143 expect(instantSearch).toHaveAttribute("data-index-name", "docs")
144 })
145
146 test("renders SearchBox", () => {
147 const suggestions: SearchSuggestionType[] = []
148 const { container } = render(
149 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
150 )
151 const searchBox = container.querySelector("[data-testid='search-box']")
152 expect(searchBox).toBeInTheDocument()
153 })
154
155 test("renders search input with placeholder", () => {
156 const suggestions: SearchSuggestionType[] = []
157 const { container } = render(
158 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
159 )
160 const input = container.querySelector("[data-testid='search-input']")
161 expect(input).toHaveAttribute("placeholder", "Find something...")
162 })
163
164 test("renders SearchFilters", () => {
165 const suggestions: SearchSuggestionType[] = []
166 const { container } = render(
167 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
168 )
169 const filters = container.querySelector("[data-testid='search-filters']")
170 expect(filters).toBeInTheDocument()
171 })
172
173 test("renders SearchEmptyQueryBoundary", () => {
174 const suggestions: SearchSuggestionType[] = []
175 const { container } = render(
176 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
177 )
178 const boundary = container.querySelector(
179 "[data-testid='empty-query-boundary']"
180 )
181 expect(boundary).toBeInTheDocument()
182 })
183
184 test("renders SearchSuggestions as fallback", () => {
185 const suggestions: SearchSuggestionType[] = [
186 { title: "Getting Started", items: ["Item 1"] },
187 ]
188 const { container } = render(
189 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
190 )
191 const suggestionsEl = container.querySelector(
192 "[data-testid='search-suggestions']"
193 )
194 expect(suggestionsEl).toBeInTheDocument()
195 })
196
197 test("renders SearchHitsWrapper", () => {
198 const suggestions: SearchSuggestionType[] = []
199 const { container } = render(
200 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
201 )
202 const hits = container.querySelector("[data-testid='search-hits-wrapper']")
203 expect(hits).toBeInTheDocument()
204 })
205
206 test("renders SearchFooter", () => {
207 const suggestions: SearchSuggestionType[] = []
208 const { container } = render(
209 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
210 )
211 const footer = container.querySelector("[data-testid='search-footer']")
212 expect(footer).toBeInTheDocument()
213 })
214
215 test("renders SearchCallout", () => {
216 const suggestions: SearchSuggestionType[] = []
217 const { container } = render(
218 <Search algolia={defaultAlgoliaProps} suggestions={suggestions} />
219 )
220 const callout = container.querySelector("[data-testid='search-callout']")
221 expect(callout).toBeInTheDocument()
222 })
223
224 test("passes checkInternalPattern to SearchHitsWrapper", () => {
225 const suggestions: SearchSuggestionType[] = []
226 const pattern = /\/docs\//
227 const { container } = render(
228 <Search
229 algolia={defaultAlgoliaProps}
230 suggestions={suggestions}
231 checkInternalPattern={pattern}
232 />
233 )
234 const hits = container.querySelector("[data-testid='search-hits-wrapper']")
235 expect(hits).toHaveAttribute("data-pattern", pattern.toString())
236 })
237 })
238
239 describe("focus management", () => {
240 test("sets up search navigation hook", () => {
241 const suggestions: SearchSuggestionType[] = []
242 render(<Search algolia={defaultAlgoliaProps} suggestions={suggestions} />)
243 expect(mockUseSearchNavigation).toHaveBeenCalledWith({
244 getInputElm: expect.any(Function),
245 focusInput: expect.any(Function),
246 keyboardProps: {
247 isLoading: false,
248 },
249 })
250 })
251
252 test("passes isLoading to keyboard props", () => {
253 const suggestions: SearchSuggestionType[] = []
254 render(
255 <Search
256 algolia={defaultAlgoliaProps}
257 suggestions={suggestions}
258 isLoading={true}
259 />
260 )
261 expect(mockUseSearchNavigation).toHaveBeenCalledWith(
262 expect.objectContaining({
263 keyboardProps: {
264 isLoading: true,
265 },
266 })
267 )
268 })
269 })