@labdigital/commercetools-mock 1.4.0 → 1.6.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.
Files changed (129) hide show
  1. package/README.md +5 -4
  2. package/dist/index.cjs +116 -18
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +30 -7
  5. package/dist/index.d.ts +30 -7
  6. package/dist/index.js +116 -18
  7. package/dist/index.js.map +1 -1
  8. package/package.json +4 -3
  9. package/src/constants.ts +2 -2
  10. package/src/ctMock.ts +176 -176
  11. package/src/exceptions.ts +10 -10
  12. package/src/helpers.ts +26 -26
  13. package/src/index.test.ts +173 -173
  14. package/src/index.ts +3 -3
  15. package/src/lib/expandParser.ts +19 -19
  16. package/src/lib/haversine.test.ts +13 -13
  17. package/src/lib/haversine.ts +14 -14
  18. package/src/lib/masking.ts +15 -15
  19. package/src/lib/parser.ts +2 -2
  20. package/src/lib/predicateParser.test.ts +204 -204
  21. package/src/lib/predicateParser.ts +398 -398
  22. package/src/lib/projectionSearchFilter.test.ts +168 -168
  23. package/src/lib/projectionSearchFilter.ts +272 -269
  24. package/src/lib/proxy.ts +8 -8
  25. package/src/oauth/errors.ts +4 -4
  26. package/src/oauth/helpers.ts +6 -6
  27. package/src/oauth/server.ts +103 -101
  28. package/src/oauth/store.ts +27 -27
  29. package/src/priceSelector.test.ts +68 -68
  30. package/src/priceSelector.ts +70 -70
  31. package/src/product-projection-search.ts +296 -296
  32. package/src/projectAPI.test.ts +3 -3
  33. package/src/projectAPI.ts +46 -46
  34. package/src/repositories/abstract.ts +190 -190
  35. package/src/repositories/associate-role.ts +10 -7
  36. package/src/repositories/attribute-group.ts +63 -8
  37. package/src/repositories/business-unit.ts +10 -7
  38. package/src/repositories/cart-discount.ts +134 -134
  39. package/src/repositories/cart.ts +517 -514
  40. package/src/repositories/category.ts +170 -167
  41. package/src/repositories/channel.ts +114 -111
  42. package/src/repositories/custom-object.ts +66 -63
  43. package/src/repositories/customer-group.ts +72 -69
  44. package/src/repositories/customer.ts +93 -79
  45. package/src/repositories/discount-code.ts +171 -168
  46. package/src/repositories/errors.ts +15 -15
  47. package/src/repositories/extension.ts +79 -76
  48. package/src/repositories/helpers.ts +180 -180
  49. package/src/repositories/index.ts +39 -39
  50. package/src/repositories/inventory-entry.ts +98 -95
  51. package/src/repositories/my-order.ts +11 -11
  52. package/src/repositories/order-edit.ts +29 -29
  53. package/src/repositories/order.test.ts +191 -191
  54. package/src/repositories/order.ts +393 -389
  55. package/src/repositories/payment.ts +155 -155
  56. package/src/repositories/product-discount.ts +149 -149
  57. package/src/repositories/product-projection.ts +116 -52
  58. package/src/repositories/product-selection.ts +31 -31
  59. package/src/repositories/product-type.ts +156 -156
  60. package/src/repositories/product.ts +600 -597
  61. package/src/repositories/project.ts +136 -135
  62. package/src/repositories/quote-request.ts +19 -19
  63. package/src/repositories/quote.ts +19 -19
  64. package/src/repositories/review.ts +24 -24
  65. package/src/repositories/shipping-method.ts +217 -217
  66. package/src/repositories/shopping-list.ts +49 -49
  67. package/src/repositories/staged-quote.ts +20 -20
  68. package/src/repositories/standalone-price.ts +72 -61
  69. package/src/repositories/state.ts +84 -84
  70. package/src/repositories/store.ts +114 -114
  71. package/src/repositories/subscription.ts +40 -40
  72. package/src/repositories/tax-category.ts +98 -98
  73. package/src/repositories/type.ts +157 -157
  74. package/src/repositories/zone.ts +71 -71
  75. package/src/server.ts +2 -2
  76. package/src/services/abstract.ts +173 -173
  77. package/src/services/attribute-group.ts +16 -0
  78. package/src/services/cart-discount.ts +8 -8
  79. package/src/services/cart.test.ts +409 -409
  80. package/src/services/cart.ts +50 -50
  81. package/src/services/category.test.ts +25 -25
  82. package/src/services/category.ts +8 -8
  83. package/src/services/channel.ts +8 -8
  84. package/src/services/custom-object.test.ts +184 -184
  85. package/src/services/custom-object.ts +48 -48
  86. package/src/services/customer-group.ts +8 -8
  87. package/src/services/customer.test.ts +151 -129
  88. package/src/services/customer.ts +27 -27
  89. package/src/services/discount-code.ts +8 -8
  90. package/src/services/extension.ts +8 -8
  91. package/src/services/index.ts +52 -44
  92. package/src/services/inventory-entry.test.ts +162 -162
  93. package/src/services/inventory-entry.ts +8 -8
  94. package/src/services/my-cart.test.ts +78 -78
  95. package/src/services/my-cart.ts +28 -28
  96. package/src/services/my-customer.test.ts +44 -44
  97. package/src/services/my-customer.ts +53 -53
  98. package/src/services/my-order.ts +20 -20
  99. package/src/services/my-payment.test.ts +65 -65
  100. package/src/services/my-payment.ts +8 -8
  101. package/src/services/order.test.ts +527 -527
  102. package/src/services/order.ts +31 -31
  103. package/src/services/payment.test.ts +65 -65
  104. package/src/services/payment.ts +8 -8
  105. package/src/services/product-discount.ts +8 -8
  106. package/src/services/product-projection.test.ts +492 -428
  107. package/src/services/product-projection.ts +32 -18
  108. package/src/services/product-type.test.ts +56 -56
  109. package/src/services/product-type.ts +8 -8
  110. package/src/services/product.test.ts +510 -510
  111. package/src/services/product.ts +8 -8
  112. package/src/services/project.ts +34 -34
  113. package/src/services/shipping-method.test.ts +81 -81
  114. package/src/services/shipping-method.ts +12 -12
  115. package/src/services/shopping-list.ts +8 -8
  116. package/src/services/standalone-price.test.ts +256 -256
  117. package/src/services/standalone-price.ts +8 -8
  118. package/src/services/state.test.ts +42 -42
  119. package/src/services/state.ts +8 -8
  120. package/src/services/store.test.ts +57 -57
  121. package/src/services/store.ts +8 -8
  122. package/src/services/subscription.ts +8 -8
  123. package/src/services/tax-category.test.ts +61 -61
  124. package/src/services/tax-category.ts +8 -8
  125. package/src/services/type.ts +8 -8
  126. package/src/services/zone.ts +8 -8
  127. package/src/storage/abstract.ts +58 -58
  128. package/src/storage/in-memory.ts +419 -419
  129. package/src/types.ts +82 -82
