|
1
|
import { Modules } from "@medusajs/utils" |
|
2
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { SettingsTypes } from "@medusajs/types" |
|
4
|
|
|
5
|
jest.setTimeout(30000) |
|
6
|
|
|
7
|
moduleIntegrationTestRunner<SettingsTypes.ISettingsModuleService>({ |
|
8
|
moduleName: Modules.SETTINGS, |
|
9
|
testSuite: ({ service }) => { |
|
10
|
describe("PropertyLabel", function () { |
|
11
|
describe("createPropertyLabels", function () { |
|
12
|
it("should create a property label", async () => { |
|
13
|
const label = await service.createPropertyLabels({ |
|
14
|
entity: "Order", |
|
15
|
property: "display_id", |
|
16
|
label: "Order Number", |
|
17
|
description: "The public-facing order identifier", |
|
18
|
}) |
|
19
|
|
|
20
|
expect(label).toEqual( |
|
21
|
expect.objectContaining({ |
|
22
|
id: expect.stringMatching(/^plbl_/), |
|
23
|
entity: "Order", |
|
24
|
property: "display_id", |
|
25
|
label: "Order Number", |
|
26
|
description: "The public-facing order identifier", |
|
27
|
created_at: expect.any(Date), |
|
28
|
updated_at: expect.any(Date), |
|
29
|
}) |
|
30
|
) |
|
31
|
}) |
|
32
|
|
|
33
|
it("should create a property label without description", async () => { |
|
34
|
const label = await service.createPropertyLabels({ |
|
35
|
entity: "Product", |
|
36
|
property: "title", |
|
37
|
label: "Product Name", |
|
38
|
}) |
|
39
|
|
|
40
|
expect(label).toEqual( |
|
41
|
expect.objectContaining({ |
|
42
|
id: expect.stringMatching(/^plbl_/), |
|
43
|
entity: "Product", |
|
44
|
property: "title", |
|
45
|
label: "Product Name", |
|
46
|
description: null, |
|
47
|
}) |
|
48
|
) |
|
49
|
}) |
|
50
|
|
|
51
|
it("should create multiple property labels", async () => { |
|
52
|
const labels = await service.createPropertyLabels([ |
|
53
|
{ |
|
54
|
entity: "Order", |
|
55
|
property: "status", |
|
56
|
label: "Order Status", |
|
57
|
}, |
|
58
|
{ |
|
59
|
entity: "Order", |
|
60
|
property: "total", |
|
61
|
label: "Order Total", |
|
62
|
}, |
|
63
|
{ |
|
64
|
entity: "Product", |
|
65
|
property: "handle", |
|
66
|
label: "URL Slug", |
|
67
|
}, |
|
68
|
]) |
|
69
|
|
|
70
|
expect(labels).toHaveLength(3) |
|
71
|
expect(labels).toEqual( |
|
72
|
expect.arrayContaining([ |
|
73
|
expect.objectContaining({ |
|
74
|
entity: "Order", |
|
75
|
property: "status", |
|
76
|
label: "Order Status", |
|
77
|
}), |
|
78
|
expect.objectContaining({ |
|
79
|
entity: "Order", |
|
80
|
property: "total", |
|
81
|
label: "Order Total", |
|
82
|
}), |
|
83
|
expect.objectContaining({ |
|
84
|
entity: "Product", |
|
85
|
property: "handle", |
|
86
|
label: "URL Slug", |
|
87
|
}), |
|
88
|
]) |
|
89
|
) |
|
90
|
}) |
|
91
|
|
|
92
|
it("should create labels for nested properties", async () => { |
|
93
|
const label = await service.createPropertyLabels({ |
|
94
|
entity: "Order", |
|
95
|
property: "customer.email", |
|
96
|
label: "Customer Email", |
|
97
|
}) |
|
98
|
|
|
99
|
expect(label).toEqual( |
|
100
|
expect.objectContaining({ |
|
101
|
entity: "Order", |
|
102
|
property: "customer.email", |
|
103
|
label: "Customer Email", |
|
104
|
}) |
|
105
|
) |
|
106
|
}) |
|
107
|
}) |
|
108
|
|
|
109
|
describe("listPropertyLabels", function () { |
|
110
|
it("should list all property labels", async () => { |
|
111
|
await service.createPropertyLabels([ |
|
112
|
{ entity: "Order", property: "id", label: "ID" }, |
|
113
|
{ entity: "Order", property: "status", label: "Status" }, |
|
114
|
{ entity: "Product", property: "title", label: "Title" }, |
|
115
|
]) |
|
116
|
|
|
117
|
const labels = await service.listPropertyLabels() |
|
118
|
|
|
119
|
expect(labels).toHaveLength(3) |
|
120
|
}) |
|
121
|
|
|
122
|
it("should filter property labels by entity", async () => { |
|
123
|
await service.createPropertyLabels([ |
|
124
|
{ entity: "Order", property: "id", label: "Order ID" }, |
|
125
|
{ entity: "Order", property: "status", label: "Order Status" }, |
|
126
|
{ entity: "Product", property: "id", label: "Product ID" }, |
|
127
|
]) |
|
128
|
|
|
129
|
const orderLabels = await service.listPropertyLabels({ |
|
130
|
entity: "Order", |
|
131
|
}) |
|
132
|
|
|
133
|
expect(orderLabels).toHaveLength(2) |
|
134
|
expect(orderLabels.every((l) => l.entity === "Order")).toBe(true) |
|
135
|
}) |
|
136
|
|
|
137
|
it("should filter property labels by property", async () => { |
|
138
|
await service.createPropertyLabels([ |
|
139
|
{ entity: "Order", property: "id", label: "Order ID" }, |
|
140
|
{ entity: "Product", property: "id", label: "Product ID" }, |
|
141
|
{ entity: "Customer", property: "email", label: "Email" }, |
|
142
|
]) |
|
143
|
|
|
144
|
const idLabels = await service.listPropertyLabels({ property: "id" }) |
|
145
|
|
|
146
|
expect(idLabels).toHaveLength(2) |
|
147
|
expect(idLabels.every((l) => l.property === "id")).toBe(true) |
|
148
|
}) |
|
149
|
|
|
150
|
it("should filter property labels by entity and property", async () => { |
|
151
|
await service.createPropertyLabels([ |
|
152
|
{ entity: "Order", property: "id", label: "Order ID" }, |
|
153
|
{ entity: "Product", property: "id", label: "Product ID" }, |
|
154
|
]) |
|
155
|
|
|
156
|
const labels = await service.listPropertyLabels({ |
|
157
|
entity: "Order", |
|
158
|
property: "id", |
|
159
|
}) |
|
160
|
|
|
161
|
expect(labels).toHaveLength(1) |
|
162
|
expect(labels[0]).toEqual( |
|
163
|
expect.objectContaining({ |
|
164
|
entity: "Order", |
|
165
|
property: "id", |
|
166
|
label: "Order ID", |
|
167
|
}) |
|
168
|
) |
|
169
|
}) |
|
170
|
}) |
|
171
|
|
|
172
|
describe("updatePropertyLabels", function () { |
|
173
|
it("should update a property label by ID", async () => { |
|
174
|
const label = await service.createPropertyLabels({ |
|
175
|
entity: "Order", |
|
176
|
property: "display_id", |
|
177
|
label: "Display ID", |
|
178
|
}) |
|
179
|
|
|
180
|
const [updated] = await service.updatePropertyLabels([ |
|
181
|
{ |
|
182
|
id: label.id, |
|
183
|
label: "Order Number", |
|
184
|
description: "The customer-facing order number", |
|
185
|
}, |
|
186
|
]) |
|
187
|
|
|
188
|
expect(updated.label).toBe("Order Number") |
|
189
|
expect(updated.description).toBe("The customer-facing order number") |
|
190
|
}) |
|
191
|
|
|
192
|
it("should update multiple property labels", async () => { |
|
193
|
const labels = await service.createPropertyLabels([ |
|
194
|
{ entity: "Order", property: "status", label: "Status" }, |
|
195
|
{ entity: "Order", property: "total", label: "Total" }, |
|
196
|
]) |
|
197
|
|
|
198
|
const updated = await service.updatePropertyLabels([ |
|
199
|
{ id: labels[0].id, label: "Order Status" }, |
|
200
|
{ id: labels[1].id, label: "Order Total" }, |
|
201
|
]) |
|
202
|
|
|
203
|
expect(updated).toHaveLength(2) |
|
204
|
expect(updated.find((l) => l.id === labels[0].id)?.label).toBe( |
|
205
|
"Order Status" |
|
206
|
) |
|
207
|
expect(updated.find((l) => l.id === labels[1].id)?.label).toBe( |
|
208
|
"Order Total" |
|
209
|
) |
|
210
|
}) |
|
211
|
|
|
212
|
it("should update only the provided fields", async () => { |
|
213
|
const label = await service.createPropertyLabels({ |
|
214
|
entity: "Product", |
|
215
|
property: "title", |
|
216
|
label: "Title", |
|
217
|
description: "Original description", |
|
218
|
}) |
|
219
|
|
|
220
|
const [updated] = await service.updatePropertyLabels([ |
|
221
|
{ id: label.id, label: "Product Name" }, |
|
222
|
]) |
|
223
|
|
|
224
|
expect(updated.label).toBe("Product Name") |
|
225
|
expect(updated.description).toBe("Original description") |
|
226
|
}) |
|
227
|
}) |
|
228
|
|
|
229
|
describe("deletePropertyLabels", function () { |
|
230
|
it("should delete a property label by ID", async () => { |
|
231
|
const label = await service.createPropertyLabels({ |
|
232
|
entity: "Order", |
|
233
|
property: "id", |
|
234
|
label: "Order ID", |
|
235
|
}) |
|
236
|
|
|
237
|
await service.deletePropertyLabels(label.id) |
|
238
|
|
|
239
|
const labels = await service.listPropertyLabels({ entity: "Order" }) |
|
240
|
expect(labels).toHaveLength(0) |
|
241
|
}) |
|
242
|
|
|
243
|
it("should delete multiple property labels by ID", async () => { |
|
244
|
const labels = await service.createPropertyLabels([ |
|
245
|
{ entity: "Order", property: "id", label: "Order ID" }, |
|
246
|
{ entity: "Order", property: "status", label: "Status" }, |
|
247
|
{ entity: "Product", property: "id", label: "Product ID" }, |
|
248
|
]) |
|
249
|
|
|
250
|
await service.deletePropertyLabels([labels[0].id, labels[1].id]) |
|
251
|
|
|
252
|
const remaining = await service.listPropertyLabels() |
|
253
|
expect(remaining).toHaveLength(1) |
|
254
|
expect(remaining[0].entity).toBe("Product") |
|
255
|
}) |
|
256
|
}) |
|
257
|
|
|
258
|
describe("listPropertyLabels", function () { |
|
259
|
it("should list all labels", async () => { |
|
260
|
await service.createPropertyLabels([ |
|
261
|
{ entity: "Order", property: "id", label: "Order ID" }, |
|
262
|
{ entity: "Order", property: "status", label: "Order Status" }, |
|
263
|
{ entity: "Product", property: "id", label: "Product ID" }, |
|
264
|
]) |
|
265
|
|
|
266
|
const labels = await service.listPropertyLabels() |
|
267
|
|
|
268
|
expect(labels).toHaveLength(3) |
|
269
|
}) |
|
270
|
}) |
|
271
|
|
|
272
|
describe("retrievePropertyLabel", function () { |
|
273
|
it("should retrieve a specific label by id", async () => { |
|
274
|
const label = await service.createPropertyLabels({ |
|
275
|
entity: "Order", |
|
276
|
property: "display_id", |
|
277
|
label: "Order Number", |
|
278
|
}) |
|
279
|
|
|
280
|
const retrievedLabel = await service.retrievePropertyLabel(label.id) |
|
281
|
|
|
282
|
expect(retrievedLabel).toEqual( |
|
283
|
expect.objectContaining({ |
|
284
|
id: label.id, |
|
285
|
}) |
|
286
|
) |
|
287
|
}) |
|
288
|
}) |
|
289
|
|
|
290
|
describe("upsertPropertyLabels", function () { |
|
291
|
it("should upsert a new label", async () => { |
|
292
|
const labels = await service.upsertPropertyLabels([ |
|
293
|
{ |
|
294
|
entity: "Order", |
|
295
|
property: "display_id", |
|
296
|
label: "Order Number", |
|
297
|
}, |
|
298
|
]) |
|
299
|
|
|
300
|
expect(labels[0]).toEqual( |
|
301
|
expect.objectContaining({ |
|
302
|
entity: "Order", |
|
303
|
property: "display_id", |
|
304
|
label: "Order Number", |
|
305
|
}) |
|
306
|
) |
|
307
|
}) |
|
308
|
|
|
309
|
it("should update an existing label", async () => { |
|
310
|
const label = await service.createPropertyLabels({ |
|
311
|
entity: "Order", |
|
312
|
property: "display_id", |
|
313
|
label: "Order Number", |
|
314
|
}) |
|
315
|
|
|
316
|
const upsertedLabels = await service.upsertPropertyLabels([ |
|
317
|
{ |
|
318
|
id: label.id, |
|
319
|
label: "Updated Order Number", |
|
320
|
description: "Updated description", |
|
321
|
}, |
|
322
|
]) |
|
323
|
|
|
324
|
expect(upsertedLabels[0].label).toBe("Updated Order Number") |
|
325
|
expect(upsertedLabels[0].description).toBe("Updated description") |
|
326
|
|
|
327
|
// Verify only one label exists |
|
328
|
const labels = await service.listPropertyLabels({ |
|
329
|
entity: "Order", |
|
330
|
property: "display_id", |
|
331
|
}) |
|
332
|
expect(labels).toHaveLength(1) |
|
333
|
}) |
|
334
|
}) |
|
335
|
|
|
336
|
describe("deletePropertyLabel", function () { |
|
337
|
it("should delete a label by entity and property", async () => { |
|
338
|
const label = await service.createPropertyLabels({ |
|
339
|
entity: "Order", |
|
340
|
property: "display_id", |
|
341
|
label: "Order Number", |
|
342
|
}) |
|
343
|
|
|
344
|
await service.deletePropertyLabels(label.id) |
|
345
|
|
|
346
|
const labels = await service.listPropertyLabels({ |
|
347
|
entity: "Order", |
|
348
|
property: "display_id", |
|
349
|
}) |
|
350
|
expect(labels).toHaveLength(0) |
|
351
|
}) |
|
352
|
|
|
353
|
it("should not throw if label does not exist", async () => { |
|
354
|
await expect( |
|
355
|
service.deletePropertyLabels(["nonexistent"]) |
|
356
|
).resolves.not.toThrow() |
|
357
|
}) |
|
358
|
}) |
|
359
|
|
|
360
|
describe("PropertyLabelDTO shape validation", function () { |
|
361
|
it("should have correct PropertyLabelDTO shape", async () => { |
|
362
|
const label = await service.createPropertyLabels({ |
|
363
|
entity: "Order", |
|
364
|
property: "display_id", |
|
365
|
label: "Order Number", |
|
366
|
description: "The order identifier", |
|
367
|
}) |
|
368
|
|
|
369
|
// Validate all expected fields exist |
|
370
|
expect(label).toHaveProperty("id") |
|
371
|
expect(label).toHaveProperty("entity") |
|
372
|
expect(label).toHaveProperty("property") |
|
373
|
expect(label).toHaveProperty("label") |
|
374
|
expect(label).toHaveProperty("description") |
|
375
|
expect(label).toHaveProperty("created_at") |
|
376
|
expect(label).toHaveProperty("updated_at") |
|
377
|
|
|
378
|
// Validate types |
|
379
|
expect(typeof label.id).toBe("string") |
|
380
|
expect(typeof label.entity).toBe("string") |
|
381
|
expect(typeof label.property).toBe("string") |
|
382
|
expect(typeof label.label).toBe("string") |
|
383
|
expect( |
|
384
|
label.description === null || typeof label.description === "string" |
|
385
|
).toBe(true) |
|
386
|
expect(label.created_at).toBeInstanceOf(Date) |
|
387
|
expect(label.updated_at).toBeInstanceOf(Date) |
|
388
|
|
|
389
|
// Validate ID prefix |
|
390
|
expect(label.id).toMatch(/^plbl_/) |
|
391
|
}) |
|
392
|
}) |
|
393
|
}) |
|
394
|
}, |
|
395
|
}) |