|
1
|
import React from "react" |
|
2
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, fireEvent, render, waitFor } from "@testing-library/react" |
|
4
|
import { SearchProvider, useSearch } from "../index" |
|
5
|
import type { SearchCommand, AlgoliaIndex } from "../index" |
|
6
|
|
|
7
|
// mock components |
|
8
|
vi.mock("@/components/Modal", () => ({ |
|
9
|
Modal: ({ |
|
10
|
children, |
|
11
|
open, |
|
12
|
onClose, |
|
13
|
...props |
|
14
|
}: { |
|
15
|
children?: React.ReactNode |
|
16
|
open?: boolean |
|
17
|
onClose?: () => void |
|
18
|
[key: string]: unknown |
|
19
|
}) => ( |
|
20
|
<div |
|
21
|
data-testid="search-modal" |
|
22
|
data-open={open} |
|
23
|
onClick={onClose} |
|
24
|
{...props} |
|
25
|
> |
|
26
|
{children} |
|
27
|
</div> |
|
28
|
), |
|
29
|
})) |
|
30
|
|
|
31
|
vi.mock("@/components/Search", () => ({ |
|
32
|
Search: ({ algolia }: { algolia: unknown }) => ( |
|
33
|
<div data-testid="search-component" data-algolia={JSON.stringify(algolia)}> |
|
34
|
Search |
|
35
|
</div> |
|
36
|
), |
|
37
|
})) |
|
38
|
|
|
39
|
vi.mock("react-transition-group", () => ({ |
|
40
|
CSSTransition: ({ |
|
41
|
children, |
|
42
|
}: { |
|
43
|
children: React.ReactNode |
|
44
|
classNames?: Record<string, string> |
|
45
|
timeout?: number |
|
46
|
}) => <div data-testid="css-transition">{children}</div>, |
|
47
|
SwitchTransition: ({ children }: { children: React.ReactNode }) => ( |
|
48
|
<div data-testid="switch-transition">{children}</div> |
|
49
|
), |
|
50
|
})) |
|
51
|
|
|
52
|
vi.mock("algoliasearch/lite", () => ({ |
|
53
|
liteClient: vi.fn(() => ({ |
|
54
|
search: vi.fn(), |
|
55
|
})), |
|
56
|
})) |
|
57
|
|
|
58
|
const TestComponent = () => { |
|
59
|
const { |
|
60
|
isOpen, |
|
61
|
setIsOpen, |
|
62
|
commands, |
|
63
|
command, |
|
64
|
setCommand, |
|
65
|
setCommands, |
|
66
|
selectedIndex, |
|
67
|
setSelectedIndex, |
|
68
|
} = useSearch() |
|
69
|
|
|
70
|
return ( |
|
71
|
<div> |
|
72
|
<div data-testid="is-open">{isOpen ? "open" : "closed"}</div> |
|
73
|
<div data-testid="commands-count">{commands.length}</div> |
|
74
|
<div data-testid="command">{command?.name || "none"}</div> |
|
75
|
<div data-testid="selected-index">{selectedIndex}</div> |
|
76
|
<button data-testid="open" onClick={() => setIsOpen(true)}> |
|
77
|
Open |
|
78
|
</button> |
|
79
|
<button data-testid="close" onClick={() => setIsOpen(false)}> |
|
80
|
Close |
|
81
|
</button> |
|
82
|
<button |
|
83
|
data-testid="set-command" |
|
84
|
onClick={() => { |
|
85
|
setCommand({ |
|
86
|
name: "test-command", |
|
87
|
title: "Test Command", |
|
88
|
component: ( |
|
89
|
<div data-testid="command-component">Command Component</div> |
|
90
|
), |
|
91
|
}) |
|
92
|
}} |
|
93
|
> |
|
94
|
Set Command |
|
95
|
</button> |
|
96
|
<button |
|
97
|
data-testid="add-command" |
|
98
|
onClick={() => |
|
99
|
setCommands([ |
|
100
|
{ |
|
101
|
name: "new-command", |
|
102
|
title: "New Command", |
|
103
|
}, |
|
104
|
]) |
|
105
|
} |
|
106
|
> |
|
107
|
Add Command |
|
108
|
</button> |
|
109
|
<button |
|
110
|
data-testid="change-index" |
|
111
|
onClick={() => setSelectedIndex("index2")} |
|
112
|
> |
|
113
|
Change Index |
|
114
|
</button> |
|
115
|
</div> |
|
116
|
) |
|
117
|
} |
|
118
|
|
|
119
|
const defaultIndices: AlgoliaIndex[] = [ |
|
120
|
{ value: "index1", title: "Index 1" }, |
|
121
|
{ value: "index2", title: "Index 2" }, |
|
122
|
] |
|
123
|
|
|
124
|
const defaultAlgolia = { |
|
125
|
appId: "test-app-id", |
|
126
|
apiKey: "test-api-key", |
|
127
|
mainIndexName: "index1", |
|
128
|
} |
|
129
|
|
|
130
|
const defaultSearchProps = { |
|
131
|
indices: defaultIndices, |
|
132
|
suggestions: [], |
|
133
|
} |
|
134
|
|
|
135
|
beforeEach(() => { |
|
136
|
vi.clearAllMocks() |
|
137
|
}) |
|
138
|
|
|
139
|
afterEach(() => { |
|
140
|
cleanup() |
|
141
|
}) |
|
142
|
|
|
143
|
describe("rendering", () => { |
|
144
|
test("renders children", () => { |
|
145
|
const { container } = render( |
|
146
|
<SearchProvider |
|
147
|
indices={defaultIndices} |
|
148
|
defaultIndex="index1" |
|
149
|
algolia={defaultAlgolia} |
|
150
|
searchProps={defaultSearchProps} |
|
151
|
> |
|
152
|
<div>Test</div> |
|
153
|
</SearchProvider> |
|
154
|
) |
|
155
|
expect(container).toHaveTextContent("Test") |
|
156
|
}) |
|
157
|
|
|
158
|
test("renders search modal when open", () => { |
|
159
|
const { getByTestId } = render( |
|
160
|
<SearchProvider |
|
161
|
indices={defaultIndices} |
|
162
|
defaultIndex="index1" |
|
163
|
algolia={defaultAlgolia} |
|
164
|
searchProps={defaultSearchProps} |
|
165
|
> |
|
166
|
<TestComponent /> |
|
167
|
</SearchProvider> |
|
168
|
) |
|
169
|
|
|
170
|
fireEvent.click(getByTestId("open")) |
|
171
|
|
|
172
|
expect(getByTestId("search-modal")).toBeInTheDocument() |
|
173
|
expect(getByTestId("search-modal")).toHaveAttribute("data-open", "true") |
|
174
|
}) |
|
175
|
|
|
176
|
test("renders search component when no command is set", () => { |
|
177
|
const { getByTestId } = render( |
|
178
|
<SearchProvider |
|
179
|
indices={defaultIndices} |
|
180
|
defaultIndex="index1" |
|
181
|
algolia={defaultAlgolia} |
|
182
|
searchProps={defaultSearchProps} |
|
183
|
> |
|
184
|
<TestComponent /> |
|
185
|
</SearchProvider> |
|
186
|
) |
|
187
|
|
|
188
|
fireEvent.click(getByTestId("open")) |
|
189
|
|
|
190
|
expect(getByTestId("search-component")).toBeInTheDocument() |
|
191
|
}) |
|
192
|
|
|
193
|
test("renders command component when command is set", () => { |
|
194
|
const { getByTestId } = render( |
|
195
|
<SearchProvider |
|
196
|
indices={defaultIndices} |
|
197
|
defaultIndex="index1" |
|
198
|
algolia={defaultAlgolia} |
|
199
|
searchProps={defaultSearchProps} |
|
200
|
> |
|
201
|
<TestComponent /> |
|
202
|
</SearchProvider> |
|
203
|
) |
|
204
|
|
|
205
|
fireEvent.click(getByTestId("open")) |
|
206
|
fireEvent.click(getByTestId("set-command")) |
|
207
|
|
|
208
|
expect(getByTestId("command-component")).toBeInTheDocument() |
|
209
|
}) |
|
210
|
}) |
|
211
|
|
|
212
|
describe("useSearch hook", () => { |
|
213
|
test("isOpen defaults to false", () => { |
|
214
|
const { getByTestId } = render( |
|
215
|
<SearchProvider |
|
216
|
indices={defaultIndices} |
|
217
|
defaultIndex="index1" |
|
218
|
algolia={defaultAlgolia} |
|
219
|
searchProps={defaultSearchProps} |
|
220
|
> |
|
221
|
<TestComponent /> |
|
222
|
</SearchProvider> |
|
223
|
) |
|
224
|
|
|
225
|
expect(getByTestId("is-open")).toHaveTextContent("closed") |
|
226
|
}) |
|
227
|
|
|
228
|
test("setIsOpen updates isOpen", () => { |
|
229
|
const { getByTestId } = render( |
|
230
|
<SearchProvider |
|
231
|
indices={defaultIndices} |
|
232
|
defaultIndex="index1" |
|
233
|
algolia={defaultAlgolia} |
|
234
|
searchProps={defaultSearchProps} |
|
235
|
> |
|
236
|
<TestComponent /> |
|
237
|
</SearchProvider> |
|
238
|
) |
|
239
|
|
|
240
|
fireEvent.click(getByTestId("open")) |
|
241
|
|
|
242
|
expect(getByTestId("is-open")).toHaveTextContent("open") |
|
243
|
|
|
244
|
fireEvent.click(getByTestId("close")) |
|
245
|
|
|
246
|
expect(getByTestId("is-open")).toHaveTextContent("closed") |
|
247
|
}) |
|
248
|
|
|
249
|
test("initializes with default commands", () => { |
|
250
|
const commands: SearchCommand[] = [ |
|
251
|
{ |
|
252
|
name: "command1", |
|
253
|
title: "Command 1", |
|
254
|
}, |
|
255
|
] |
|
256
|
|
|
257
|
const { getByTestId } = render( |
|
258
|
<SearchProvider |
|
259
|
indices={defaultIndices} |
|
260
|
defaultIndex="index1" |
|
261
|
algolia={defaultAlgolia} |
|
262
|
searchProps={defaultSearchProps} |
|
263
|
commands={commands} |
|
264
|
> |
|
265
|
<TestComponent /> |
|
266
|
</SearchProvider> |
|
267
|
) |
|
268
|
|
|
269
|
expect(getByTestId("commands-count")).toHaveTextContent("1") |
|
270
|
}) |
|
271
|
|
|
272
|
test("setCommands updates commands", () => { |
|
273
|
const { getByTestId } = render( |
|
274
|
<SearchProvider |
|
275
|
indices={defaultIndices} |
|
276
|
defaultIndex="index1" |
|
277
|
algolia={defaultAlgolia} |
|
278
|
searchProps={defaultSearchProps} |
|
279
|
> |
|
280
|
<TestComponent /> |
|
281
|
</SearchProvider> |
|
282
|
) |
|
283
|
|
|
284
|
expect(getByTestId("commands-count")).toHaveTextContent("0") |
|
285
|
|
|
286
|
fireEvent.click(getByTestId("add-command")) |
|
287
|
|
|
288
|
expect(getByTestId("commands-count")).toHaveTextContent("1") |
|
289
|
}) |
|
290
|
|
|
291
|
test("setCommand updates command", () => { |
|
292
|
const { getByTestId } = render( |
|
293
|
<SearchProvider |
|
294
|
indices={defaultIndices} |
|
295
|
defaultIndex="index1" |
|
296
|
algolia={defaultAlgolia} |
|
297
|
searchProps={defaultSearchProps} |
|
298
|
> |
|
299
|
<TestComponent /> |
|
300
|
</SearchProvider> |
|
301
|
) |
|
302
|
|
|
303
|
expect(getByTestId("command")).toHaveTextContent("none") |
|
304
|
|
|
305
|
fireEvent.click(getByTestId("set-command")) |
|
306
|
|
|
307
|
expect(getByTestId("command")).toHaveTextContent("test-command") |
|
308
|
}) |
|
309
|
|
|
310
|
test("calls command action when command is set", () => { |
|
311
|
const action = vi.fn() |
|
312
|
const TestComponentWithAction = () => { |
|
313
|
const { setCommand } = useSearch() |
|
314
|
return ( |
|
315
|
<button |
|
316
|
data-testid="set-command-action" |
|
317
|
onClick={() => |
|
318
|
setCommand({ |
|
319
|
name: "test-command", |
|
320
|
title: "Test Command", |
|
321
|
action, |
|
322
|
}) |
|
323
|
} |
|
324
|
> |
|
325
|
Set Command |
|
326
|
</button> |
|
327
|
) |
|
328
|
} |
|
329
|
|
|
330
|
const { getByTestId } = render( |
|
331
|
<SearchProvider |
|
332
|
indices={defaultIndices} |
|
333
|
defaultIndex="index1" |
|
334
|
algolia={defaultAlgolia} |
|
335
|
searchProps={defaultSearchProps} |
|
336
|
> |
|
337
|
<TestComponentWithAction /> |
|
338
|
</SearchProvider> |
|
339
|
) |
|
340
|
|
|
341
|
fireEvent.click(getByTestId("set-command-action")) |
|
342
|
|
|
343
|
expect(action).toHaveBeenCalled() |
|
344
|
}) |
|
345
|
|
|
346
|
test("selectedIndex defaults to defaultIndex", () => { |
|
347
|
const { getByTestId } = render( |
|
348
|
<SearchProvider |
|
349
|
indices={defaultIndices} |
|
350
|
defaultIndex="index1" |
|
351
|
algolia={defaultAlgolia} |
|
352
|
searchProps={defaultSearchProps} |
|
353
|
> |
|
354
|
<TestComponent /> |
|
355
|
</SearchProvider> |
|
356
|
) |
|
357
|
|
|
358
|
expect(getByTestId("selected-index")).toHaveTextContent("index1") |
|
359
|
}) |
|
360
|
|
|
361
|
test("setSelectedIndex updates selectedIndex", () => { |
|
362
|
const { getByTestId } = render( |
|
363
|
<SearchProvider |
|
364
|
indices={defaultIndices} |
|
365
|
defaultIndex="index1" |
|
366
|
algolia={defaultAlgolia} |
|
367
|
searchProps={defaultSearchProps} |
|
368
|
> |
|
369
|
<TestComponent /> |
|
370
|
</SearchProvider> |
|
371
|
) |
|
372
|
|
|
373
|
fireEvent.click(getByTestId("change-index")) |
|
374
|
|
|
375
|
expect(getByTestId("selected-index")).toHaveTextContent("index2") |
|
376
|
}) |
|
377
|
|
|
378
|
test("updates selectedIndex when defaultIndex changes", async () => { |
|
379
|
const { getByTestId, rerender } = render( |
|
380
|
<SearchProvider |
|
381
|
indices={defaultIndices} |
|
382
|
defaultIndex="index1" |
|
383
|
algolia={defaultAlgolia} |
|
384
|
searchProps={defaultSearchProps} |
|
385
|
> |
|
386
|
<TestComponent /> |
|
387
|
</SearchProvider> |
|
388
|
) |
|
389
|
|
|
390
|
expect(getByTestId("selected-index")).toHaveTextContent("index1") |
|
391
|
|
|
392
|
rerender( |
|
393
|
<SearchProvider |
|
394
|
indices={defaultIndices} |
|
395
|
defaultIndex="index2" |
|
396
|
algolia={defaultAlgolia} |
|
397
|
searchProps={defaultSearchProps} |
|
398
|
> |
|
399
|
<TestComponent /> |
|
400
|
</SearchProvider> |
|
401
|
) |
|
402
|
|
|
403
|
await waitFor(() => { |
|
404
|
expect(getByTestId("selected-index")).toHaveTextContent("index2") |
|
405
|
}) |
|
406
|
}) |
|
407
|
|
|
408
|
test("provides searchClient", () => { |
|
409
|
const TestComponentWithClient = () => { |
|
410
|
const { searchClient } = useSearch() |
|
411
|
return <div data-testid="client">{searchClient ? "yes" : "no"}</div> |
|
412
|
} |
|
413
|
|
|
414
|
const { getByTestId } = render( |
|
415
|
<SearchProvider |
|
416
|
indices={defaultIndices} |
|
417
|
defaultIndex="index1" |
|
418
|
algolia={defaultAlgolia} |
|
419
|
searchProps={defaultSearchProps} |
|
420
|
> |
|
421
|
<TestComponentWithClient /> |
|
422
|
</SearchProvider> |
|
423
|
) |
|
424
|
|
|
425
|
expect(getByTestId("client")).toHaveTextContent("yes") |
|
426
|
}) |
|
427
|
|
|
428
|
test("provides modalRef", () => { |
|
429
|
const TestComponentWithRef = () => { |
|
430
|
const { modalRef } = useSearch() |
|
431
|
return <div data-testid="ref">{modalRef.current ? "yes" : "no"}</div> |
|
432
|
} |
|
433
|
|
|
434
|
const { getByTestId } = render( |
|
435
|
<SearchProvider |
|
436
|
indices={defaultIndices} |
|
437
|
defaultIndex="index1" |
|
438
|
algolia={defaultAlgolia} |
|
439
|
searchProps={defaultSearchProps} |
|
440
|
> |
|
441
|
<TestComponentWithRef /> |
|
442
|
</SearchProvider> |
|
443
|
) |
|
444
|
|
|
445
|
expect(getByTestId("ref")).toBeInTheDocument() |
|
446
|
}) |
|
447
|
|
|
448
|
test("throws error when used outside provider", () => { |
|
449
|
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
|
450
|
|
|
451
|
expect(() => { |
|
452
|
render(<TestComponent />) |
|
453
|
}).toThrow("useSearch must be used inside a SearchProvider") |
|
454
|
|
|
455
|
consoleSpy.mockRestore() |
|
456
|
}) |
|
457
|
}) |