@@ -1,9 +1,9 @@
1
1
  import type {
2
- Image,
3
- PriceDraft,
4
- Product,
5
- ProductData,
6
- ProductDraft,
2
+ Image,
3
+ PriceDraft,
4
+ Product,
5
+ ProductData,
6
+ ProductDraft,
7
7
  } from '@commercetools/platform-sdk'
8
8
  import assert from 'assert'
9
9
  import supertest from 'supertest'
@@ -13,518 +13,518 @@ import { CommercetoolsMock } from '../index.js'
13
13
  const ctMock = new CommercetoolsMock()
14
14
 
15
15
  const publishedProductDraft: ProductDraft = {
16
- name: {
17
- 'nl-NL': 'test published product',
18
- },
19
- productType: {
20
- typeId: 'product-type',
21
- id: 'some-uuid',
22
- },
23
- masterVariant: {
24
- sku: '1337',
25
- attributes: [
26
- {
27
- name: 'test',
28
- value: 'test',
29
- },
30
- ],
31
- prices: [
32
- {
33
- country: 'NL',
34
- value: {
35
- currencyCode: 'EUR',
36
- centAmount: 1000,
37
- },
38
- },
39
- ],
40
- },
41
- variants: [
42
- {
43
- sku: '1338',
44
- attributes: [
45
- {
46
- name: 'test2',
47
- value: 'test2',
48
- },
49
- ],
50
- prices: [
51
- {
52
- country: 'NL',
53
- value: {
54
- currencyCode: 'EUR',
55
- centAmount: 2000,
56
- },
57
- },
58
- ],
59
- },
60
- ],
61
- slug: {
62
- 'nl-NL': 'test-published-product',
63
- },
64
- publish: true,
16
+ name: {
17
+ 'nl-NL': 'test published product',
18
+ },
19
+ productType: {
20
+ typeId: 'product-type',
21
+ id: 'some-uuid',
22
+ },
23
+ masterVariant: {
24
+ sku: '1337',
25
+ attributes: [
26
+ {
27
+ name: 'test',
28
+ value: 'test',
29
+ },
30
+ ],
31
+ prices: [
32
+ {
33
+ country: 'NL',
34
+ value: {
35
+ currencyCode: 'EUR',
36
+ centAmount: 1000,
37
+ },
38
+ },
39
+ ],
40
+ },
41
+ variants: [
42
+ {
43
+ sku: '1338',
44
+ attributes: [
45
+ {
46
+ name: 'test2',
47
+ value: 'test2',
48
+ },
49
+ ],
50
+ prices: [
51
+ {
52
+ country: 'NL',
53
+ value: {
54
+ currencyCode: 'EUR',
55
+ centAmount: 2000,
56
+ },
57
+ },
58
+ ],
59
+ },
60
+ ],
61
+ slug: {
62
+ 'nl-NL': 'test-published-product',
63
+ },
64
+ publish: true,
65
65
  }
