@labdigital/commercetools-mock 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,110 @@
1
1
  import type {
2
+ Category,
3
+ CategoryDraft,
2
4
  Image,
3
5
  PriceDraft,
4
6
  Product,
5
7
  ProductData,
6
8
  ProductDraft,
9
+ ProductType,
10
+ ProductTypeDraft,
11
+ State,
12
+ StateDraft,
13
+ TaxCategory,
14
+ TaxCategoryDraft,
7
15
  } from '@commercetools/platform-sdk'
8
16
  import assert from 'assert'
9
17
  import supertest from 'supertest'
10
- import { beforeEach, describe, expect, test } from 'vitest'
18
+ import { beforeAll, beforeEach, describe, expect, test } from 'vitest'
11
19
  import { CommercetoolsMock } from '../index.js'
12
20
 
13
- const ctMock = new CommercetoolsMock()
21
+ const productTypeDraft: ProductTypeDraft = {
22
+ key: 'test-product-type',
23
+ name: 'Test Product Type',
24
+ description: 'Test Product Type description',
25
+ }
26
+
27
+ const categoryDraft1: CategoryDraft = {
28
+ key: 'category-1',
29
+ name: {
30
+ 'nl-NL': 'Category One',
31
+ },
32
+ slug: {
33
+ 'nl-NL': 'category_1',
34
+ },
35
+ }
36
+
37
+ const categoryDraft2: CategoryDraft = {
38
+ key: 'category-2',
39
+ name: {
40
+ 'nl-NL': 'Category Two',
41
+ },
42
+ slug: {
43
+ 'nl-NL': 'category_2',
44
+ },
45
+ }
46
+
47
+ const taxcategoryDraft1: TaxCategoryDraft = {
48
+ name: 'Tax category 1',
49
+ key: 'tax-category-1',
50
+ }
51
+
52
+ const taxcategoryDraft2: TaxCategoryDraft = {
53
+ name: 'Tax category 2',
54
+ key: 'tax-category-2',
55
+ }
56
+
57
+ const productState1Draft: StateDraft = {
58
+ key: 'initial-state',
59
+ type: 'ProductState',
60
+ initial: true,
61
+ name: {
62
+ 'nl-NL': 'Initial state',
63
+ },
64
+ description: {
65
+ 'nl-NL': 'Product initial state',
66
+ },
67
+ }
68
+
69
+ const productState2Draft: StateDraft = {
70
+ key: 'another-state',
71
+ type: 'ProductState',
72
+ initial: true,
73
+ name: {
74
+ 'nl-NL': 'Another state',
75
+ },
76
+ description: {
77
+ 'nl-NL': 'Product another state',
78
+ },
79
+ }
14
80
 
