-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathutils.test.ts
More file actions
408 lines (353 loc) · 13 KB
/
utils.test.ts
File metadata and controls
408 lines (353 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { type AllAppExtensionMounts } from "@dashboard/extensions/domain/app-extension-manifest-available-mounts";
import { type Extension } from "@dashboard/extensions/types";
import { type PermissionEnum } from "@dashboard/graphql";
import { orderDraftListUrl, orderListUrl } from "@dashboard/orders/urls";
import { type SidebarMenuItem } from "./types";
import { getMenuItemExtension, isMenuActive, mapToExtensionsItems } from "./utils";
jest.mock("@dashboard/extensions/urls", () => ({
ExtensionsUrls: {
resolveDashboardUrlFromAppCompleteUrl: jest.fn(
(url, appUrl, appId) => `mockExtensionUrl:${url}:${appUrl}:${appId}`,
),
},
}));
// To grab the mocked functions for assertions
const { ExtensionsUrls } = jest.requireMock("@dashboard/extensions/urls");
describe("mapToExtensionsItems", () => {
beforeEach(() => {
ExtensionsUrls.resolveDashboardUrlFromAppCompleteUrl.mockClear();
});
const mockApp: Extension["app"] = {
__typename: "App",
id: "app-1",
appUrl: "https://app.example.com",
name: "App name",
brand: null,
};
const mockExtension: Extension = {
id: "test-extension",
label: "Test Extension",
app: mockApp,
url: "/test",
permissions: [] as PermissionEnum[],
open: jest.fn(),
accessToken: "mock-token",
mountName: "NAVIGATION_CATALOG",
targetName: "APP_PAGE",
settings: {},
};
const mockHeader: SidebarMenuItem = {
id: "extensions-header",
label: "Extensions",
type: "divider",
};
it("should map extensions to menu items", () => {
const result = mapToExtensionsItems([mockExtension], mockHeader);
expect(result).toHaveLength(2);
expect(result[0]).toBe(mockHeader);
expect(result[1]).toEqual({
id: "extension-test-extension",
label: "Test Extension",
url: "mockExtensionUrl:/test:https://app.example.com:app-1",
permissions: [] as PermissionEnum[],
onClick: mockExtension.open,
type: "item",
});
expect(ExtensionsUrls.resolveDashboardUrlFromAppCompleteUrl).toHaveBeenCalledWith(
mockExtension.url,
mockExtension.app.appUrl,
mockExtension.app.id,
);
});
it("should return no menu items ", () => {
const result = mapToExtensionsItems([], mockHeader);
expect(result).toHaveLength(0);
});
});
describe("isMenuActive", () => {
const mockMenuItem: SidebarMenuItem = {
id: "test-item",
label: "Test Item",
url: "/test",
type: "item",
};
it("should identify menu item as active when current path matches its URL", () => {
const result = isMenuActive("/test", mockMenuItem);
expect(result).toBe(true);
});
it("should identify menu item as active when current path matches one of its alternative URLs (matchUrls)", () => {
const menuItemWithMatchUrls: SidebarMenuItem = {
...mockMenuItem,
matchUrls: ["/test/alternative", "/test"],
};
const result = isMenuActive("/test/alternative", menuItemWithMatchUrls);
expect(result).toBe(true);
});
it("should identify menu item as inactive when current path matches none of its URLs", () => {
const result = isMenuActive("/different", mockMenuItem);
expect(result).toBe(false);
});
it("should identify extension (dashboard extensions from apps) menu items as inactive", () => {
const extensionMenuItem: SidebarMenuItem = {
...mockMenuItem,
id: "extension-test",
};
const result = isMenuActive("/test", extensionMenuItem);
expect(result).toBe(false);
});
it("should identify Order List item as inactive when on the Order Drafts page", () => {
const orderMenuItem: SidebarMenuItem = {
...mockMenuItem,
url: orderListUrl(),
};
const result = isMenuActive(orderDraftListUrl(), orderMenuItem);
expect(result).toBe(false);
});
it("should correctly match paths regardless of '/dashboard' prefix in current location", () => {
const result = isMenuActive("/dashboard/test", mockMenuItem);
expect(result).toBe(true);
});
it("should correctly match paths by ignoring query parameters in current location and item URLs", () => {
const result = isMenuActive("/test?param=value", mockMenuItem);
expect(result).toBe(true);
});
it("should identify Home menu item (URL '/') as active when current path is root ('/')", () => {
const homeMenuItem: SidebarMenuItem = {
...mockMenuItem,
url: "/",
};
const result = isMenuActive("/", homeMenuItem);
expect(result).toBe(true);
});
it("should identify item as inactive if its main URL is undefined and no alternative URLs match", () => {
const menuItemWithOnlyMatchUrls: SidebarMenuItem = {
id: "test-item-match",
label: "Test Item Match",
matchUrls: ["/foo", "/bar"],
type: "item",
};
const result = isMenuActive("/baz", menuItemWithOnlyMatchUrls);
expect(result).toBe(false);
});
it("should identify item as active if its main URL is undefined and an alternative URL matches", () => {
const menuItemWithOnlyMatchUrls: SidebarMenuItem = {
id: "test-item-match",
label: "Test Item Match",
matchUrls: ["/foo", "/bar"],
type: "item",
};
const result = isMenuActive("/foo", menuItemWithOnlyMatchUrls);
expect(result).toBe(true);
});
it("should identify item as inactive if it has no main URL or alternative URLs defined", () => {
const menuItemWithoutUrls: SidebarMenuItem = {
id: "test-item-no-urls",
label: "Test Item No URLs",
type: "item",
};
const result = isMenuActive("/anywhere", menuItemWithoutUrls);
expect(result).toBe(false);
});
it("should identify Order List item as active when on the Order List page", () => {
const orderMenuItem: SidebarMenuItem = {
...mockMenuItem,
url: orderListUrl(),
};
const result = isMenuActive(orderListUrl(), orderMenuItem);
expect(result).toBe(true);
});
});
describe("getMenuItemExtension", () => {
const mockAppDefinition: Extension["app"] = {
__typename: "App",
id: "app-1",
appUrl: "https://app.example.com",
name: "App name",
brand: null,
};
const baseMockExtension: Extension = {
id: "base-id",
label: "Base Label",
app: mockAppDefinition,
url: "/base-url",
permissions: [] as PermissionEnum[],
open: jest.fn(),
accessToken: "mock-token",
mountName: "NAVIGATION_CATALOG",
settings: {},
targetName: "POPUP",
};
const mockExtension: Extension = {
...baseMockExtension,
id: "test-extension",
label: "Test Extension",
app: mockAppDefinition,
url: "/test",
};
const mockExtensionsRecord: Record<AllAppExtensionMounts, Extension[]> = {
NAVIGATION_CATALOG: [mockExtension],
CUSTOMER_DETAILS_MORE_ACTIONS: [],
CUSTOMER_OVERVIEW_CREATE: [],
CUSTOMER_OVERVIEW_MORE_ACTIONS: [],
NAVIGATION_CUSTOMERS: [],
NAVIGATION_DISCOUNTS: [],
NAVIGATION_ORDERS: [],
NAVIGATION_PAGES: [],
NAVIGATION_TRANSLATIONS: [],
ORDER_DETAILS_MORE_ACTIONS: [],
ORDER_OVERVIEW_CREATE: [],
ORDER_OVERVIEW_MORE_ACTIONS: [],
PRODUCT_DETAILS_MORE_ACTIONS: [],
PRODUCT_OVERVIEW_CREATE: [],
PRODUCT_OVERVIEW_MORE_ACTIONS: [],
DRAFT_ORDER_DETAILS_MORE_ACTIONS: [],
DRAFT_ORDER_OVERVIEW_CREATE: [],
DRAFT_ORDER_OVERVIEW_MORE_ACTIONS: [],
DISCOUNT_DETAILS_MORE_ACTIONS: [],
DISCOUNT_OVERVIEW_CREATE: [],
DISCOUNT_OVERVIEW_MORE_ACTIONS: [],
VOUCHER_DETAILS_MORE_ACTIONS: [],
VOUCHER_OVERVIEW_CREATE: [],
VOUCHER_OVERVIEW_MORE_ACTIONS: [],
PAGE_DETAILS_MORE_ACTIONS: [],
PAGE_OVERVIEW_CREATE: [],
PAGE_OVERVIEW_MORE_ACTIONS: [],
PAGE_TYPE_DETAILS_MORE_ACTIONS: [],
PAGE_TYPE_OVERVIEW_CREATE: [],
PAGE_TYPE_OVERVIEW_MORE_ACTIONS: [],
MENU_DETAILS_MORE_ACTIONS: [],
MENU_OVERVIEW_CREATE: [],
MENU_OVERVIEW_MORE_ACTIONS: [],
COLLECTION_DETAILS_MORE_ACTIONS: [],
CATEGORY_DETAILS_MORE_ACTIONS: [],
GIFT_CARD_DETAILS_MORE_ACTIONS: [],
CATEGORY_OVERVIEW_CREATE: [],
CATEGORY_OVERVIEW_MORE_ACTIONS: [],
GIFT_CARD_OVERVIEW_CREATE: [],
GIFT_CARD_OVERVIEW_MORE_ACTIONS: [],
COLLECTION_OVERVIEW_CREATE: [],
COLLECTION_OVERVIEW_MORE_ACTIONS: [],
CUSTOMER_DETAILS_WIDGETS: [],
ORDER_DETAILS_WIDGETS: [],
COLLECTION_DETAILS_WIDGETS: [],
PRODUCT_DETAILS_WIDGETS: [],
VOUCHER_DETAILS_WIDGETS: [],
DRAFT_ORDER_DETAILS_WIDGETS: [],
GIFT_CARD_DETAILS_WIDGETS: [],
TRANSLATIONS_MORE_ACTIONS: [],
HOMEPAGE_WIDGETS: [],
};
const emptyExtensionsRecord: Record<AllAppExtensionMounts, Extension[]> = {
NAVIGATION_CATALOG: [],
CUSTOMER_DETAILS_MORE_ACTIONS: [],
CUSTOMER_OVERVIEW_CREATE: [],
CUSTOMER_OVERVIEW_MORE_ACTIONS: [],
NAVIGATION_CUSTOMERS: [],
NAVIGATION_DISCOUNTS: [],
NAVIGATION_ORDERS: [],
NAVIGATION_PAGES: [],
NAVIGATION_TRANSLATIONS: [],
ORDER_DETAILS_MORE_ACTIONS: [],
ORDER_OVERVIEW_CREATE: [],
ORDER_OVERVIEW_MORE_ACTIONS: [],
PRODUCT_DETAILS_MORE_ACTIONS: [],
PRODUCT_OVERVIEW_CREATE: [],
PRODUCT_OVERVIEW_MORE_ACTIONS: [],
DRAFT_ORDER_DETAILS_MORE_ACTIONS: [],
DRAFT_ORDER_OVERVIEW_CREATE: [],
DRAFT_ORDER_OVERVIEW_MORE_ACTIONS: [],
DISCOUNT_DETAILS_MORE_ACTIONS: [],
DISCOUNT_OVERVIEW_CREATE: [],
DISCOUNT_OVERVIEW_MORE_ACTIONS: [],
VOUCHER_DETAILS_MORE_ACTIONS: [],
VOUCHER_OVERVIEW_CREATE: [],
VOUCHER_OVERVIEW_MORE_ACTIONS: [],
PAGE_DETAILS_MORE_ACTIONS: [],
PAGE_OVERVIEW_CREATE: [],
PAGE_OVERVIEW_MORE_ACTIONS: [],
PAGE_TYPE_DETAILS_MORE_ACTIONS: [],
PAGE_TYPE_OVERVIEW_CREATE: [],
PAGE_TYPE_OVERVIEW_MORE_ACTIONS: [],
MENU_DETAILS_MORE_ACTIONS: [],
MENU_OVERVIEW_CREATE: [],
MENU_OVERVIEW_MORE_ACTIONS: [],
COLLECTION_DETAILS_MORE_ACTIONS: [],
CATEGORY_DETAILS_MORE_ACTIONS: [],
GIFT_CARD_DETAILS_MORE_ACTIONS: [],
CATEGORY_OVERVIEW_CREATE: [],
CATEGORY_OVERVIEW_MORE_ACTIONS: [],
GIFT_CARD_OVERVIEW_CREATE: [],
GIFT_CARD_OVERVIEW_MORE_ACTIONS: [],
COLLECTION_OVERVIEW_CREATE: [],
COLLECTION_OVERVIEW_MORE_ACTIONS: [],
CUSTOMER_DETAILS_WIDGETS: [],
ORDER_DETAILS_WIDGETS: [],
COLLECTION_DETAILS_WIDGETS: [],
PRODUCT_DETAILS_WIDGETS: [],
VOUCHER_DETAILS_WIDGETS: [],
DRAFT_ORDER_DETAILS_WIDGETS: [],
GIFT_CARD_DETAILS_WIDGETS: [],
TRANSLATIONS_MORE_ACTIONS: [],
HOMEPAGE_WIDGETS: [],
};
it("should return the corresponding Extension object when a menu item ID represents a registered extension", () => {
const result = getMenuItemExtension(mockExtensionsRecord, "extension-test-extension");
expect(result).toBe(mockExtension);
});
it("should return undefined if a prefixed menu item ID (e.g., 'extension-non-existent') does not match any registered extension", () => {
const result = getMenuItemExtension(mockExtensionsRecord, "extension-non-existent");
expect(result).toBeUndefined();
});
it("should return undefined if the list of all available extensions is empty", () => {
const result = getMenuItemExtension(emptyExtensionsRecord, "extension-test-extension");
expect(result).toBeUndefined();
});
it("should find the specific extension when multiple extensions are registered within the same category (mount point)", () => {
const anotherExtension: Extension = {
...baseMockExtension,
id: "another-extension",
label: "Another Extension",
app: { ...mockAppDefinition, id: "app-2" },
url: "/another",
mountName: "NAVIGATION_CATALOG",
};
const extensionsWithMultiple = {
...emptyExtensionsRecord,
NAVIGATION_CATALOG: [mockExtension, anotherExtension],
};
const result = getMenuItemExtension(extensionsWithMultiple, "extension-another-extension");
expect(result).toBe(anotherExtension);
});
it("should find the specific extension even if it's registered in a different category (mount point) than other extensions", () => {
const catalogExtensionApp: Extension["app"] = {
__typename: "App",
id: "app-2",
appUrl: "https://app2.example.com",
name: "App name",
brand: null,
};
const catalogExtension: Extension = {
...baseMockExtension,
id: "catalog-ext",
label: "Catalog Extension",
app: catalogExtensionApp,
url: "/catalog-test",
mountName: "NAVIGATION_PAGES",
};
const extensionsWithMultipleMounts = {
...emptyExtensionsRecord,
NAVIGATION_CATALOG: [mockExtension],
NAVIGATION_PAGES: [catalogExtension],
};
const result = getMenuItemExtension(extensionsWithMultipleMounts, "extension-catalog-ext");
expect(result).toBe(catalogExtension);
});
it("should return undefined for a prefixed menu item ID (e.g., 'extension-does-not-exist') that doesn't match any registered extension", () => {
const result = getMenuItemExtension(mockExtensionsRecord, "extension-does-not-exist");
expect(result).toBeUndefined();
});
it("should return undefined for a menu item ID that is not prefixed with 'extension-', confirming it's not an extension type", () => {
const result = getMenuItemExtension(mockExtensionsRecord, "test-extension"); // Same id as mockExtension, but no prefix
expect(result).toBeUndefined();
});
});