|
1
|
import { describe, expect, test, vi } from "vitest" |
|
2
|
|
|
3
|
/** |
|
4
|
* The registry keeps module-level state (the seeded map and the customized |
|
5
|
* flag), so each test loads a fresh instance. |
|
6
|
*/ |
|
7
|
async function loadRegistry() { |
|
8
|
vi.resetModules() |
|
9
|
return await import("../search-entities") |
|
10
|
} |
|
11
|
|
|
12
|
describe("search entity registry", () => { |
|
13
|
test("seeds the built-in entities at module evaluation", async () => { |
|
14
|
const registry = await loadRegistry() |
|
15
|
|
|
16
|
expect(registry.hasSearchEntity("product")).toBe(true) |
|
17
|
expect(registry.hasSearchEntity("order")).toBe(true) |
|
18
|
expect(registry.getSearchEntityNames().length).toBeGreaterThan(0) |
|
19
|
expect(registry.isSearchRegistryCustomized()).toBe(false) |
|
20
|
}) |
|
21
|
|
|
22
|
test("defineSearchEntity adds a custom entity and marks the registry customized", async () => { |
|
23
|
const registry = await loadRegistry() |
|
24
|
|
|
25
|
registry.defineSearchEntity("project", { |
|
26
|
groupLabel: "Projects", |
|
27
|
transform: (item) => ({ |
|
28
|
id: item.id, |
|
29
|
title: item.name, |
|
30
|
to: `/projects/${item.id}`, |
|
31
|
value: `project:${item.id}`, |
|
32
|
}), |
|
33
|
}) |
|
34
|
|
|
35
|
expect(registry.hasSearchEntity("project")).toBe(true) |
|
36
|
expect(registry.getSearchEntity("project")?.groupLabel).toEqual("Projects") |
|
37
|
expect(registry.isSearchRegistryCustomized()).toBe(true) |
|
38
|
}) |
|
39
|
|
|
40
|
test("overriding a built-in entity replaces it in place", async () => { |
|
41
|
const registry = await loadRegistry() |
|
42
|
|
|
43
|
const positionBefore = registry.getSearchEntityNames().indexOf("product") |
|
44
|
|
|
45
|
const custom = { |
|
46
|
groupLabel: "Catalog", |
|
47
|
transform: (item: any) => ({ |
|
48
|
id: item.id, |
|
49
|
title: item.title, |
|
50
|
to: `/catalog/${item.id}`, |
|
51
|
value: `product:${item.id}`, |
|
52
|
}), |
|
53
|
} |
|
54
|
|
|
55
|
registry.defineSearchEntity("product", custom) |
|
56
|
|
|
57
|
expect(registry.getSearchEntity("product")).toBe(custom) |
|
58
|
expect(registry.getSearchEntityNames().indexOf("product")).toEqual( |
|
59
|
positionBefore |
|
60
|
) |
|
61
|
}) |
|
62
|
|
|
63
|
test("clearSearchEntities removes every registration, including the defaults", async () => { |
|
64
|
const registry = await loadRegistry() |
|
65
|
|
|
66
|
registry.clearSearchEntities() |
|
67
|
|
|
68
|
expect(registry.getSearchEntityNames()).toEqual([]) |
|
69
|
expect(registry.hasSearchEntity("product")).toBe(false) |
|
70
|
expect(registry.isSearchRegistryCustomized()).toBe(true) |
|
71
|
}) |
|
72
|
|
|
73
|
test("replacing the defaults with a custom set keeps only that set", async () => { |
|
74
|
const registry = await loadRegistry() |
|
75
|
|
|
76
|
registry.clearSearchEntities() |
|
77
|
registry.defineSearchEntity("organization", { |
|
78
|
transform: (item) => ({ |
|
79
|
id: item.id, |
|
80
|
title: item.name, |
|
81
|
to: `/organizations/${item.id}`, |
|
82
|
value: `organization:${item.id}`, |
|
83
|
}), |
|
84
|
}) |
|
85
|
|
|
86
|
expect(registry.getSearchEntityNames()).toEqual(["organization"]) |
|
87
|
}) |
|
88
|
|
|
89
|
test("resolveSearchEntityGroupLabel prefers groupLabel over the i18n key", async () => { |
|
90
|
const registry = await loadRegistry() |
|
91
|
const t = ((key: string) => `translated:${key}`) as any |
|
92
|
|
|
93
|
registry.defineSearchEntity("organization", { |
|
94
|
groupLabel: "Organizations", |
|
95
|
transform: (item) => ({ |
|
96
|
id: item.id, |
|
97
|
title: item.name, |
|
98
|
to: `/organizations/${item.id}`, |
|
99
|
value: `organization:${item.id}`, |
|
100
|
}), |
|
101
|
}) |
|
102
|
|
|
103
|
expect(registry.resolveSearchEntityGroupLabel("organization", t)).toEqual( |
|
104
|
"Organizations" |
|
105
|
) |
|
106
|
|
|
107
|
registry.defineSearchEntity("project", { |
|
108
|
groupLabel: (translate) => translate("custom.projects"), |
|
109
|
transform: (item) => ({ |
|
110
|
id: item.id, |
|
111
|
title: item.name, |
|
112
|
to: `/projects/${item.id}`, |
|
113
|
value: `project:${item.id}`, |
|
114
|
}), |
|
115
|
}) |
|
116
|
|
|
117
|
expect(registry.resolveSearchEntityGroupLabel("project", t)).toEqual( |
|
118
|
"translated:custom.projects" |
|
119
|
) |
|
120
|
|
|
121
|
// A definition without groupLabel resolves the conventional key. |
|
122
|
expect(registry.resolveSearchEntityGroupLabel("product", t)).toEqual( |
|
123
|
"translated:app.search.groups.product" |
|
124
|
) |
|
125
|
}) |
|
126
|
|
|
127
|
test("resolveSearchEntityGroupLabel falls back to the entity name when the key is untranslated", async () => { |
|
128
|
const registry = await loadRegistry() |
|
129
|
|
|
130
|
// Mirrors i18next: returns `defaultValue` when the key has no translation. |
|
131
|
const t = ((key: string, options?: Record<string, unknown>) => |
|
132
|
(options?.defaultValue as string) ?? key) as any |
|
133
|
|
|
134
|
expect(registry.resolveSearchEntityGroupLabel("organization", t)).toEqual( |
|
135
|
"organization" |
|
136
|
) |
|
137
|
}) |
|
138
|
|
|
139
|
test("the built-in entities carry their Jump to shortcuts", async () => { |
|
140
|
const registry = await loadRegistry() |
|
141
|
|
|
142
|
const shortcuts = registry.getSearchEntityShortcuts() |
|
143
|
const order = shortcuts.find(({ entity }) => entity === "order") |
|
144
|
|
|
145
|
expect(order?.shortcut.to).toEqual("/orders") |
|
146
|
// Entities without a page of their own have no entry. |
|
147
|
expect(shortcuts.some(({ entity }) => entity === "product_variant")).toBe( |
|
148
|
false |
|
149
|
) |
|
150
|
}) |
|
151
|
|
|
152
|
test("a shortcut-only entry navigates but is never searched", async () => { |
|
153
|
const registry = await loadRegistry() |
|
154
|
|
|
155
|
const shortcuts = registry.getSearchEntityShortcuts() |
|
156
|
|
|
157
|
expect( |
|
158
|
shortcuts.find(({ entity }) => entity === "reservation")?.shortcut.to |
|
159
|
).toEqual("/reservations") |
|
160
|
expect(registry.hasSearchEntity("reservation")).toBe(false) |
|
161
|
expect(registry.getSearchEntityNames()).not.toContain("reservation") |
|
162
|
}) |
|
163
|
|
|
164
|
test("clearing the registry removes the Jump to entries with it", async () => { |
|
165
|
const registry = await loadRegistry() |
|
166
|
|
|
167
|
registry.clearSearchEntities() |
|
168
|
|
|
169
|
expect(registry.getSearchEntityShortcuts()).toEqual([]) |
|
170
|
}) |
|
171
|
|
|
172
|
test("a custom entity can declare its own Jump to entry", async () => { |
|
173
|
const registry = await loadRegistry() |
|
174
|
|
|
175
|
registry.defineSearchEntity("organization", { |
|
176
|
shortcut: { |
|
177
|
keys: { Mac: ["G", "Z"] }, |
|
178
|
label: "Go to Organizations", |
|
179
|
to: "/organizations", |
|
180
|
}, |
|
181
|
transform: (item) => ({ |
|
182
|
id: item.id, |
|
183
|
title: item.name, |
|
184
|
to: `/organizations/${item.id}`, |
|
185
|
value: `organization:${item.id}`, |
|
186
|
}), |
|
187
|
}) |
|
188
|
|
|
189
|
const entry = registry |
|
190
|
.getSearchEntityShortcuts() |
|
191
|
.find(({ entity }) => entity === "organization") |
|
192
|
|
|
193
|
expect(entry?.shortcut.to).toEqual("/organizations") |
|
194
|
|
|
195
|
const t = ((key: string) => `translated:${key}`) as any |
|
196
|
expect( |
|
197
|
registry.resolveSearchEntityShortcutLabel("organization", t) |
|
198
|
).toEqual("Go to Organizations") |
|
199
|
}) |
|
200
|
|
|
201
|
test("a shortcut without a label falls back to the entity's group label", async () => { |
|
202
|
const registry = await loadRegistry() |
|
203
|
|
|
204
|
registry.defineSearchEntity("organization", { |
|
205
|
groupLabel: "Organizations", |
|
206
|
shortcut: { |
|
207
|
keys: { Mac: ["G", "Z"] }, |
|
208
|
to: "/organizations", |
|
209
|
}, |
|
210
|
transform: (item) => ({ |
|
211
|
id: item.id, |
|
212
|
title: item.name, |
|
213
|
to: `/organizations/${item.id}`, |
|
214
|
value: `organization:${item.id}`, |
|
215
|
}), |
|
216
|
}) |
|
217
|
|
|
218
|
const t = ((key: string) => `translated:${key}`) as any |
|
219
|
expect( |
|
220
|
registry.resolveSearchEntityShortcutLabel("organization", t) |
|
221
|
).toEqual("Organizations") |
|
222
|
}) |
|
223
|
}) |