66
66
 
67
67
  const unpublishedProductDraft: ProductDraft = {
68
- name: {
69
- 'nl-NL': 'test unpublished product',
70
- },
71
- productType: {
72
- typeId: 'product-type',
73
- id: 'some-uuid',
74
- },
75
- masterVariant: {
76
- sku: '2337',
77
- attributes: [
78
- {
79
- name: 'test',
80
- value: 'test',
81
- },
82
- ],
83
- },
84
- variants: [
85
- {
86
- sku: '2338',
87
- attributes: [
88
- {
89
- name: 'test2',
90
- value: 'test2',
91
- },
92
- ],
93
- },
94
- ],
95
- slug: {
96
- 'nl-NL': 'test-unpublished-product',
97
- },
98
- publish: false,
68
+ name: {
69
+ 'nl-NL': 'test unpublished product',
70
+ },
71
+ productType: {
72
+ typeId: 'product-type',
73
+ id: 'some-uuid',
74
+ },
75
+ masterVariant: {
76
+ sku: '2337',
77
+ attributes: [
78
+ {
79
+ name: 'test',
80
+ value: 'test',
81
+ },
82
+ ],
83
+ },
84
+ variants: [
85
+ {
86
+ sku: '2338',
87
+ attributes: [
88
+ {
89
+ name: 'test2',
90
+ value: 'test2',
91
+ },
92
+ ],
93
+ },
94
+ ],
95
+ slug: {
96
+ 'nl-NL': 'test-unpublished-product',
97
+ },
98
+ publish: false,
99
99
  }
100
100
 
101
101
  describe('Product', () => {
102
- test('Create product', async () => {
103
- const response = await supertest(ctMock.app)
104
- .post('/dummy/products')
105
- .send(unpublishedProductDraft)
106
-
107
- const productData: ProductData = {
108
- name: {
109
- 'nl-NL': 'test unpublished product',
110
- },
111
- slug: {
112
- 'nl-NL': 'test-unpublished-product',
113
- },
114
- categories: [],
115
- masterVariant: {
116
- sku: '2337',
117
- assets: [],
118
- attributes: [
119
- {
120
- name: 'test',
121
- value: 'test',
122
- },
123
- ],
124
- id: 1,
125
- images: [],
126
- },
127
- variants: [
128
- {
129
- sku: '2338',
130
- assets: [],
131
- id: 2,
132
- images: [],
133
- attributes: [
134
- {
135
- name: 'test2',
136
- value: 'test2',
137
- },
138
- ],
139
- },
140
- ],
141
- searchKeywords: {},
142
- }
143
-
144
- expect(response.body).toEqual({
145
- createdAt: expect.anything(),
146
- id: expect.anything(),
147
- lastModifiedAt: expect.anything(),
148
- masterData: {
149
- staged: productData,
150
- current: productData,
151
- hasStagedChanges: false,
152
- published: false,
153
- },
154
- productType: {
155
- typeId: 'product-type',
156
- id: 'some-uuid',
157
- },
158
- version: 1,
159
- } as Product)
160
- })
102
+ test('Create product', async () => {
103
+ const response = await supertest(ctMock.app)
104
+ .post('/dummy/products')
105
+ .send(unpublishedProductDraft)
106
+
107
+ const productData: ProductData = {
108
+ name: {
109
+ 'nl-NL': 'test unpublished product',
110
+ },
111
+ slug: {
112
+ 'nl-NL': 'test-unpublished-product',
113
+ },
114
+ categories: [],
115
+ masterVariant: {
116
+ sku: '2337',
117
+ assets: [],
118
+ attributes: [
119
+ {
120
+ name: 'test',
121
+ value: 'test',
122
+ },
123
+ ],
124
+ id: 1,
125
+ images: [],
126
+ },
127
+ variants: [
128
+ {
129
+ sku: '2338',
130
+ assets: [],
131
+ id: 2,
132
+ images: [],
133
+ attributes: [
134
+ {
135
+ name: 'test2',
136
+ value: 'test2',
137
+ },
138
+ ],
139
+ },
140
+ ],
141
+ searchKeywords: {},
142
+ }
143
+
144
+ expect(response.body).toEqual({
145
+ createdAt: expect.anything(),
146
+ id: expect.anything(),
147
+ lastModifiedAt: expect.anything(),
148
+ masterData: {
149
+ staged: productData,
150
+ current: productData,
151
+ hasStagedChanges: false,
152
+ published: false,
153
+ },
154
+ productType: {
155
+ typeId: 'product-type',
156
+ id: 'some-uuid',
157
+ },
158
+ version: 1,
159
+ } as Product)
160
+ })
161
161
  })
162
162
 
