|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, render } from "@testing-library/react" |
|
4
|
import SearchProvider from "../search" |
|
5
|
|
|
6
|
// Mock functions |
|
7
|
const mockIsLoading = vi.fn(() => false) |
|
8
|
const mockUsePageLoading = vi.fn(() => ({ |
|
9
|
isLoading: mockIsLoading(), |
|
10
|
})) |
|
11
|
const mockBasePathUrl = vi.fn((url: string) => url) |
|
12
|
|
|
13
|
vi.mock("docs-ui", () => ({ |
|
14
|
usePageLoading: () => mockUsePageLoading(), |
|
15
|
SearchProvider: ({ |
|
16
|
children, |
|
17
|
algolia, |
|
18
|
indices, |
|
19
|
defaultIndex, |
|
20
|
searchProps, |
|
21
|
}: { |
|
22
|
children: React.ReactNode |
|
23
|
algolia: unknown |
|
24
|
indices: unknown[] |
|
25
|
defaultIndex: string |
|
26
|
searchProps: unknown |
|
27
|
}) => ( |
|
28
|
<div |
|
29
|
data-testid="ui-search-provider" |
|
30
|
data-algolia={JSON.stringify(algolia)} |
|
31
|
data-indices={JSON.stringify(indices)} |
|
32
|
data-default-index={defaultIndex} |
|
33
|
data-search-props={JSON.stringify(searchProps)} |
|
34
|
> |
|
35
|
{children} |
|
36
|
</div> |
|
37
|
), |
|
38
|
})) |
|
39
|
|
|
40
|
vi.mock("@/config", () => ({ |
|
41
|
config: { |
|
42
|
baseUrl: "https://test.com", |
|
43
|
}, |
|
44
|
})) |
|
45
|
|
|
46
|
vi.mock("@/utils/base-path-url", () => ({ |
|
47
|
default: (url: string) => mockBasePathUrl(url), |
|
48
|
})) |
|
49
|
|
|
50
|
describe("SearchProvider", () => { |
|
51
|
beforeEach(() => { |
|
52
|
vi.clearAllMocks() |
|
53
|
cleanup() |
|
54
|
// Set environment variables |
|
55
|
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID = "test-app-id" |
|
56
|
process.env.NEXT_PUBLIC_ALGOLIA_API_KEY = "test-api-key" |
|
57
|
process.env.NEXT_PUBLIC_API_ALGOLIA_INDEX_NAME = "test-api-index" |
|
58
|
process.env.NEXT_PUBLIC_DOCS_ALGOLIA_INDEX_NAME = "test-docs-index" |
|
59
|
mockIsLoading.mockReturnValue(false) |
|
60
|
}) |
|
61
|
|
|
62
|
describe("rendering", () => { |
|
63
|
test("renders children", () => { |
|
64
|
const { getByText } = render( |
|
65
|
<SearchProvider> |
|
66
|
<div>Test Content</div> |
|
67
|
</SearchProvider> |
|
68
|
) |
|
69
|
expect(getByText("Test Content")).toBeInTheDocument() |
|
70
|
}) |
|
71
|
|
|
72
|
test("renders UiSearchProvider with correct props", () => { |
|
73
|
const { getByTestId } = render( |
|
74
|
<SearchProvider> |
|
75
|
<div>Test</div> |
|
76
|
</SearchProvider> |
|
77
|
) |
|
78
|
const uiProvider = getByTestId("ui-search-provider") |
|
79
|
expect(uiProvider).toBeInTheDocument() |
|
80
|
|
|
81
|
const algolia = JSON.parse(uiProvider.getAttribute("data-algolia") || "{}") |
|
82
|
expect(algolia).toEqual({ |
|
83
|
appId: "test-app-id", |
|
84
|
apiKey: "test-api-key", |
|
85
|
mainIndexName: "test-api-index", |
|
86
|
}) |
|
87
|
|
|
88
|
const indices = JSON.parse(uiProvider.getAttribute("data-indices") || "[]") |
|
89
|
expect(indices).toEqual([ |
|
90
|
{ |
|
91
|
value: "test-docs-index", |
|
92
|
title: "Docs", |
|
93
|
}, |
|
94
|
{ |
|
95
|
value: "test-api-index", |
|
96
|
title: "Store & Admin API", |
|
97
|
}, |
|
98
|
]) |
|
99
|
|
|
100
|
expect(uiProvider.getAttribute("data-default-index")).toBe("test-api-index") |
|
101
|
}) |
|
102
|
}) |
|
103
|
|
|
104
|
describe("environment variables", () => { |
|
105
|
test("uses default values when env vars are not set", () => { |
|
106
|
delete process.env.NEXT_PUBLIC_ALGOLIA_APP_ID |
|
107
|
delete process.env.NEXT_PUBLIC_ALGOLIA_API_KEY |
|
108
|
delete process.env.NEXT_PUBLIC_API_ALGOLIA_INDEX_NAME |
|
109
|
delete process.env.NEXT_PUBLIC_DOCS_ALGOLIA_INDEX_NAME |
|
110
|
|
|
111
|
const { getByTestId } = render( |
|
112
|
<SearchProvider> |
|
113
|
<div>Test</div> |
|
114
|
</SearchProvider> |
|
115
|
) |
|
116
|
const uiProvider = getByTestId("ui-search-provider") |
|
117
|
const algolia = JSON.parse(uiProvider.getAttribute("data-algolia") || "{}") |
|
118
|
expect(algolia.appId).toBe("temp") |
|
119
|
expect(algolia.apiKey).toBe("temp") |
|
120
|
expect(algolia.mainIndexName).toBe("temp") |
|
121
|
}) |
|
122
|
}) |
|
123
|
|
|
124
|
describe("searchProps", () => { |
|
125
|
test("passes isLoading from usePageLoading", () => { |
|
126
|
mockIsLoading.mockReturnValue(true) |
|
127
|
const { getByTestId } = render( |
|
128
|
<SearchProvider> |
|
129
|
<div>Test</div> |
|
130
|
</SearchProvider> |
|
131
|
) |
|
132
|
const uiProvider = getByTestId("ui-search-provider") |
|
133
|
const searchProps = JSON.parse(uiProvider.getAttribute("data-search-props") || "{}") |
|
134
|
expect(searchProps.isLoading).toBe(true) |
|
135
|
}) |
|
136
|
|
|
137
|
test("includes suggestions", () => { |
|
138
|
const { getByTestId } = render( |
|
139
|
<SearchProvider> |
|
140
|
<div>Test</div> |
|
141
|
</SearchProvider> |
|
142
|
) |
|
143
|
const uiProvider = getByTestId("ui-search-provider") |
|
144
|
const searchProps = JSON.parse(uiProvider.getAttribute("data-search-props") || "{}") |
|
145
|
expect(searchProps.suggestions).toBeDefined() |
|
146
|
expect(Array.isArray(searchProps.suggestions)).toBe(true) |
|
147
|
expect(searchProps.suggestions.length).toBeGreaterThan(0) |
|
148
|
}) |
|
149
|
|
|
150
|
test("includes checkInternalPattern regex", () => { |
|
151
|
const { getByTestId } = render( |
|
152
|
<SearchProvider> |
|
153
|
<div>Test</div> |
|
154
|
</SearchProvider> |
|
155
|
) |
|
156
|
const uiProvider = getByTestId("ui-search-provider") |
|
157
|
const searchProps = JSON.parse(uiProvider.getAttribute("data-search-props") || "{}") |
|
158
|
expect(searchProps.checkInternalPattern).toBeDefined() |
|
159
|
}) |
|
160
|
}) |
|
161
|
}) |
|
162
|
|