|
1
|
import { Product, ProductCategory, ProductCollection } from "@models" |
|
2
|
import { |
|
3
|
assignCategoriesToProduct, |
|
4
|
buildProductOnlyData, |
|
5
|
createCollections, |
|
6
|
createProductAndTags, |
|
7
|
createProductVariants, |
|
8
|
} from "../__fixtures__/product" |
|
9
|
|
|
10
|
import { |
|
11
|
InferEntityType, |
|
12
|
IProductModuleService, |
|
13
|
ProductDTO, |
|
14
|
} from "@medusajs/framework/types" |
|
15
|
import { |
|
16
|
kebabCase, |
|
17
|
Module, |
|
18
|
Modules, |
|
19
|
ProductStatus, |
|
20
|
toMikroORMEntity, |
|
21
|
} from "@medusajs/framework/utils" |
|
22
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
23
|
import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql" |
|
24
|
import { |
|
25
|
ProductCategoryService, |
|
26
|
ProductModuleService, |
|
27
|
ProductService, |
|
28
|
} from "@services" |
|
29
|
import { |
|
30
|
categoriesData, |
|
31
|
productsData, |
|
32
|
variantsData, |
|
33
|
} from "../__fixtures__/product/data" |
|
34
|
|
|
35
|
jest.setTimeout(300000) |
|
36
|
|
|
37
|
type Service = IProductModuleService & { |
|
38
|
productService_: ProductService |
|
39
|
productCategoryService_: ProductCategoryService |
|
40
|
} |
|
41
|
|
|
42
|
moduleIntegrationTestRunner<Service>({ |
|
43
|
moduleName: Modules.PRODUCT, |
|
44
|
testSuite: ({ MikroOrmWrapper, service: moduleService }) => { |
|
45
|
let service: ProductService |
|
46
|
let categoryService: ProductCategoryService |
|
47
|
|
|
48
|
beforeEach(() => { |
|
49
|
service = moduleService.productService_ |
|
50
|
categoryService = moduleService.productCategoryService_ |
|
51
|
}) |
|
52
|
|
|
53
|
it(`should export the appropriate linkable configuration`, () => { |
|
54
|
const linkable = Module(Modules.PRODUCT, { |
|
55
|
service: ProductModuleService, |
|
56
|
}).linkable |
|
57
|
|
|
58
|
expect(Object.keys(linkable)).toHaveLength(9) |
|
59
|
expect(Object.keys(linkable)).toEqual( |
|
60
|
expect.arrayContaining([ |
|
61
|
"product", |
|
62
|
"productVariant", |
|
63
|
"productOption", |
|
64
|
"productOptionValue", |
|
65
|
"productType", |
|
66
|
"productTag", |
|
67
|
"productCollection", |
|
68
|
"productCategory", |
|
69
|
"productImage", |
|
70
|
]) |
|
71
|
) |
|
72
|
|
|
73
|
Object.keys(linkable).forEach((key) => { |
|
74
|
delete linkable[key].toJSON |
|
75
|
}) |
|
76
|
|
|
77
|
expect(linkable).toEqual({ |
|
78
|
product: { |
|
79
|
id: { |
|
80
|
linkable: "product_id", |
|
81
|
entity: "Product", |
|
82
|
primaryKey: "id", |
|
83
|
serviceName: "product", |
|
84
|
field: "product", |
|
85
|
}, |
|
86
|
}, |
|
87
|
productVariant: { |
|
88
|
id: { |
|
89
|
linkable: "product_variant_id", |
|
90
|
entity: "ProductVariant", |
|
91
|
primaryKey: "id", |
|
92
|
serviceName: "product", |
|
93
|
field: "productVariant", |
|
94
|
}, |
|
95
|
variant_id: { |
|
96
|
field: "productVariant", |
|
97
|
entity: "ProductVariant", |
|
98
|
linkable: "variant_id", |
|
99
|
primaryKey: "variant_id", |
|
100
|
serviceName: "product", |
|
101
|
}, |
|
102
|
}, |
|
103
|
productOption: { |
|
104
|
id: { |
|
105
|
linkable: "product_option_id", |
|
106
|
entity: "ProductOption", |
|
107
|
primaryKey: "id", |
|
108
|
serviceName: "product", |
|
109
|
field: "productOption", |
|
110
|
}, |
|
111
|
}, |
|
112
|
productOptionValue: { |
|
113
|
id: { |
|
114
|
linkable: "product_option_value_id", |
|
115
|
entity: "ProductOptionValue", |
|
116
|
primaryKey: "id", |
|
117
|
serviceName: "product", |
|
118
|
field: "productOptionValue", |
|
119
|
}, |
|
120
|
}, |
|
121
|
productType: { |
|
122
|
id: { |
|
123
|
linkable: "product_type_id", |
|
124
|
entity: "ProductType", |
|
125
|
primaryKey: "id", |
|
126
|
serviceName: "product", |
|
127
|
field: "productType", |
|
128
|
}, |
|
129
|
}, |
|
130
|
productTag: { |
|
131
|
id: { |
|
132
|
linkable: "product_tag_id", |
|
133
|
entity: "ProductTag", |
|
134
|
primaryKey: "id", |
|
135
|
serviceName: "product", |
|
136
|
field: "productTag", |
|
137
|
}, |
|
138
|
}, |
|
139
|
productCollection: { |
|
140
|
id: { |
|
141
|
linkable: "product_collection_id", |
|
142
|
entity: "ProductCollection", |
|
143
|
primaryKey: "id", |
|
144
|
serviceName: "product", |
|
145
|
field: "productCollection", |
|
146
|
}, |
|
147
|
}, |
|
148
|
productCategory: { |
|
149
|
id: { |
|
150
|
linkable: "product_category_id", |
|
151
|
entity: "ProductCategory", |
|
152
|
primaryKey: "id", |
|
153
|
serviceName: "product", |
|
154
|
field: "productCategory", |
|
155
|
}, |
|
156
|
}, |
|
157
|
productImage: { |
|
158
|
id: { |
|
159
|
entity: "ProductImage", |
|
160
|
field: "productImage", |
|
161
|
linkable: "product_image_id", |
|
162
|
primaryKey: "id", |
|
163
|
serviceName: "product", |
|
164
|
}, |
|
165
|
}, |
|
166
|
}) |
|
167
|
}) |
|
168
|
|
|
169
|
describe("Product Service", () => { |
|
170
|
let testManager: SqlEntityManager |
|
171
|
let products!: InferEntityType<typeof Product>[] |
|
172
|
let productOne: InferEntityType<typeof Product> |
|
173
|
let categories!: InferEntityType<typeof ProductCategory>[] |
|
174
|
|
|
175
|
describe("retrieve", () => { |
|
176
|
beforeEach(async () => { |
|
177
|
testManager = await MikroOrmWrapper.forkManager() |
|
178
|
productOne = testManager.create(toMikroORMEntity(Product), { |
|
179
|
id: "product-1", |
|
180
|
title: "product 1", |
|
181
|
handle: "product-1", |
|
182
|
status: ProductStatus.PUBLISHED, |
|
183
|
}) |
|
184
|
|
|
185
|
await testManager.persistAndFlush([productOne]) |
|
186
|
}) |
|
187
|
|
|
188
|
it("should throw an error when an id is not provided", async () => { |
|
189
|
let error |
|
190
|
|
|
191
|
try { |
|
192
|
await service.retrieve(undefined as unknown as string) |
|
193
|
} catch (e) { |
|
194
|
error = e |
|
195
|
} |
|
196
|
|
|
197
|
expect(error.message).toEqual("product - id must be defined") |
|
198
|
}) |
|
199
|
|
|
200
|
it("should throw an error when product with id does not exist", async () => { |
|
201
|
let error |
|
202
|
|
|
203
|
try { |
|
204
|
await service.retrieve("does-not-exist") |
|
205
|
} catch (e) { |
|
206
|
error = e |
|
207
|
} |
|
208
|
|
|
209
|
expect(error.message).toEqual( |
|
210
|
"Product with id: does-not-exist was not found" |
|
211
|
) |
|
212
|
}) |
|
213
|
|
|
214
|
it("should return a product when product with an id exists", async () => { |
|
215
|
const result = await service.retrieve(productOne.id) |
|
216
|
|
|
217
|
expect(result).toEqual( |
|
218
|
expect.objectContaining({ |
|
219
|
id: productOne.id, |
|
220
|
}) |
|
221
|
) |
|
222
|
}) |
|
223
|
}) |
|
224
|
|
|
225
|
describe("create", function () { |
|
226
|
beforeEach(async () => { |
|
227
|
testManager = await MikroOrmWrapper.forkManager() |
|
228
|
}) |
|
229
|
|
|
230
|
it("should create a product", async () => { |
|
231
|
const data = buildProductOnlyData() |
|
232
|
|
|
233
|
const products = await service.create([data]) |
|
234
|
|
|
235
|
expect(products).toHaveLength(1) |
|
236
|
expect(JSON.parse(JSON.stringify(products[0]))).toEqual( |
|
237
|
expect.objectContaining({ |
|
238
|
id: expect.any(String), |
|
239
|
title: data.title, |
|
240
|
handle: kebabCase(data.title), |
|
241
|
description: data.description, |
|
242
|
subtitle: data.subtitle, |
|
243
|
is_giftcard: data.is_giftcard, |
|
244
|
discountable: data.discountable, |
|
245
|
status: data.status, |
|
246
|
}) |
|
247
|
) |
|
248
|
}) |
|
249
|
}) |
|
250
|
|
|
251
|
describe("update", function () { |
|
252
|
beforeEach(async () => { |
|
253
|
testManager = await MikroOrmWrapper.forkManager() |
|
254
|
|
|
255
|
productOne = testManager.create(toMikroORMEntity(Product), { |
|
256
|
id: "product-1", |
|
257
|
title: "product 1", |
|
258
|
handle: "product-1", |
|
259
|
status: ProductStatus.PUBLISHED, |
|
260
|
}) |
|
261
|
|
|
262
|
await testManager.persistAndFlush([productOne]) |
|
263
|
}) |
|
264
|
|
|
265
|
it("should update a product and its allowed relations", async () => { |
|
266
|
const updateData = [ |
|
267
|
{ |
|
268
|
id: productOne.id, |
|
269
|
title: "update test 1", |
|
270
|
}, |
|
271
|
] |
|
272
|
|
|
273
|
const products = await service.update(updateData) |
|
274
|
|
|
275
|
expect(products.length).toEqual(1) |
|
276
|
|
|
277
|
let result = await service.retrieve(productOne.id) |
|
278
|
let serialized = JSON.parse(JSON.stringify(result)) |
|
279
|
|
|
280
|
expect(serialized).toEqual( |
|
281
|
expect.objectContaining({ |
|
282
|
id: productOne.id, |
|
283
|
title: "update test 1", |
|
284
|
}) |
|
285
|
) |
|
286
|
}) |
|
287
|
|
|
288
|
it("should update a product and its allowed relations using selector", async () => { |
|
289
|
const updateData = [ |
|
290
|
{ |
|
291
|
selector: { |
|
292
|
id: productOne.id, |
|
293
|
}, |
|
294
|
data: { |
|
295
|
title: "update test 1", |
|
296
|
}, |
|
297
|
}, |
|
298
|
] |
|
299
|
|
|
300
|
const products = await service.update(updateData) |
|
301
|
|
|
302
|
expect(products.length).toEqual(1) |
|
303
|
|
|
304
|
let result = await service.retrieve(productOne.id) |
|
305
|
let serialized = JSON.parse(JSON.stringify(result)) |
|
306
|
|
|
307
|
expect(serialized).toEqual( |
|
308
|
expect.objectContaining({ |
|
309
|
id: productOne.id, |
|
310
|
title: "update test 1", |
|
311
|
}) |
|
312
|
) |
|
313
|
}) |
|
314
|
|
|
315
|
it("should update a single product and its allowed relations", async () => { |
|
316
|
const updateData = { |
|
317
|
id: productOne.id, |
|
318
|
title: "update test 1", |
|
319
|
} |
|
320
|
|
|
321
|
const product = await service.update(updateData) |
|
322
|
|
|
323
|
expect(product).toEqual( |
|
324
|
expect.objectContaining({ |
|
325
|
id: productOne.id, |
|
326
|
title: "update test 1", |
|
327
|
}) |
|
328
|
) |
|
329
|
|
|
330
|
let result = await service.retrieve(productOne.id) |
|
331
|
let serialized = JSON.parse(JSON.stringify(result)) |
|
332
|
|
|
333
|
expect(serialized).toEqual( |
|
334
|
expect.objectContaining({ |
|
335
|
id: productOne.id, |
|
336
|
title: "update test 1", |
|
337
|
}) |
|
338
|
) |
|
339
|
}) |
|
340
|
|
|
341
|
it("should throw an error when id is not present", async () => { |
|
342
|
let error |
|
343
|
const updateData = [ |
|
344
|
{ |
|
345
|
id: productOne.id, |
|
346
|
title: "update test 1", |
|
347
|
}, |
|
348
|
{ |
|
349
|
id: undefined as unknown as string, |
|
350
|
title: "update test 2", |
|
351
|
}, |
|
352
|
] |
|
353
|
|
|
354
|
try { |
|
355
|
await service.update(updateData) |
|
356
|
} catch (e) { |
|
357
|
error = e |
|
358
|
} |
|
359
|
|
|
360
|
expect(error.message).toEqual(`Product with id "" not found`) |
|
361
|
|
|
362
|
let result = await service.retrieve(productOne.id) |
|
363
|
|
|
364
|
expect(result.title).not.toBe("update test 1") |
|
365
|
}) |
|
366
|
|
|
367
|
it("should throw an error when product with id does not exist", async () => { |
|
368
|
let error |
|
369
|
const updateData = [ |
|
370
|
{ |
|
371
|
id: "does-not-exist", |
|
372
|
title: "update test 1", |
|
373
|
}, |
|
374
|
] |
|
375
|
|
|
376
|
try { |
|
377
|
await service.update(updateData) |
|
378
|
} catch (e) { |
|
379
|
error = e |
|
380
|
} |
|
381
|
|
|
382
|
expect(error.message).toEqual( |
|
383
|
`Product with id "does-not-exist" not found` |
|
384
|
) |
|
385
|
}) |
|
386
|
}) |
|
387
|
|
|
388
|
describe("list", () => { |
|
389
|
it("should list all product that match the free text search", async () => { |
|
390
|
const data = buildProductOnlyData({ |
|
391
|
title: "test product", |
|
392
|
}) |
|
393
|
const data2 = buildProductOnlyData({ |
|
394
|
title: "space X", |
|
395
|
}) |
|
396
|
|
|
397
|
const products = await service.create([data, data2]) |
|
398
|
|
|
399
|
const result = await service.list({ |
|
400
|
q: "test", |
|
401
|
}) |
|
402
|
|
|
403
|
expect(result).toHaveLength(1) |
|
404
|
expect(result[0].title).toEqual("test product") |
|
405
|
|
|
406
|
const result2 = await service.list({ |
|
407
|
q: "space", |
|
408
|
}) |
|
409
|
|
|
410
|
expect(result2).toHaveLength(1) |
|
411
|
expect(result2[0].title).toEqual("space X") |
|
412
|
}) |
|
413
|
|
|
414
|
describe("soft deleted", function () { |
|
415
|
let product |
|
416
|
|
|
417
|
beforeEach(async () => { |
|
418
|
testManager = await MikroOrmWrapper.forkManager() |
|
419
|
|
|
420
|
const products = await createProductAndTags( |
|
421
|
testManager, |
|
422
|
productsData |
|
423
|
) |
|
424
|
|
|
425
|
product = products[1] |
|
426
|
await service.softDelete([products[0].id]) |
|
427
|
}) |
|
428
|
|
|
429
|
it("should list all products that are not deleted", async () => { |
|
430
|
const products = await service.list() |
|
431
|
|
|
432
|
expect(products).toHaveLength(2) |
|
433
|
expect(products[0].id).toEqual(product.id) |
|
434
|
}) |
|
435
|
|
|
436
|
it("should list all products including the deleted", async () => { |
|
437
|
const products = await service.list({}, { withDeleted: true }) |
|
438
|
|
|
439
|
expect(products).toHaveLength(3) |
|
440
|
}) |
|
441
|
}) |
|
442
|
|
|
443
|
describe("relation: tags", () => { |
|
444
|
beforeEach(async () => { |
|
445
|
testManager = await MikroOrmWrapper.forkManager() |
|
446
|
|
|
447
|
products = await createProductAndTags(testManager, productsData) |
|
448
|
}) |
|
449
|
|
|
450
|
it("should filter by id and including relations", async () => { |
|
451
|
const productsResult = await service.list( |
|
452
|
{ |
|
453
|
id: products[0].id, |
|
454
|
}, |
|
455
|
{ |
|
456
|
relations: ["tags"], |
|
457
|
} |
|
458
|
) |
|
459
|
|
|
460
|
productsResult.forEach((product, index) => { |
|
461
|
const tags = product.tags.toArray() |
|
462
|
|
|
463
|
expect(product).toEqual( |
|
464
|
expect.objectContaining({ |
|
465
|
id: productsData[index].id, |
|
466
|
title: productsData[index].title, |
|
467
|
}) |
|
468
|
) |
|
469
|
|
|
470
|
tags.forEach((tag, tagIndex) => { |
|
471
|
expect(tag).toEqual( |
|
472
|
expect.objectContaining({ |
|
473
|
...productsData[index].tags[tagIndex], |
|
474
|
}) |
|
475
|
) |
|
476
|
}) |
|
477
|
}) |
|
478
|
}) |
|
479
|
|
|
480
|
it("should filter by id and without relations", async () => { |
|
481
|
const productsResult = await service.list({ |
|
482
|
id: products[0].id, |
|
483
|
}) |
|
484
|
|
|
485
|
productsResult.forEach((product, index) => { |
|
486
|
const tags = product.tags.getItems(false) |
|
487
|
|
|
488
|
expect(product).toEqual( |
|
489
|
expect.objectContaining({ |
|
490
|
id: productsData[index].id, |
|
491
|
title: productsData[index].title, |
|
492
|
}) |
|
493
|
) |
|
494
|
|
|
495
|
expect(tags.length).toBe(0) |
|
496
|
}) |
|
497
|
}) |
|
498
|
}) |
|
499
|
|
|
500
|
describe("relation: categories", () => { |
|
501
|
let workingProduct: InferEntityType<typeof Product> |
|
502
|
let workingCategory: InferEntityType<typeof ProductCategory> |
|
503
|
|
|
504
|
beforeEach(async () => { |
|
505
|
testManager = await MikroOrmWrapper.forkManager() |
|
506
|
|
|
507
|
products = await createProductAndTags(testManager, productsData) |
|
508
|
workingProduct = products.find((p) => p.id === "test-1")! |
|
509
|
categories = [] |
|
510
|
for (const entry of categoriesData) { |
|
511
|
categories.push((await categoryService.create([entry]))[0]) |
|
512
|
} |
|
513
|
|
|
514
|
workingCategory = (await testManager.findOne( |
|
515
|
toMikroORMEntity(ProductCategory), |
|
516
|
"category-1" |
|
517
|
))! |
|
518
|
|
|
519
|
workingProduct = await assignCategoriesToProduct( |
|
520
|
testManager, |
|
521
|
workingProduct, |
|
522
|
categories |
|
523
|
) |
|
524
|
}) |
|
525
|
|
|
526
|
it("should filter by categories relation and scope fields", async () => { |
|
527
|
const products = await service.list( |
|
528
|
{ |
|
529
|
id: workingProduct.id, |
|
530
|
categories: { id: [workingCategory.id] }, |
|
531
|
}, |
|
532
|
{ |
|
533
|
select: [ |
|
534
|
"title", |
|
535
|
"categories.name", |
|
536
|
"categories.handle", |
|
537
|
"categories.mpath", |
|
538
|
] as (keyof ProductDTO)[], |
|
539
|
relations: ["categories"], |
|
540
|
} |
|
541
|
) |
|
542
|
|
|
543
|
const product = products.find((p) => p.id === workingProduct.id)! |
|
544
|
|
|
545
|
expect(product).toEqual( |
|
546
|
expect.objectContaining({ |
|
547
|
id: workingProduct.id, |
|
548
|
title: workingProduct.title, |
|
549
|
}) |
|
550
|
) |
|
551
|
|
|
552
|
const categories = product.categories.toArray() |
|
553
|
expect(categories).toHaveLength(3) |
|
554
|
expect(categories).toEqual( |
|
555
|
expect.arrayContaining([ |
|
556
|
{ |
|
557
|
id: "category-0", |
|
558
|
name: "category 0", |
|
559
|
handle: "category-0", |
|
560
|
mpath: "category-0", |
|
561
|
}, |
|
562
|
{ |
|
563
|
id: "category-1", |
|
564
|
name: "category 1", |
|
565
|
handle: "category-1", |
|
566
|
mpath: "category-0.category-1", |
|
567
|
}, |
|
568
|
{ |
|
569
|
id: "category-1-a", |
|
570
|
name: "category 1 a", |
|
571
|
handle: "category-1-a", |
|
572
|
mpath: "category-0.category-1.category-1-a", |
|
573
|
}, |
|
574
|
]) |
|
575
|
) |
|
576
|
}) |
|
577
|
|
|
578
|
it("should returns empty array when querying for a category that doesnt exist", async () => { |
|
579
|
const products = await service.list( |
|
580
|
{ |
|
581
|
id: workingProduct.id, |
|
582
|
categories: { id: ["category-doesnt-exist-id"] }, |
|
583
|
}, |
|
584
|
{ |
|
585
|
select: [ |
|
586
|
"title", |
|
587
|
"categories.name", |
|
588
|
"categories.handle", |
|
589
|
] as (keyof ProductDTO)[], |
|
590
|
relations: ["categories"], |
|
591
|
} |
|
592
|
) |
|
593
|
|
|
594
|
expect(products).toEqual([]) |
|
595
|
}) |
|
596
|
}) |
|
597
|
|
|
598
|
describe("relation: collections", () => { |
|
599
|
let workingProduct: InferEntityType<typeof Product> |
|
600
|
let workingProductTwo: InferEntityType<typeof Product> |
|
601
|
let workingCollection: InferEntityType<typeof ProductCollection> |
|
602
|
let workingCollectionTwo: InferEntityType<typeof ProductCollection> |
|
603
|
const collectionData = [ |
|
604
|
{ |
|
605
|
id: "test-1", |
|
606
|
title: "col 1", |
|
607
|
handle: "col-1", |
|
608
|
}, |
|
609
|
{ |
|
610
|
id: "test-2", |
|
611
|
title: "col 2", |
|
612
|
handle: "col-2", |
|
613
|
}, |
|
614
|
] |
|
615
|
|
|
616
|
beforeEach(async () => { |
|
617
|
testManager = await MikroOrmWrapper.forkManager() |
|
618
|
await createCollections(testManager, collectionData) |
|
619
|
workingCollection = await testManager.findOne( |
|
620
|
toMikroORMEntity(ProductCollection), |
|
621
|
"test-1" |
|
622
|
) |
|
623
|
workingCollectionTwo = (await testManager.findOne( |
|
624
|
toMikroORMEntity(ProductCollection), |
|
625
|
"test-2" |
|
626
|
))! |
|
627
|
|
|
628
|
products = await createProductAndTags(testManager, [ |
|
629
|
{ |
|
630
|
...productsData[0], |
|
631
|
collection_id: workingCollection.id, |
|
632
|
}, |
|
633
|
{ |
|
634
|
...productsData[1], |
|
635
|
collection_id: workingCollectionTwo.id, |
|
636
|
}, |
|
637
|
{ |
|
638
|
...productsData[2], |
|
639
|
}, |
|
640
|
]) |
|
641
|
|
|
642
|
workingProduct = products.find((p) => p.id === "test-1")! |
|
643
|
workingProductTwo = products.find((p) => p.id === "test-2")! |
|
644
|
}) |
|
645
|
|
|
646
|
it("should filter by collection relation and scope fields", async () => { |
|
647
|
const products = await moduleService.listProducts( |
|
648
|
{ |
|
649
|
id: workingProduct.id, |
|
650
|
collection_id: workingCollection.id, |
|
651
|
}, |
|
652
|
{ |
|
653
|
select: [ |
|
654
|
"title", |
|
655
|
"handle", |
|
656
|
"collection.title", |
|
657
|
"collection.handle", |
|
658
|
], |
|
659
|
relations: ["collection"], |
|
660
|
} |
|
661
|
) |
|
662
|
|
|
663
|
expect(products.length).toEqual(1) |
|
664
|
expect(products).toEqual([ |
|
665
|
{ |
|
666
|
id: workingProduct.id, |
|
667
|
title: workingProduct.title, |
|
668
|
handle: "product-1", |
|
669
|
collection_id: workingCollection.id, |
|
670
|
collection: { |
|
671
|
handle: "col-1", |
|
672
|
id: workingCollection.id, |
|
673
|
title: workingCollection.title, |
|
674
|
}, |
|
675
|
}, |
|
676
|
]) |
|
677
|
}) |
|
678
|
|
|
679
|
it("should filter by collection when multiple collection ids are passed", async () => { |
|
680
|
const products = await moduleService.listProducts( |
|
681
|
{ |
|
682
|
collection_id: [workingCollection.id, workingCollectionTwo.id], |
|
683
|
}, |
|
684
|
{ |
|
685
|
select: [ |
|
686
|
"title", |
|
687
|
"handle", |
|
688
|
"collection.title", |
|
689
|
"collection.handle", |
|
690
|
], |
|
691
|
relations: ["collection"], |
|
692
|
} |
|
693
|
) |
|
694
|
|
|
695
|
expect(products.length).toEqual(2) |
|
696
|
expect(products).toEqual([ |
|
697
|
{ |
|
698
|
id: workingProduct.id, |
|
699
|
title: workingProduct.title, |
|
700
|
handle: "product-1", |
|
701
|
collection_id: workingCollection.id, |
|
702
|
collection: { |
|
703
|
handle: "col-1", |
|
704
|
id: workingCollection.id, |
|
705
|
title: workingCollection.title, |
|
706
|
}, |
|
707
|
}, |
|
708
|
{ |
|
709
|
id: workingProductTwo.id, |
|
710
|
title: workingProductTwo.title, |
|
711
|
handle: "product", |
|
712
|
collection_id: workingCollectionTwo.id, |
|
713
|
collection: { |
|
714
|
handle: "col-2", |
|
715
|
id: workingCollectionTwo.id, |
|
716
|
title: workingCollectionTwo.title, |
|
717
|
}, |
|
718
|
}, |
|
719
|
]) |
|
720
|
}) |
|
721
|
|
|
722
|
it("should returns empty array when querying for a collection that doesnt exist", async () => { |
|
723
|
const products = await service.list( |
|
724
|
{ |
|
725
|
id: workingProduct.id, |
|
726
|
collection_id: "collection-doesnt-exist-id", |
|
727
|
}, |
|
728
|
{ |
|
729
|
select: ["title", "collection.title"] as (keyof ProductDTO)[], |
|
730
|
relations: ["collection"], |
|
731
|
} |
|
732
|
) |
|
733
|
|
|
734
|
expect(products).toEqual([]) |
|
735
|
}) |
|
736
|
}) |
|
737
|
|
|
738
|
describe("relation: variants", () => { |
|
739
|
beforeEach(async () => { |
|
740
|
testManager = await MikroOrmWrapper.forkManager() |
|
741
|
|
|
742
|
products = await createProductAndTags(testManager, productsData) |
|
743
|
await createProductVariants(testManager, variantsData) |
|
744
|
}) |
|
745
|
|
|
746
|
it("should filter by id and including relations", async () => { |
|
747
|
const productsResult = await service.list( |
|
748
|
{ |
|
749
|
id: products[0].id, |
|
750
|
}, |
|
751
|
{ |
|
752
|
relations: ["variants"], |
|
753
|
} |
|
754
|
) |
|
755
|
|
|
756
|
productsResult.forEach((product, index) => { |
|
757
|
const variants = product.variants.toArray() |
|
758
|
|
|
759
|
expect(product).toEqual( |
|
760
|
expect.objectContaining({ |
|
761
|
id: productsData[index].id, |
|
762
|
title: productsData[index].title, |
|
763
|
}) |
|
764
|
) |
|
765
|
|
|
766
|
variants.forEach((variant, variantIndex) => { |
|
767
|
const expectedVariant = variantsData.filter( |
|
768
|
(d) => d.product.id === product.id |
|
769
|
)[variantIndex] |
|
770
|
|
|
771
|
const variantProduct = variant.product |
|
772
|
|
|
773
|
expect(variant).toEqual( |
|
774
|
expect.objectContaining({ |
|
775
|
id: expectedVariant.id, |
|
776
|
sku: expectedVariant.sku, |
|
777
|
title: expectedVariant.title, |
|
778
|
}) |
|
779
|
) |
|
780
|
}) |
|
781
|
}) |
|
782
|
}) |
|
783
|
}) |
|
784
|
}) |
|
785
|
|
|
786
|
describe("softDelete", function () { |
|
787
|
beforeEach(async () => { |
|
788
|
testManager = await MikroOrmWrapper.forkManager() |
|
789
|
}) |
|
790
|
|
|
791
|
it("should soft delete a product", async () => { |
|
792
|
const data = buildProductOnlyData() |
|
793
|
|
|
794
|
const products = await service.create([data]) |
|
795
|
await service.softDelete(products.map((p) => p.id)) |
|
796
|
const deleteProducts = await service.list( |
|
797
|
{ id: products.map((p) => p.id) }, |
|
798
|
{ |
|
799
|
relations: [ |
|
800
|
"variants", |
|
801
|
"variants.options", |
|
802
|
"options", |
|
803
|
"options.values", |
|
804
|
], |
|
805
|
withDeleted: true, |
|
806
|
} |
|
807
|
) |
|
808
|
|
|
809
|
expect(deleteProducts).toHaveLength(1) |
|
810
|
expect(deleteProducts[0].deleted_at).not.toBeNull() |
|
811
|
}) |
|
812
|
}) |
|
813
|
|
|
814
|
describe("restore", function () { |
|
815
|
beforeEach(async () => { |
|
816
|
testManager = await MikroOrmWrapper.forkManager() |
|
817
|
}) |
|
818
|
|
|
819
|
it("should restore a soft deleted product", async () => { |
|
820
|
const data = buildProductOnlyData() |
|
821
|
|
|
822
|
const products = await service.create([data]) |
|
823
|
const product = products[0] |
|
824
|
await service.softDelete([product.id]) |
|
825
|
const [restoreProducts] = await service.restore([product.id]) |
|
826
|
|
|
827
|
expect(restoreProducts).toHaveLength(1) |
|
828
|
expect(restoreProducts[0].deleted_at).toBeNull() |
|
829
|
}) |
|
830
|
}) |
|
831
|
}) |
|
832
|
}, |
|
833
|
}) |