163
163
  describe('Product update actions', () => {
164
- const ctMock = new CommercetoolsMock()
165
- let productPublished: Product | undefined
166
- let productUnpublished: Product | undefined
167
-
168
- beforeEach(async () => {
169
- let response
170
- response = await supertest(ctMock.app)
171
- .post('/dummy/products')
172
- .send(publishedProductDraft)
173
-
174
- expect(response.status).toBe(201)
175
- productPublished = response.body
176
-
177
- response = await supertest(ctMock.app)
178
- .post('/dummy/products')
179
- .send(unpublishedProductDraft)
180
-
181
- expect(response.status).toBe(201)
182
- productUnpublished = response.body
183
- })
184
-
185
- test('setAttribute masterVariant (staged)', async () => {
186
- assert(productPublished, 'product not created')
187
-
188
- {
189
- const response = await supertest(ctMock.app)
190
- .post(`/dummy/products/${productPublished.id}`)
191
- .send({
192
- version: 1,
193
- actions: [
194
- { action: 'setAttribute', sku: '1337', name: 'foo', value: 'bar' },
195
- ],
196
- })
197
-
198
- expect(response.status).toBe(200)
199
- const product: Product = response.body
200
- expect(product.version).toBe(2)
201
- expect(product.masterData.hasStagedChanges).toBeTruthy()
202
- expect(product.masterData.current.masterVariant.attributes).toHaveLength(
203
- 1
204
- )
205
- expect(product.masterData.staged.masterVariant.attributes).toHaveLength(2)
206
-
207
- const attr = response.body.masterData.staged.masterVariant.attributes[1]
208
- expect(attr).toEqual({ name: 'foo', value: 'bar' })
209
- }
210
-
211
- // Publish
212
- {
213
- const response = await supertest(ctMock.app)
214
- .post(`/dummy/products/${productPublished.id}`)
215
- .send({
216
- version: 2,
217
- actions: [{ action: 'publish', scope: 'All' }],
218
- })
219
-
220
- expect(response.status).toBe(200)
221
- const product: Product = response.body
222
- expect(product.version).toBe(3)
223
- expect(product.masterData.hasStagedChanges).toBeFalsy()
224
- expect(product.masterData.current.masterVariant.attributes).toHaveLength(
225
- 2
226
- )
227
- }
228
- })
229
-
230
- test('setAttribute masterVariant (published)', async () => {
231
- assert(productPublished, 'product not created')
232
-
233
- const response = await supertest(ctMock.app)
234
- .post(`/dummy/products/${productPublished.id}`)
235
- .send({
236
- version: 1,
237
- actions: [
238
- {
239
- action: 'setAttribute',
240
- sku: '1337',
241
- name: 'foo',
242
- value: 'bar',
243
- staged: false,
244
- },
245
- ],
246
- })
247
-
248
- expect(response.status).toBe(200)
249
- const product: Product = response.body
250
-
251
- // TODO: Since we auto publish it actually does two version updates. So the
252
- // version should be 3
253
- expect(product.version).toBe(2)
254
- expect(product.masterData.hasStagedChanges).toBeFalsy()
255
- expect(product.masterData.current.masterVariant.attributes).toHaveLength(2)
256
- expect(product.masterData.staged.masterVariant.attributes).toHaveLength(2)
257
-
258
- const attr = response.body.masterData.staged.masterVariant.attributes[1]
259
- expect(attr).toEqual({ name: 'foo', value: 'bar' })
260
- })
261
-
262
- test('setAttribute variant', async () => {
263
- assert(productPublished, 'product not created')
264
-
265
- const response = await supertest(ctMock.app)
266
- .post(`/dummy/products/${productPublished.id}`)
267
- .send({
268
- version: 1,
269
- actions: [
270
- { action: 'setAttribute', sku: '1338', name: 'foo', value: 'bar' },
271
- ],
272
- })
273
- expect(response.status).toBe(200)
274
- expect(response.body.version).toBe(2)
275
- expect(response.body.masterData.staged.variants[0].attributes).toHaveLength(
276
- 2
277
- )
278
- const attr = response.body.masterData.staged.variants[0].attributes[1]
279
- expect(attr).toEqual({ name: 'foo', value: 'bar' })
280
- })
281
-
282
- test('setAttribute variant and publish', async () => {
283
- assert(productPublished, 'product not created')
284
-
285
- const response = await supertest(ctMock.app)
286
- .post(`/dummy/products/${productPublished.id}`)
287
- .send({
288
- version: 1,
289
- actions: [
290
- { action: 'setAttribute', sku: '1338', name: 'foo', value: 'bar' },
291
- { action: 'publish' },
292
- ],
293
- })
294
- expect(response.status).toBe(200)
295
- expect(response.body.version).toBe(3)
296
- expect(
297
- response.body.masterData.current.variants[0].attributes
298
- ).toHaveLength(2)
299
- const attr = response.body.masterData.current.variants[0].attributes[1]
300
- expect(attr).toEqual({ name: 'foo', value: 'bar' })
301
- })
302
-
303
- test('setAttribute overwrite', async () => {
304
- assert(productPublished, 'product not created')
305
-
306
- const response = await supertest(ctMock.app)
307
- .post(`/dummy/products/${productPublished.id}`)
308
- .send({
309
- version: 1,
310
- actions: [
311
- { action: 'setAttribute', sku: '1337', name: 'test', value: 'foo' },
312
- ],
313
- })
314
- expect(response.status).toBe(200)
315
- expect(response.body.version).toBe(2)
316
- expect(
317
- response.body.masterData.staged.masterVariant.attributes
318
- ).toHaveLength(1)
319
- const attr = response.body.masterData.staged.masterVariant.attributes[0]
320
- expect(attr).toEqual({ name: 'test', value: 'foo' })
321
- })
322
-
323
- test('addExternalImage variant', async () => {
324
- assert(productPublished, 'product not created')
325
-
326
- const image: Image = {
327
- url: 'http://example.com/image',
328
- dimensions: { w: 100, h: 100 },
329
- }
330
- const response = await supertest(ctMock.app)
331
- .post(`/dummy/products/${productPublished.id}`)
332
- .send({
333
- version: 1,
334
- actions: [{ action: 'addExternalImage', sku: '1338', image }],
335
- })
336
- expect(response.status).toBe(200)
337
- expect(response.body.version).toBe(2)
338
- expect(response.body.masterData.staged.variants[0].images).toHaveLength(1)
339
- const attr = response.body.masterData.staged.variants[0].images[0]
340
- expect(attr).toEqual(image)
341
- })
342
-
343
- test('removeImage variant', async () => {
344
- assert(productPublished, 'product not created')
345
-
346
- const image: Image = {
347
- url: 'http://example.com/image',
348
- dimensions: { w: 100, h: 100 },
349
- }
350
-
351
- {
352
- const response = await supertest(ctMock.app)
353
- .post(`/dummy/products/${productPublished.id}`)
354
- .send({
355
- version: 1,
356
- actions: [{ action: 'addExternalImage', sku: '1338', image }],
357
- })
358
- expect(response.status).toBe(200)
359
- expect(response.body.version).toBe(2)
360
- }
361
-
362
- const response = await supertest(ctMock.app)
363
- .post(`/dummy/products/${productPublished.id}`)
364
- .send({
365
- version: 2,
366
- actions: [
367
- {
368
- action: 'removeImage',
369
- sku: '1338',
370
- imageUrl: image.url,
371
- },
372
- ],
373
- })
374
- expect(response.status).toBe(200)
375
- expect(response.body.version).toBe(3)
376
- expect(response.body.masterData.staged.variants[0].images).toHaveLength(0)
377
- })
378
-
379
- test('moveImageToPosition variant', async () => {
380
- assert(productPublished, 'product not created')
381
-
382
- const image1: Image = {
383
- url: 'http://example.com/image1',
384
- dimensions: { w: 100, h: 100 },
385
- }
386
- const image2: Image = {
387
- url: 'http://example.com/image2',
388
- dimensions: { w: 100, h: 100 },
389
- }
390
-
391
- {
392
- const response = await supertest(ctMock.app)
393
- .post(`/dummy/products/${productPublished.id}`)
394
- .send({
395
- version: 1,
396
- actions: [
397
- { action: 'addExternalImage', sku: '1338', image: image1 },
398
- { action: 'addExternalImage', sku: '1338', image: image2 },
399
- ],
400
- })
401
- expect(response.status).toBe(200)
402
- expect(response.body.version).toBe(3)
403
- }
404
-
405
- const response = await supertest(ctMock.app)
406
- .post(`/dummy/products/${productPublished.id}`)
407
- .send({
408
- version: 3,
409
- actions: [
410
- {
411
- action: 'moveImageToPosition',
412
- sku: '1338',
413
- imageUrl: image2.url,
414
- position: 0,
415
- },
416
- ],
417
- })
418
- expect(response.status).toBe(200)
419
- expect(response.body.version).toBe(4)
420
- expect(response.body.masterData.staged.variants[0].images).toEqual([
421
- { url: 'http://example.com/image2', dimensions: { w: 100, h: 100 } },
422
- { url: 'http://example.com/image1', dimensions: { w: 100, h: 100 } },
423
- ])
424
- })
425
-
426
- test('addPrice variant', async () => {
427
- assert(productPublished, 'product not created')
428
-
429
- const priceDraft: PriceDraft = {
430
- country: 'BE',
431
- value: {
432
- currencyCode: 'EUR',
433
- centAmount: 3000,
434
- },
435
- }
436
-
437
- const response = await supertest(ctMock.app)
438
- .post(`/dummy/products/${productPublished.id}`)
439
- .send({
440
- version: 1,
441
- actions: [
442
- {
443
- action: 'addPrice',
444
- price: priceDraft,
445
- variantId: 1,
446
- },
447
- ],
448
- })
449
- expect(response.status).toBe(200)
450
- expect(response.body.version).toBe(2)
451
- expect(response.body.masterData.staged.masterVariant.prices).toMatchObject([
452
- {
453
- country: 'NL',
454
- value: {
455
- currencyCode: 'EUR',
456
- centAmount: 1000,
457
- },
458
- },
459
- {
460
- country: 'BE',
461
- value: {
462
- currencyCode: 'EUR',
463
- centAmount: 3000,
464
- },
465
- },
466
- ])
467
- })
468
-
469
- test('changePrice variant', async () => {
470
- assert(productPublished, 'product not created')
471
- const priceId =
472
- productPublished?.masterData.current.masterVariant.prices?.[0].id
473
- assert(priceId)
474
-
475
- const priceDraft: PriceDraft = {
476
- country: 'BE',
477
- value: {
478
- currencyCode: 'EUR',
479
- centAmount: 3000,
480
- },
481
- }
482
-
483
- const response = await supertest(ctMock.app)
484
- .post(`/dummy/products/${productPublished.id}`)
485
- .send({
486
- version: 1,
487
- actions: [
488
- {
489
- action: 'changePrice',
490
- priceId,
491
- price: priceDraft,
492
- },
493
- ],
494
- })
495
- expect(response.status).toBe(200)
496
- expect(response.body.version).toBe(2)
497
- expect(response.body.masterData.staged.masterVariant.prices).toMatchObject([
498
- {
499
- id: priceId,
500
- country: 'BE',
501
- value: {
502
- currencyCode: 'EUR',
503
- centAmount: 3000,
504
- },
505
- },
506
- ])
507
- })
508
-
509
- test('removePrice variant', async () => {
510
- assert(productPublished, 'product not created')
511
- const priceId =
512
- productPublished?.masterData.current.masterVariant.prices?.[0].id
513
- assert(priceId)
514
-
515
- const response = await supertest(ctMock.app)
516
- .post(`/dummy/products/${productPublished.id}`)
517
- .send({
518
- version: 1,
519
- actions: [
520
- {
521
- action: 'removePrice',
522
- priceId,
523
- },
524
- ],
525
- })
526
- expect(response.status).toBe(200)
527
- expect(response.body.version).toBe(2)
528
- expect(response.body.masterData.staged.masterVariant.prices).toHaveLength(0)
529
- })
164
+ const ctMock = new CommercetoolsMock()
165
+ let productPublished: Product | undefined
166
+ let productUnpublished: Product | undefined
167
+
168
+ beforeEach(async () => {
169
+ let response
170
+ response = await supertest(ctMock.app)
171
+ .post('/dummy/products')
172
+ .send(publishedProductDraft)
173
+
174
+ expect(response.status).toBe(201)
175
+ productPublished = response.body
176
+
177
+ response = await supertest(ctMock.app)
178
+ .post('/dummy/products')
179
+ .send(unpublishedProductDraft)
180
+
181
+ expect(response.status).toBe(201)
182
+ productUnpublished = response.body
183
+ })
184
+
185
+ test('setAttribute masterVariant (staged)', async () => {
186
+ assert(productPublished, 'product not created')
187
+
188
+ {
189
+ const response = await supertest(ctMock.app)
190
+ .post(`/dummy/products/${productPublished.id}`)
191
+ .send({
192
+ version: 1,
193
+ actions: [
194
+ { action: 'setAttribute', sku: '1337', name: 'foo', value: 'bar' },
195
+ ],
196
+ })
197
+
198
+ expect(response.status).toBe(200)
199
+ const product: Product = response.body
200
+ expect(product.version).toBe(2)
201
+ expect(product.masterData.hasStagedChanges).toBeTruthy()
202
+ expect(product.masterData.current.masterVariant.attributes).toHaveLength(
203
+ 1
204
+ )
205
+ expect(product.masterData.staged.masterVariant.attributes).toHaveLength(2)
206
+
207
+ const attr = response.body.masterData.staged.masterVariant.attributes[1]
208
+ expect(attr).toEqual({ name: 'foo', value: 'bar' })
209
+ }
210
+
211
+ // Publish
212
+ {
213
+ const response = await supertest(ctMock.app)
214
+ .post(`/dummy/products/${productPublished.id}`)
215
+ .send({
216
+ version: 2,
217
+ actions: [{ action: 'publish', scope: 'All' }],
218
+ })
219
+
220
+ expect(response.status).toBe(200)
221
+ const product: Product = response.body
222
+ expect(product.version).toBe(3)
223
+ expect(product.masterData.hasStagedChanges).toBeFalsy()
224
+ expect(product.masterData.current.masterVariant.attributes).toHaveLength(
225
+ 2
226
+ )
227
+ }
228
+ })
229
+
230
+ test('setAttribute masterVariant (published)', async () => {
231
+ assert(productPublished, 'product not created')
232
+
233
+ const response = await supertest(ctMock.app)
234
+ .post(`/dummy/products/${productPublished.id}`)
235
+ .send({
236
+ version: 1,
237
+ actions: [
238
+ {
239
+ action: 'setAttribute',
240
+ sku: '1337',
241
+ name: 'foo',
242
+ value: 'bar',
243
+ staged: false,
244
+ },
245
+ ],
246
+ })
247
+
248
+ expect(response.status).toBe(200)
249
+ const product: Product = response.body
250
+
251
+ // TODO: Since we auto publish it actually does two version updates. So the
252
+ // version should be 3
253
+ expect(product.version).toBe(2)
254
+ expect(product.masterData.hasStagedChanges).toBeFalsy()
255
+ expect(product.masterData.current.masterVariant.attributes).toHaveLength(2)
256
+ expect(product.masterData.staged.masterVariant.attributes).toHaveLength(2)
257
+
258
+ const attr = response.body.masterData.staged.masterVariant.attributes[1]
259
+ expect(attr).toEqual({ name: 'foo', value: 'bar' })
260
+ })
261
+
262
+ test('setAttribute variant', async () => {
263
+ assert(productPublished, 'product not created')
264
+
265
+ const response = await supertest(ctMock.app)
266
+ .post(`/dummy/products/${productPublished.id}`)
267
+ .send({
268
+ version: 1,
269
+ actions: [
270
+ { action: 'setAttribute', sku: '1338', name: 'foo', value: 'bar' },
271
+ ],
272
+ })
273
+ expect(response.status).toBe(200)
274
+ expect(response.body.version).toBe(2)
275
+ expect(response.body.masterData.staged.variants[0].attributes).toHaveLength(
276
+ 2
277
+ )
278
+ const attr = response.body.masterData.staged.variants[0].attributes[1]
279
+ expect(attr).toEqual({ name: 'foo', value: 'bar' })
280
+ })
281
+
282
+ test('setAttribute variant and publish', async () => {
283
+ assert(productPublished, 'product not created')
284
+
285
+ const response = await supertest(ctMock.app)
286
+ .post(`/dummy/products/${productPublished.id}`)
287
+ .send({
288
+ version: 1,
289
+ actions: [
290
+ { action: 'setAttribute', sku: '1338', name: 'foo', value: 'bar' },
291
+ { action: 'publish' },
292
+ ],
293
+ })
294
+ expect(response.status).toBe(200)
295
+ expect(response.body.version).toBe(3)
296
+ expect(
297
+ response.body.masterData.current.variants[0].attributes
298
+ ).toHaveLength(2)
299
+ const attr = response.body.masterData.current.variants[0].attributes[1]
300
+ expect(attr).toEqual({ name: 'foo', value: 'bar' })
301
+ })
302
+
303
+ test('setAttribute overwrite', async () => {
304
+ assert(productPublished, 'product not created')
305
+
306
+ const response = await supertest(ctMock.app)
307
+ .post(`/dummy/products/${productPublished.id}`)
308
+ .send({
309
+ version: 1,
310
+ actions: [
311
+ { action: 'setAttribute', sku: '1337', name: 'test', value: 'foo' },
312
+ ],
313
+ })
314
+ expect(response.status).toBe(200)
315
+ expect(response.body.version).toBe(2)
316
+ expect(
317
+ response.body.masterData.staged.masterVariant.attributes
318
+ ).toHaveLength(1)
319
+ const attr = response.body.masterData.staged.masterVariant.attributes[0]
320
+ expect(attr).toEqual({ name: 'test', value: 'foo' })
321
+ })
322
+
323
+ test('addExternalImage variant', async () => {
324
+ assert(productPublished, 'product not created')
325
+
326
+ const image: Image = {
327
+ url: 'http://example.com/image',
328
+ dimensions: { w: 100, h: 100 },
329
+ }
330
+ const response = await supertest(ctMock.app)
331
+ .post(`/dummy/products/${productPublished.id}`)
332
+ .send({
333
+ version: 1,
334
+ actions: [{ action: 'addExternalImage', sku: '1338', image }],
335
+ })
336
+ expect(response.status).toBe(200)
337
+ expect(response.body.version).toBe(2)
338
+ expect(response.body.masterData.staged.variants[0].images).toHaveLength(1)
339
+ const attr = response.body.masterData.staged.variants[0].images[0]
340
+ expect(attr).toEqual(image)
341
+ })
342
+
343
+ test('removeImage variant', async () => {
344
+ assert(productPublished, 'product not created')
345
+
346
+ const image: Image = {
347
+ url: 'http://example.com/image',
348
+ dimensions: { w: 100, h: 100 },
349
+ }
350
+
351
+ {
352
+ const response = await supertest(ctMock.app)
353
+ .post(`/dummy/products/${productPublished.id}`)
354
+ .send({
355
+ version: 1,
356
+ actions: [{ action: 'addExternalImage', sku: '1338', image }],
357
+ })
358
+ expect(response.status).toBe(200)
359
+ expect(response.body.version).toBe(2)
360
+ }
361
+
362
+ const response = await supertest(ctMock.app)
363
+ .post(`/dummy/products/${productPublished.id}`)
364
+ .send({
365
+ version: 2,
366
+ actions: [
367
+ {
368
+ action: 'removeImage',
369
+ sku: '1338',
370
+ imageUrl: image.url,
371
+ },
372
+ ],
373
+ })
374
+ expect(response.status).toBe(200)
375
+ expect(response.body.version).toBe(3)
376
+ expect(response.body.masterData.staged.variants[0].images).toHaveLength(0)
377
+ })
378
+
379
+ test('moveImageToPosition variant', async () => {
380
+ assert(productPublished, 'product not created')
381
+
382
+ const image1: Image = {
383
+ url: 'http://example.com/image1',
384
+ dimensions: { w: 100, h: 100 },
385
+ }
386
+ const image2: Image = {
387
+ url: 'http://example.com/image2',
388
+ dimensions: { w: 100, h: 100 },
389
+ }
390
+
391
+ {
392
+ const response = await supertest(ctMock.app)
393
+ .post(`/dummy/products/${productPublished.id}`)
394
+ .send({
395
+ version: 1,
396
+ actions: [
397
+ { action: 'addExternalImage', sku: '1338', image: image1 },
398
+ { action: 'addExternalImage', sku: '1338', image: image2 },
399
+ ],
400
+ })
401
+ expect(response.status).toBe(200)
402
+ expect(response.body.version).toBe(3)
403
+ }
404
+
405
+ const response = await supertest(ctMock.app)
406
+ .post(`/dummy/products/${productPublished.id}`)
407
+ .send({
408
+ version: 3,
409
+ actions: [
410
+ {
411
+ action: 'moveImageToPosition',
412
+ sku: '1338',
413
+ imageUrl: image2.url,
414
+ position: 0,
415
+ },
416
+ ],
417
+ })
418
+ expect(response.status).toBe(200)
419
+ expect(response.body.version).toBe(4)
420
+ expect(response.body.masterData.staged.variants[0].images).toEqual([
421
+ { url: 'http://example.com/image2', dimensions: { w: 100, h: 100 } },
422
+ { url: 'http://example.com/image1', dimensions: { w: 100, h: 100 } },
423
+ ])
424
+ })
425
+
426
+ test('addPrice variant', async () => {
427
+ assert(productPublished, 'product not created')
428
+
429
+ const priceDraft: PriceDraft = {
430
+ country: 'BE',
431
+ value: {
432
+ currencyCode: 'EUR',
433
+ centAmount: 3000,
434
+ },
435
+ }
436
+
437
+ const response = await supertest(ctMock.app)
438
+ .post(`/dummy/products/${productPublished.id}`)
439
+ .send({
440
+ version: 1,
441
+ actions: [
442
+ {
443
+ action: 'addPrice',
444
+ price: priceDraft,
445
+ variantId: 1,
446
+ },
447
+ ],
448
+ })
449
+ expect(response.status).toBe(200)
450
+ expect(response.body.version).toBe(2)
451
+ expect(response.body.masterData.staged.masterVariant.prices).toMatchObject([
452
+ {
453
+ country: 'NL',
454
+ value: {
455
+ currencyCode: 'EUR',
456
+ centAmount: 1000,
457
+ },
458
+ },
459
+ {
460
+ country: 'BE',
461
+ value: {
462
+ currencyCode: 'EUR',
463
+ centAmount: 3000,
464
+ },
465
+ },
466
+ ])
467
+ })
468
+
469
+ test('changePrice variant', async () => {
470
+ assert(productPublished, 'product not created')
471
+ const priceId =
472
+ productPublished?.masterData.current.masterVariant.prices?.[0].id
473
+ assert(priceId)
474
+
475
+ const priceDraft: PriceDraft = {
476
+ country: 'BE',
477
+ value: {
478
+ currencyCode: 'EUR',
479
+ centAmount: 3000,
480
+ },
481
+ }
482
+
483
+ const response = await supertest(ctMock.app)
484
+ .post(`/dummy/products/${productPublished.id}`)
485
+ .send({
486
+ version: 1,
487
+ actions: [
488
+ {
489
+ action: 'changePrice',
490
+ priceId,
491
+ price: priceDraft,
492
+ },
493
+ ],
494
+ })
495
+ expect(response.status).toBe(200)
496
+ expect(response.body.version).toBe(2)
497
+ expect(response.body.masterData.staged.masterVariant.prices).toMatchObject([
498
+ {
499
+ id: priceId,
500
+ country: 'BE',
501
+ value: {
502
+ currencyCode: 'EUR',
503
+ centAmount: 3000,
504
+ },
505
+ },
506
+ ])
507
+ })
508
+
509
+ test('removePrice variant', async () => {
510
+ assert(productPublished, 'product not created')
511
+ const priceId =
512
+ productPublished?.masterData.current.masterVariant.prices?.[0].id
513
+ assert(priceId)
514
+
515
+ const response = await supertest(ctMock.app)
516
+ .post(`/dummy/products/${productPublished.id}`)
517
+ .send({
518
+ version: 1,
519
+ actions: [
520
+ {
521
+ action: 'removePrice',
522
+ priceId,
523
+ },
524
+ ],
525
+ })
526
+ expect(response.status).toBe(200)
527
+ expect(response.body.version).toBe(2)
528
+ expect(response.body.masterData.staged.masterVariant.prices).toHaveLength(0)
529
+ })
530
530
  })