15
81
  const publishedProductDraft: ProductDraft = {
16
82
  name: {
17
83
  'nl-NL': 'test published product',
18
84
  },
85
+ description: {
86
+ 'nl-NL': 'Test published product description',
87
+ },
19
88
  productType: {
20
89
  typeId: 'product-type',
21
- id: 'some-uuid',
90
+ key: 'test-product-type',
91
+ },
92
+ categories: [
93
+ {
94
+ typeId: 'category',
95
+ key: 'category-1',
96
+ },
97
+ ],
98
+ taxCategory: {
99
+ typeId: 'tax-category',
100
+ key: taxcategoryDraft1.key,
101
+ },
102
+ state: {
103
+ typeId: 'state',
104
+ key: 'initial-state',
22
105
  },
23
106
  masterVariant: {
107
+ key: 'master-variant-key',
24
108
  sku: '1337',
25
109
  attributes: [
26
110
  {
@@ -30,6 +114,7 @@ const publishedProductDraft: ProductDraft = {
30
114
  ],
31
115
  prices: [
32
116
  {
117
+ key: 'base_price_eur',
33
118
  country: 'NL',
34
119
  value: {
35
120
  currencyCode: 'EUR',
@@ -40,6 +125,7 @@ const publishedProductDraft: ProductDraft = {
40
125
  },
41
126
  variants: [
42
127
  {
128
+ key: 'variant-1-key',
43
129
  sku: '1338',
44
130
  attributes: [
45
131
  {
@@ -49,6 +135,7 @@ const publishedProductDraft: ProductDraft = {
49
135
  ],
50
136
  prices: [
51
137
  {
138
+ key: 'base_price_eur',
52
139
  country: 'NL',
53
140
  value: {
54
141
  currencyCode: 'EUR',
@@ -61,18 +148,46 @@ const publishedProductDraft: ProductDraft = {
61
148
  slug: {
62
149
  'nl-NL': 'test-published-product',
63
150
  },
151
+ metaTitle: {
152
+ 'nl-NL': 'Unpublished product (meta title)',
153
+ },
154
+ metaDescription: {
155
+ 'nl-NL': 'Unpublished product description (meta description)',
156
+ },
157
+ metaKeywords: {
158
+ 'nl-NL': 'Test product (meta Keywords)',
159
+ },
64
160
  publish: true,
65
161
  }
66
162
 
67
163
  const unpublishedProductDraft: ProductDraft = {
164
+ key: 'test-unpublished-product',
68
165
  name: {
69
166
  'nl-NL': 'test unpublished product',
70
167
  },
168
+ description: {
169
+ 'nl-NL': 'Test published product description',
170
+ },
71
171
  productType: {
72
172
  typeId: 'product-type',
73
- id: 'some-uuid',
173
+ key: 'test-product-type',
174
+ },
175
+ categories: [
176
+ {
177
+ typeId: 'category',
178
+ key: 'category-1',
179
+ },
180
+ ],
181
+ taxCategory: {
182
+ typeId: 'tax-category',
183
+ key: taxcategoryDraft1.key,
184
+ },
185
+ state: {
186
+ typeId: 'state',
187
+ key: 'initial-state',
74
188
  },
75
189
  masterVariant: {
190
+ key: 'master-variant-key',
76
191
  sku: '2337',
77
192
  attributes: [
78
193
  {
@@ -80,9 +195,20 @@ const unpublishedProductDraft: ProductDraft = {
80
195
  value: 'test',
81
196
  },
82
197
  ],
198
+ prices: [
199
+ {
200
+ key: 'base_price_eur',
201
+ country: 'NL',
202
+ value: {
203
+ currencyCode: 'EUR',
204
+ centAmount: 1000,
205
+ },
206
+ },
207
+ ],
83
208
  },
84
209
  variants: [
85
210
  {
211
+ key: 'variant-1-key',
86
212
  sku: '2338',
87
213
  attributes: [
88
214
  {
@@ -90,16 +216,109 @@ const unpublishedProductDraft: ProductDraft = {
90
216
  value: 'test2',
91
217
  },
92
218
  ],
219
+ prices: [
220
+ {
221
+ key: 'base_price_eur',
222
+ country: 'NL',
223
+ value: {
224
+ currencyCode: 'EUR',
225
+ centAmount: 2000,
226
+ },
227
+ },
228
+ ],
93
229
  },
94
230
  ],
95
231
  slug: {
96
232
  'nl-NL': 'test-unpublished-product',
97
233
  },
234
+ metaTitle: {
235
+ 'nl-NL': 'Unpublished product (meta title)',
236
+ },
237
+ metaDescription: {
238
+ 'nl-NL': 'Unpublished product description (meta description)',
239
+ },
240
+ metaKeywords: {
241
+ 'nl-NL': 'Test product (meta Keywords)',
242
+ },
98
243
  publish: false,
99
244
  }
100
245
 
246
+ let productType: ProductType
247
+ let category1: Category
248
+ let category2: Category
249
+ let taxCategory1: TaxCategory
250
+ let taxCategory2: TaxCategory
251
+ let productState1: State
252
+ let productState2: State
253
+
254
+ async function beforeAllProductTests(mock) {
255
+ let response
256
+ // Create Product Type
257
+ response = await supertest(mock.app)
258
+ .post('/dummy/product-types')
259
+ .send(productTypeDraft)
260
+
261
+ expect(response.status).toBe(201)
262
+ productType = response.body
263
+
264
+ // Create Category 1
265
+ response = await supertest(mock.app)
266
+ .post('/dummy/categories')
267
+ .send(categoryDraft1)
268
+
269
+ expect(response.status).toBe(201)
270
+ category1 = response.body
271
+
272
+ // Create Category 2
273
+ response = await supertest(mock.app)
274
+ .post('/dummy/categories')
275
+ .send(categoryDraft2)
276
+
277
+ expect(response.status).toBe(201)
278
+ category2 = response.body
279
+
280
+ // Create Tax Category 1
281
+ response = await supertest(mock.app)
282
+ .post('/dummy/tax-categories')
283
+ .send(taxcategoryDraft1)
284
+
285
+ expect(response.status).toBe(201)
286
+ taxCategory1 = response.body
287
+
288
+ // Create Tax Category 2
289
+ response = await supertest(mock.app)
290
+ .post('/dummy/tax-categories')
291
+ .send(taxcategoryDraft2)
292
+
293
+ expect(response.status).toBe(201)
294
+ taxCategory2 = response.body
295
+
296
+ // Create Product State 1
297
+ response = await supertest(mock.app)
298
+ .post('/dummy/states')
299
+ .send(productState1Draft)
300
+
301
+ expect(response.status).toBe(201)
302
+ productState1 = response.body
303
+
304
+ // Create Product State 2
305
+ response = await supertest(mock.app)
306
+ .post('/dummy/states')
307
+ .send(productState2Draft)
308
+
309
+ expect(response.status).toBe(201)
310
+ productState2 = response.body
311
+ }
312
+
101
313
  describe('Product', () => {
314
+ const ctMock = new CommercetoolsMock()
315
+ beforeAll(async () => {
316
+ await beforeAllProductTests(ctMock)
317
+ })
318
+
102
319
  test('Create product', async () => {
320
+ assert(productType, 'product type not created')
321
+
103
322
  const response = await supertest(ctMock.app)
104
323
  .post('/dummy/products')
105
324
  .send(unpublishedProductDraft)
@@ -108,11 +327,30 @@ describe('Product', () => {
108
327
  name: {
109
328
  'nl-NL': 'test unpublished product',
110
329
  },
330
+ description: {
331
+ 'nl-NL': 'Test published product description',
332
+ },
111
333
  slug: {
112
334
  'nl-NL': 'test-unpublished-product',
113
335
  },
114
- categories: [],
336
+ categories: [
337
+ {
338
+ id: category1.id,
339
+ typeId: 'category',
340
+ },
341
+ ],
342
+ metaTitle: {
343
+ 'nl-NL': 'Unpublished product (meta title)',
344
+ },
345
+ metaDescription: {
346
+ 'nl-NL': 'Unpublished product description (meta description)',
347
+ },
348
+ metaKeywords: {
349
+ 'nl-NL': 'Test product (meta Keywords)',
350
+ },
115
351
  masterVariant: {
352
+ id: 1,
353
+ key: 'master-variant-key',
116
354
  sku: '2337',
117
355
  assets: [],
118
356
  attributes: [
@@ -121,11 +359,24 @@ describe('Product', () => {
121
359
  value: 'test',
122
360
  },
123
361
  ],
124
- id: 1,
362
+ prices: [
363
+ {
364
+ id: expect.anything(),
365
+ key: 'base_price_eur',
366
+ country: 'NL',
367
+ value: {
368
+ type: 'centPrecision',
369
+ centAmount: 1000,
370
+ currencyCode: 'EUR',
371
+ fractionDigits: 2,
372
+ },
373
+ },
374
+ ],
125
375
  images: [],
126
376
  },
127
377
  variants: [
128
378
  {
379
+ key: 'variant-1-key',
129
380
  sku: '2338',
130
381
  assets: [],
131
382
  id: 2,
@@ -136,6 +387,19 @@ describe('Product', () => {
136
387
  value: 'test2',
137
388
  },
138
389
  ],
390
+ prices: [
391
+ {
392
+ id: expect.anything(),
393
+ key: 'base_price_eur',
394
+ country: 'NL',
395
+ value: {
396
+ type: 'centPrecision',
397
+ centAmount: 2000,
398
+ currencyCode: 'EUR',
399
+ fractionDigits: 2,
400
+ },
401
+ },
402
+ ],
139
403
  },
140
404
  ],
141
405
  searchKeywords: {},
@@ -145,6 +409,11 @@ describe('Product', () => {
145
409
  createdAt: expect.anything(),
146
410
  id: expect.anything(),
147
411
  lastModifiedAt: expect.anything(),
412
+ key: 'test-unpublished-product',
413
+ taxCategory: {
414
+ typeId: 'tax-category',
415
+ id: taxCategory1.id,
416
+ },
148
417
  masterData: {
149
418
  staged: productData,
150
419
  current: productData,
@@ -153,7 +422,11 @@ describe('Product', () => {
153
422
  },
154
423
  productType: {
155
424
  typeId: 'product-type',
156
- id: 'some-uuid',
425
+ id: productType.id,
426
+ },
427
+ state: {
428
+ typeId: 'state',
429
+ id: productState1.id,
157
430
  },
158
431
  version: 1,
159
432
  } as Product)
@@ -163,6 +436,9 @@ describe('Product', () => {
163
436
  describe('Product update actions', () => {
164
437
  const ctMock = new CommercetoolsMock()
165
438
  let productPublished: Product | undefined
439
+ beforeAll(async () => {
440
+ await beforeAllProductTests(ctMock)
441
+ })
166
442
 
167
443
  beforeEach(async () => {
168
444
  let response
@@ -441,6 +717,7 @@ describe('Product update actions', () => {
441
717
  action: 'addPrice',
442
718
  price: priceDraft,
443
719
  variantId: 1,
720
+ staged: false,
444
721
  },
445
722
  ],
446
723
  })
@@ -462,6 +739,9 @@ describe('Product update actions', () => {
462
739
  },
463
740
  },
464
741
  ])
742
+ expect(response.body.masterData.staged.masterVariant.prices[1].id).toBe(
743
+ response.body.masterData.current.masterVariant.prices[1].id
744
+ )
465
745
  })
466
746
 
467
747
  test('changePrice variant', async () => {
@@ -525,4 +805,515 @@ describe('Product update actions', () => {
525
805
  expect(response.body.version).toBe(2)
526
806
  expect(response.body.masterData.staged.masterVariant.prices).toHaveLength(0)
527
807
  })
808
+
809
+ test('changeName product', async () => {
810
+ assert(productPublished, 'product not created')
811
+ const response = await supertest(ctMock.app)
812
+ .post(`/dummy/products/${productPublished.id}`)
813
+ .send({
814
+ version: 1,
815
+ actions: [
816
+ {
817
+ action: 'changeName',
818
+ name: 'new test published product',
819
+ staged: false,
820
+ },
821
+ ],
822
+ })
823
+ expect(response.status).toBe(200)
824
+ expect(response.body.version).toBe(2)
825
+ expect(response.body.masterData.staged.name).toBe(
826
+ 'new test published product'
827
+ )
828
+ expect(response.body.masterData.current.name).toBe(
829
+ 'new test published product'
830
+ )
831
+ })
832
+
833
+ test('changeSlug product', async () => {
834
+ assert(productPublished, 'product not created')
835
+ const response = await supertest(ctMock.app)
836
+ .post(`/dummy/products/${productPublished.id}`)
837
+ .send({
838
+ version: 1,
839
+ actions: [
840
+ {
841
+ action: 'changeSlug',
842
+ slug: {
843
+ 'nl-NL': 'test-published-product-new',
844
+ },
845
+ staged: false,
846
+ },
847
+ ],
848
+ })
849
+ expect(response.status).toBe(200)
850
+ expect(response.body.version).toBe(2)
851
+ expect(response.body.masterData.staged.slug).toMatchObject({
852
+ 'nl-NL': 'test-published-product-new',
853
+ })
854
+ expect(response.body.masterData.current.slug).toMatchObject({
855
+ 'nl-NL': 'test-published-product-new',
856
+ })
857
+ })
858
+
859
+ test('setMetaTitle product', async () => {
860
+ assert(productPublished, 'product not created')
861
+ const response = await supertest(ctMock.app)
862
+ .post(`/dummy/products/${productPublished.id}`)
863
+ .send({
864
+ version: 1,
865
+ actions: [
866
+ {
867
+ action: 'setMetaTitle',
868
+ metaTitle: {
869
+ 'nl-NL': 'Unpublished product (new meta title)',
870
+ },
871
+ staged: false,
872
+ },
873
+ ],
874
+ })
875
+ expect(response.status).toBe(200)
876
+ expect(response.body.version).toBe(2)
877
+ expect(response.body.masterData.staged.metaTitle).toMatchObject({
878
+ 'nl-NL': 'Unpublished product (new meta title)',
879
+ })
880
+ expect(response.body.masterData.current.metaTitle).toMatchObject({
881
+ 'nl-NL': 'Unpublished product (new meta title)',
882
+ })
883
+ })
884
+
885
+ test('setMetaDescription product', async () => {
886
+ assert(productPublished, 'product not created')
887
+ const response = await supertest(ctMock.app)
888
+ .post(`/dummy/products/${productPublished.id}`)
889
+ .send({
890
+ version: 1,
891
+ actions: [
892
+ {
893
+ action: 'setMetaDescription',
894
+ metaDescription: {
895
+ 'nl-NL': 'Unpublished product description (new meta description)',
896
+ },
897
+ staged: false,
898
+ },
899
+ ],
900
+ })
901
+ expect(response.status).toBe(200)
902
+ expect(response.body.version).toBe(2)
903
+ expect(response.body.masterData.staged.metaDescription).toMatchObject({
904
+ 'nl-NL': 'Unpublished product description (new meta description)',
905
+ })
906
+ expect(response.body.masterData.current.metaDescription).toMatchObject({
907
+ 'nl-NL': 'Unpublished product description (new meta description)',
908
+ })
909
+ })
910
+
911
+ test('setMetaKeywords product', async () => {
912
+ assert(productPublished, 'product not created')
913
+ const response = await supertest(ctMock.app)
914
+ .post(`/dummy/products/${productPublished.id}`)
915
+ .send({
916
+ version: 1,
917
+ actions: [
918
+ {
919
+ action: 'setMetaKeywords',
920
+ metaKeywords: {
921
+ 'nl-NL': 'Test product (newmeta Keywords)',
922
+ },
923
+ staged: false,
924
+ },
925
+ ],
926
+ })
927
+ expect(response.status).toBe(200)
928
+ expect(response.body.version).toBe(2)
929
+ expect(response.body.masterData.staged.metaKeywords).toMatchObject({
930
+ 'nl-NL': 'Test product (newmeta Keywords)',
931
+ })
932
+ expect(response.body.masterData.current.metaKeywords).toMatchObject({
933
+ 'nl-NL': 'Test product (newmeta Keywords)',
934
+ })
935
+ })
936
+
937
+ test('addVariant product', async () => {
938
+ assert(productPublished, 'product not created')
939
+ const response = await supertest(ctMock.app)
940
+ .post(`/dummy/products/${productPublished.id}`)
941
+ .send({
942
+ version: 1,
943
+ actions: [
944
+ {
945
+ action: 'addVariant',
946
+ sku: '4567',
947
+ key: 'variant-2-key',
948
+ price: [
949
+ {
950
+ key: 'base_price_eur',
951
+ country: 'NL',
952
+ value: {
953
+ currencyCode: 'EUR',
954
+ centAmount: 3000,
955
+ },
956
+ },
957
+ ],
958
+ images: [],
959
+ attributes: [
960
+ {
961
+ name: 'test3',
962
+ value: 'test3',
963
+ },
964
+ ],
965
+ assets: [],
966
+ staged: false,
967
+ },
968
+ ],
969
+ })
970
+ expect(response.status).toBe(200)
971
+ expect(response.body.version).toBe(2)
972
+ expect(response.body.masterData.staged.variants).toHaveLength(2)
973
+ expect(response.body.masterData.current.variants).toHaveLength(2)
974
+ expect(response.body.masterData.staged.variants[1].id).toBe(3)
975
+ expect(response.body.masterData.current.variants[1].id).toBe(3)
976
+ })
977
+
978
+ test('removeVariant by id', async () => {
979
+ assert(productPublished, 'product not created')
980
+ const response = await supertest(ctMock.app)
981
+ .post(`/dummy/products/${productPublished.id}`)
982
+ .send({
983
+ version: 1,
984
+ actions: [
985
+ {
986
+ action: 'removeVariant',
987
+ id: 2,
988
+ staged: false,
989
+ },
990
+ ],
991
+ })
992
+ expect(response.status).toBe(200)
993
+ expect(response.body.version).toBe(2)
994
+ expect(response.body.masterData.staged.variants).toHaveLength(0)
995
+ expect(response.body.masterData.current.variants).toHaveLength(0)
996
+ })
997
+
998
+ test('removeVariant by sku', async () => {
999
+ assert(productPublished, 'product not created')
1000
+ const response = await supertest(ctMock.app)
1001
+ .post(`/dummy/products/${productPublished.id}`)
1002
+ .send({
1003
+ version: 1,
1004
+ actions: [
1005
+ {
1006
+ action: 'removeVariant',
1007
+ sku: '1338',
1008
+ staged: false,
1009
+ },
1010
+ ],
1011
+ })
1012
+ expect(response.status).toBe(200)
1013
+ expect(response.body.version).toBe(2)
1014
+ expect(response.body.masterData.staged.variants).toHaveLength(0)
1015
+ expect(response.body.masterData.current.variants).toHaveLength(0)
1016
+ })
1017
+
1018
+ test('removeVariant master', async () => {
1019
+ assert(productPublished, 'product not created')
1020
+ const response = await supertest(ctMock.app)
1021
+ .post(`/dummy/products/${productPublished.id}`)
1022
+ .send({
1023
+ version: 1,
1024
+ actions: [
1025
+ {
1026
+ action: 'removeVariant',
1027
+ id: 1,
1028
+ staged: false,
1029
+ },
1030
+ ],
1031
+ })
1032
+ expect(response.status).toBe(500)
1033
+ expect(response.body.error).toBe(
1034
+ `Can not remove the variant [ID:1] for [Product:${productPublished.id}] since it's the master variant`
1035
+ )
1036
+ })
1037
+
1038
+ test('changeMasterVariant', async () => {
1039
+ assert(productPublished, 'product not created')
1040
+ const response = await supertest(ctMock.app)
1041
+ .post(`/dummy/products/${productPublished.id}`)
1042
+ .send({
1043
+ version: 1,
1044
+ actions: [
1045
+ {
1046
+ action: 'changeMasterVariant',
1047
+ sku: '1338',
1048
+ staged: false,
1049
+ },
1050
+ ],
1051
+ })
1052
+ expect(response.status).toBe(200)
1053
+ expect(response.body.version).toBe(2)
1054
+ expect(response.body.masterData.staged.variants).toHaveLength(1)
1055
+ expect(response.body.masterData.staged.masterVariant.id).toBe(2)
1056
+ expect(response.body.masterData.staged.variants[0].id).toBe(1)
1057
+
1058
+ expect(response.body.masterData.current.variants).toHaveLength(1)
1059
+ expect(response.body.masterData.current.masterVariant.id).toBe(2)
1060
+ expect(response.body.masterData.current.variants[0].id).toBe(1)
1061
+ })
1062
+
1063
+ test('changeMasterVariant same master', async () => {
1064
+ assert(productPublished, 'product not created')
1065
+ const response = await supertest(ctMock.app)
1066
+ .post(`/dummy/products/${productPublished.id}`)
1067
+ .send({
1068
+ version: 1,
1069
+ actions: [
1070
+ {
1071
+ action: 'changeMasterVariant',
1072
+ variantId: 1,
1073
+ staged: false,
1074
+ },
1075
+ ],
1076
+ })
1077
+ expect(response.status).toBe(200)
1078
+ expect(response.body.version).toBe(1)
1079
+ expect(response.body.masterData.staged.variants).toHaveLength(1)
1080
+ expect(response.body.masterData.staged.masterVariant.id).toBe(1)
1081
+ expect(response.body.masterData.staged.variants[0].id).toBe(2)
1082
+
1083
+ expect(response.body.masterData.current.variants).toHaveLength(1)
1084
+ expect(response.body.masterData.current.masterVariant.id).toBe(1)
1085
+ expect(response.body.masterData.current.variants[0].id).toBe(2)
1086
+ })
1087
+
1088
+ test('setTaxCategory', async () => {
1089
+ assert(productPublished, 'product not created')
1090
+ const response = await supertest(ctMock.app)
1091
+ .post(`/dummy/products/${productPublished.id}`)
1092
+ .send({
1093
+ version: 1,
1094
+ actions: [
1095
+ {
1096
+ action: 'setTaxCategory',
1097
+ taxCategory: {
1098
+ typeId: 'tax-category',
1099
+ id: taxCategory2.id,
1100
+ },
1101
+ },
1102
+ ],
1103
+ })
1104
+ expect(response.status).toBe(200)
1105
+ expect(response.body.taxCategory.id).toBe(taxCategory2.id)
1106
+ })
1107
+
1108
+ test('setTaxCategory fail 1', async () => {
1109
+ assert(productPublished, 'product not created')
1110
+ const fakeTaxCategoryId = '00000000-0000-0000-0000-000000000000'
1111
+ const response = await supertest(ctMock.app)
1112
+ .post(`/dummy/products/${productPublished.id}`)
1113
+ .send({
1114
+ version: 1,
1115
+ actions: [
1116
+ {
1117
+ action: 'setTaxCategory',
1118
+ taxCategory: {
1119
+ typeId: 'tax-category',
1120
+ id: fakeTaxCategoryId,
1121
+ },
1122
+ },
1123
+ ],
1124
+ })
1125
+ expect(response.status).toBe(400)
1126
+ expect(response.body.errors[0].code).toBe('ReferencedResourceNotFound')
1127
+ })
1128
+
1129
+ test('setTaxCategory fail 2', async () => {
1130
+ assert(productPublished, 'product not created')
1131
+ const response = await supertest(ctMock.app)
1132
+ .post(`/dummy/products/${productPublished.id}`)
1133
+ .send({
1134
+ version: 1,
1135
+ actions: [
1136
+ {
1137
+ action: 'setTaxCategory',
1138
+ taxCategory: {
1139
+ typeId: 'tax-category',
1140
+ },
1141
+ },
1142
+ ],
1143
+ })
1144
+ expect(response.status).toBe(400)
1145
+ expect(response.body.errors[0].code).toBe('InvalidJsonInput')
1146
+ })
1147
+
1148
+ test('addToCategory by id', async () => {
1149
+ assert(productPublished, 'product not created')
1150
+ const response = await supertest(ctMock.app)
1151
+ .post(`/dummy/products/${productPublished.id}`)
1152
+ .send({
1153
+ version: 1,
1154
+ actions: [
1155
+ {
1156
+ action: 'addToCategory',
1157
+ category: {
1158
+ typeId: 'category',
1159
+ id: category2.id,
1160
+ },
1161
+ staged: false,
1162
+ },
1163
+ ],
1164
+ })
1165
+ expect(response.status).toBe(200)
1166
+ expect(response.body.masterData.staged.categories).toHaveLength(2)
1167
+ expect(response.body.masterData.current.categories).toHaveLength(2)
1168
+ })
1169
+
1170
+ test('addToCategory by key', async () => {
1171
+ assert(productPublished, 'product not created')
1172
+ const response = await supertest(ctMock.app)
1173
+ .post(`/dummy/products/${productPublished.id}`)
1174
+ .send({
1175
+ version: 1,
1176
+ actions: [
1177
+ {
1178
+ action: 'addToCategory',
1179
+ category: {
1180
+ typeId: 'category',
1181
+ key: category2.key,
1182
+ },
1183
+ staged: true,
1184
+ },
1185
+ ],
1186
+ })
1187
+ expect(response.status).toBe(200)
1188
+ expect(response.body.masterData.staged.categories).toHaveLength(2)
1189
+ expect(response.body.masterData.current.categories).toHaveLength(1)
1190
+ })
1191
+
1192
+ test('addToCategory fail 1', async () => {
1193
+ assert(productPublished, 'product not created')
1194
+ const fakeCategoryId = '00000000-0000-0000-0000-000000000000'
1195
+ const response = await supertest(ctMock.app)
1196
+ .post(`/dummy/products/${productPublished.id}`)
1197
+ .send({
1198
+ version: 1,
1199
+ actions: [
1200
+ {
1201
+ action: 'addToCategory',
1202
+ category: {
1203
+ typeId: 'category',
1204
+ id: fakeCategoryId,
1205
+ },
1206
+ staged: true,
1207
+ },
1208
+ ],
1209
+ })
1210
+ expect(response.status).toBe(400)
1211
+ expect(response.body.errors[0].code).toBe('ReferencedResourceNotFound')
1212
+ })
1213
+
1214
+ test('addToCategory fail 2', async () => {
1215
+ assert(productPublished, 'product not created')
1216
+ const response = await supertest(ctMock.app)
1217
+ .post(`/dummy/products/${productPublished.id}`)
1218
+ .send({
1219
+ version: 1,
1220
+ actions: [
1221
+ {
1222
+ action: 'addToCategory',
1223
+ category: null,
1224
+ staged: true,
1225
+ },
1226
+ ],
1227
+ })
1228
+ expect(response.status).toBe(400)
1229
+ expect(response.body.errors[0].code).toBe('InvalidJsonInput')
1230
+ })
1231
+
1232
+ test('removeFromCategory', async () => {
1233
+ assert(productPublished, 'product not created')
1234
+ const response = await supertest(ctMock.app)
1235
+ .post(`/dummy/products/${productPublished.id}`)
1236
+ .send({
1237
+ version: 1,
1238
+ actions: [
1239
+ {
1240
+ action: 'removeFromCategory',
1241
+ category: {
1242
+ typeId: 'category',
1243
+ key: category1.key,
1244
+ },
1245
+ staged: false,
1246
+ },
1247
+ ],
1248
+ })
1249
+ expect(response.status).toBe(200)
1250
+ expect(response.body.masterData.staged.categories).toHaveLength(0)
1251
+ expect(response.body.masterData.current.categories).toHaveLength(0)
1252
+ })
1253
+
1254
+ test('removeFromCategory fail 1', async () => {
1255
+ assert(productPublished, 'product not created')
1256
+ const response = await supertest(ctMock.app)
1257
+ .post(`/dummy/products/${productPublished.id}`)
1258
+ .send({
1259
+ version: 1,
1260
+ actions: [
1261
+ {
1262
+ action: 'removeFromCategory',
1263
+ category: {
1264
+ typeId: 'category',
1265
+ id: 'fake-category-id',
1266
+ },
1267
+ staged: false,
1268
+ },
1269
+ ],
1270
+ })
1271
+ expect(response.status).toBe(400)
1272
+ expect(response.body.errors[0].code).toBe('ReferencedResourceNotFound')
1273
+ })
1274
+
1275
+ test('removeFromCategory fail 2', async () => {
1276
+ assert(productPublished, 'product not created')
1277
+ const response = await supertest(ctMock.app)
1278
+ .post(`/dummy/products/${productPublished.id}`)
1279
+ .send({
1280
+ version: 1,
1281
+ actions: [
1282
+ {
1283
+ action: 'removeFromCategory',
1284
+ category: {
1285
+ typeId: 'category',
1286
+ id: category2.id,
1287
+ },
1288
+ staged: false,
1289
+ },
1290
+ ],
1291
+ })
1292
+ expect(response.status).toBe(400)
1293
+ expect(response.body.errors[0].code).toBe('InvalidOperation')
1294
+ })
1295
+
1296
+ test('transitionState', async () => {
1297
+ assert(productPublished, 'product not created')
1298
+ const response = await supertest(ctMock.app)
1299
+ .post(`/dummy/products/${productPublished.id}`)
1300
+ .send({
1301
+ version: 1,
1302
+ actions: [
1303
+ {
1304
+ action: 'transitionState',
1305
+ state: {
1306
+ typeId: 'state',
1307
+ id: productState2.id,
1308
+ },
1309
+ force: false,
1310
+ },
1311
+ ],
1312
+ })
1313
+ expect(response.status).toBe(200)
1314
+ expect(response.body.state).toMatchObject({
1315
+ typeId: 'state',
1316
+ id: productState2.id,
1317
+ })
1318
+ })
528
1319
  })