@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,8 +1,8 @@
1
1
  import type {
2
- Address,
3
- Cart,
4
- CentPrecisionMoney,
5
- ProductDraft,
2
+ Address,
3
+ Cart,
4
+ CentPrecisionMoney,
5
+ ProductDraft,
6
6
  } from '@commercetools/platform-sdk'
7
7
  import assert from 'assert'
8
8
  import supertest from 'supertest'
@@ -10,412 +10,412 @@ import { afterEach, beforeEach, describe, expect, test } from 'vitest'
10
10
  import { CommercetoolsMock } from '../index.js'
11
11
 
12
12
  describe('Carts Query', () => {
13
- const ctMock = new CommercetoolsMock()
14
-
15
- beforeEach(async () => {
16
- let response
17
- response = await supertest(ctMock.app)
18
- .post('/dummy/types')
19
- .send({
20
- key: 'my-cart',
21
- name: {
22
- en: 'Test',
23
- },
24
- description: {
25
- en: 'Test Type',
26
- },
27
- resourceTypeIds: ['order'],
28
- fieldDefinitions: [
29
- {
30
- name: 'offer_name',
31
- label: {
32
- en: 'offer_name',
33
- },
34
- required: false,
35
- type: {
36
- name: 'String',
37
- },
38
- inputHint: 'SingleLine',
39
- },
40
- ],
41
- })
42
- expect(response.status).toBe(201)
43
-
44
- response = await supertest(ctMock.app)
45
- .post('/dummy/carts')
46
- .send({
47
- currency: 'EUR',
48
- custom: {
49
- type: {
50
- typeId: 'type',
51
- key: 'my-cart',
52
- },
53
- fields: {
54
- description: 'example description',
55
- },
56
- },
57
- })
58
- expect(response.status).toBe(201)
59
- })
60
-
61
- test('no filter', async () => {
62
- const response = await supertest(ctMock.app)
63
- .get('/dummy/carts')
64
- .query({
65
- expand: 'custom.type',
66
- })
67
- .send()
68
-
69
- expect(response.status).toBe(200)
70
- expect(response.body.count).toBe(1)
71
-
72
- const myCart = response.body.results[0] as Cart
73
-
74
- expect(myCart.custom?.type.id).not.toBeUndefined()
75
- expect(myCart.custom?.type.id).toBe(myCart.custom?.type.obj?.id)
76
- expect(myCart.custom?.type.obj?.description?.en).toBe('Test Type')
77
- })
13
+ const ctMock = new CommercetoolsMock()
14
+
15
+ beforeEach(async () => {
16
+ let response
17
+ response = await supertest(ctMock.app)
18
+ .post('/dummy/types')
19
+ .send({
20
+ key: 'my-cart',
21
+ name: {
22
+ en: 'Test',
23
+ },
24
+ description: {
25
+ en: 'Test Type',
26
+ },
27
+ resourceTypeIds: ['order'],
28
+ fieldDefinitions: [
29
+ {
30
+ name: 'offer_name',
31
+ label: {
32
+ en: 'offer_name',
33
+ },
34
+ required: false,
35
+ type: {
36
+ name: 'String',
37
+ },
38
+ inputHint: 'SingleLine',
39
+ },
40
+ ],
41
+ })
42
+ expect(response.status).toBe(201)
43
+
44
+ response = await supertest(ctMock.app)
45
+ .post('/dummy/carts')
46
+ .send({
47
+ currency: 'EUR',
48
+ custom: {
49
+ type: {
50
+ typeId: 'type',
51
+ key: 'my-cart',
52
+ },
53
+ fields: {
54
+ description: 'example description',
55
+ },
56
+ },
57
+ })
58
+ expect(response.status).toBe(201)
59
+ })
60
+
61
+ test('no filter', async () => {
62
+ const response = await supertest(ctMock.app)
63
+ .get('/dummy/carts')
64
+ .query({
65
+ expand: 'custom.type',
66
+ })
67
+ .send()
68
+
69
+ expect(response.status).toBe(200)
70
+ expect(response.body.count).toBe(1)
71
+
72
+ const myCart = response.body.results[0] as Cart
73
+
74
+ expect(myCart.custom?.type.id).not.toBeUndefined()
75
+ expect(myCart.custom?.type.id).toBe(myCart.custom?.type.obj?.id)
76
+ expect(myCart.custom?.type.obj?.description?.en).toBe('Test Type')
77
+ })
78
78
  })
79
79
 
80
80
  describe('Cart Update Actions', () => {
81
- const ctMock = new CommercetoolsMock()
82
- let cart: Cart | undefined
83
-
84
- const createCart = async (currency: string) => {
85
- const response = await supertest(ctMock.app).post('/dummy/carts').send({
86
- currency,
87
- })
88
- expect(response.status).toBe(201)
89
- cart = response.body
90
- }
91
-
92
- const productDraft: ProductDraft = {
93
- name: {
94
- 'nl-NL': 'test product',
95
- },
96
- productType: {
97
- typeId: 'product-type',
98
- id: 'some-uuid',
99
- },
100
- masterVariant: {
101
- sku: '1337',
102
- prices: [
103
- {
104
- value: {
105
- type: 'centPrecision',
106
- currencyCode: 'EUR',
107
- centAmount: 14900,
108
- fractionDigits: 2,
109
- } as CentPrecisionMoney,
110
- },
111
- {
112
- value: {
113
- type: 'centPrecision',
114
- currencyCode: 'GBP',
115
- centAmount: 18900,
116
- fractionDigits: 2,
117
- } as CentPrecisionMoney,
118
- },
119
- ],
120
-
121
- attributes: [
122
- {
123
- name: 'test',
124
- value: 'test',
125
- },
126
- ],
127
- },
128
- variants: [
129
- {
130
- sku: '1338',
131
- prices: [
132
- {
133
- value: {
134
- type: 'centPrecision',
135
- currencyCode: 'EUR',
136
- centAmount: 14900,
137
- fractionDigits: 2,
138
- } as CentPrecisionMoney,
139
- },
140
- ],
141
- attributes: [
142
- {
143
- name: 'test2',
144
- value: 'test2',
145
- },
146
- ],
147
- },
148
- ],
149
- slug: {
150
- 'nl-NL': 'test-product',
151
- },
152
- publish: true,
153
- }
154
-
155
- beforeEach(async () => {
156
- await createCart('EUR')
157
- })
158
-
159
- afterEach(() => {
160
- ctMock.clear()
161
- })
162
-
163
- test('no update', async () => {
164
- assert(cart, 'cart not created')
165
-
166
- const response = await supertest(ctMock.app)
167
- .post(`/dummy/carts/${cart.id}`)
168
- .send({
169
- version: 1,
170
- actions: [{ action: 'setLocale', locale: 'nl-NL' }],
171
- })
172
- expect(response.status).toBe(200)
173
- expect(response.body.version).toBe(2)
174
- expect(response.body.locale).toBe('nl-NL')
175
-
176
- const responseAgain = await supertest(ctMock.app)
177
- .post(`/dummy/carts/${cart.id}`)
178
- .send({
179
- version: 2,
180
- actions: [{ action: 'setLocale', locale: 'nl-NL' }],
181
- })
182
- expect(responseAgain.status).toBe(200)
183
- expect(responseAgain.body.version).toBe(2)
184
- expect(responseAgain.body.locale).toBe('nl-NL')
185
- })
186
-
187
- test('addLineItem', async () => {
188
- const product = await supertest(ctMock.app)
189
- .post(`/dummy/products`)
190
- .send(productDraft)
191
- .then((x) => x.body)
192
-
193
- assert(cart, 'cart not created')
194
- assert(product, 'product not created')
195
-
196
- const response = await supertest(ctMock.app)
197
- .post(`/dummy/carts/${cart.id}`)
198
- .send({
199
- version: 1,
200
- actions: [
201
- {
202
- action: 'addLineItem',
203
- productId: product.id,
204
- variantId: product.masterData.current.variants[0].id,
205
- },
206
- ],
207
- })
208
- expect(response.status).toBe(200)
209
- expect(response.body.version).toBe(2)
210
- expect(response.body.lineItems).toHaveLength(1)
211
- expect(response.body.totalPrice.centAmount).toEqual(14900)
212
- })
213
-
214
- test('addLineItem by SKU', async () => {
215
- const product = await supertest(ctMock.app)
216
- .post(`/dummy/products`)
217
- .send(productDraft)
218
- .then((x) => x.body)
219
-
220
- assert(cart, 'cart not created')
221
- assert(product, 'product not created')
222
-
223
- const response = await supertest(ctMock.app)
224
- .post(`/dummy/carts/${cart.id}`)
225
- .send({
226
- version: 1,
227
- actions: [{ action: 'addLineItem', sku: '1337', quantity: 2 }],
228
- })
229
- expect(response.status).toBe(200)
230
- expect(response.body.version).toBe(2)
231
- expect(response.body.lineItems).toHaveLength(1)
232
- expect(response.body.totalPrice.centAmount).toEqual(29800)
233
- })
234
-
235
- test.each([
236
- ['EUR', 29800],
237
- ['GBP', 37800],
238
- ])('addLineItem with price selection', async (currency, total) => {
239
- await createCart(currency)
240
-
241
- const product = await supertest(ctMock.app)
242
- .post(`/dummy/products`)
243
- .send(productDraft)
244
- .then((x) => x.body)
245
-
246
- assert(cart, 'cart not created')
247
- assert(product, 'product not created')
248
-
249
- const response = await supertest(ctMock.app)
250
- .post(`/dummy/carts/${cart.id}`)
251
- .send({
252
- version: 1,
253
- actions: [{ action: 'addLineItem', sku: '1337', quantity: 2 }],
254
- })
255
- expect(response.status).toBe(200)
256
- expect(response.body.version).toBe(2)
257
- expect(response.body.lineItems).toHaveLength(1)
258
- expect(response.body.lineItems[0].price.value.currencyCode).toBe(currency)
259
- expect(response.body.totalPrice.centAmount).toEqual(total)
260
- })
261
-
262
- test('addLineItem unknown product', async () => {
263
- assert(cart, 'cart not created')
264
-
265
- const response = await supertest(ctMock.app)
266
- .post(`/dummy/carts/${cart.id}`)
267
- .send({
268
- version: 1,
269
- actions: [{ action: 'addLineItem', productId: '123', variantId: 1 }],
270
- })
271
- expect(response.status).toBe(400)
272
- expect(response.body.message).toBe("A product with ID '123' not found.")
273
- })
274
-
275
- test('removeLineItem', async () => {
276
- const product = await supertest(ctMock.app)
277
- .post(`/dummy/products`)
278
- .send(productDraft)
279
- .then((x) => x.body)
280
-
281
- assert(cart, 'cart not created')
282
- assert(product, 'product not created')
283
-
284
- const updatedCart = await supertest(ctMock.app)
285
- .post(`/dummy/carts/${cart.id}`)
286
- .send({
287
- version: 1,
288
- actions: [
289
- {
290
- action: 'addLineItem',
291
- productId: product.id,
292
- variantId: product.masterData.current.variants[0].id,
293
- },
294
- ],
295
- })
296
- const lineItem = updatedCart.body.lineItems[0]
297
- assert(lineItem, 'lineItem not created')
298
-
299
- expect(updatedCart.body.lineItems).toHaveLength(1)
300
-
301
- const response = await supertest(ctMock.app)
302
- .post(`/dummy/carts/${cart.id}`)
303
- .send({
304
- version: updatedCart.body.version,
305
- actions: [{ action: 'removeLineItem', lineItemId: lineItem.id }],
306
- })
307
- expect(response.status).toBe(200)
308
- expect(response.body.version).toBe(3)
309
- expect(response.body.lineItems).toHaveLength(0)
310
- })
311
-
312
- test('removeLineItem decrease quantity', async () => {
313
- const product = await supertest(ctMock.app)
314
- .post(`/dummy/products`)
315
- .send(productDraft)
316
- .then((x) => x.body)
317
-
318
- assert(cart, 'cart not created')
319
- assert(product, 'product not created')
320
-
321
- const updatedCart = await supertest(ctMock.app)
322
- .post(`/dummy/carts/${cart.id}`)
323
- .send({
324
- version: 1,
325
- actions: [
326
- {
327
- action: 'addLineItem',
328
- productId: product.id,
329
- variantId: product.masterData.current.variants[0].id,
330
- quantity: 2,
331
- },
332
- ],
333
- })
334
- const lineItem = updatedCart.body.lineItems[0]
335
- assert(lineItem, 'lineItem not created')
336
-
337
- expect(updatedCart.body.lineItems).toHaveLength(1)
338
- expect(updatedCart.body.lineItems[0].quantity).toBe(2)
339
-
340
- const response = await supertest(ctMock.app)
341
- .post(`/dummy/carts/${cart.id}`)
342
- .send({
343
- version: updatedCart.body.version,
344
- actions: [
345
- { action: 'removeLineItem', lineItemId: lineItem.id, quantity: 1 },
346
- ],
347
- })
348
- expect(response.status).toBe(200)
349
- expect(response.body.version).toBe(3)
350
- expect(response.body.lineItems).toHaveLength(1)
351
- expect(response.body.lineItems[0].quantity).toBe(1)
352
- })
353
-
354
- test('setBillingAddress', async () => {
355
- assert(cart, 'cart not created')
356
-
357
- const address: Address = {
358
- streetName: 'Street name',
359
- city: 'Utrecht',
360
- country: 'NL',
361
- }
362
-
363
- const response = await supertest(ctMock.app)
364
- .post(`/dummy/carts/${cart.id}`)
365
- .send({
366
- version: 1,
367
- actions: [{ action: 'setBillingAddress', address }],
368
- })
369
- expect(response.status).toBe(200)
370
- expect(response.body.version).toBe(2)
371
- expect(response.body.billingAddress).toEqual(address)
372
- })
373
-
374
- test('setCountry', async () => {
375
- assert(cart, 'cart not created')
376
-
377
- const response = await supertest(ctMock.app)
378
- .post(`/dummy/carts/${cart.id}`)
379
- .send({
380
- version: 1,
381
- actions: [{ action: 'setCountry', country: 'NL' }],
382
- })
383
- expect(response.status).toBe(200)
384
- expect(response.body.version).toBe(2)
385
- expect(response.body.country).toBe('NL')
386
- })
387
-
388
- test('setCustomerEmail', async () => {
389
- assert(cart, 'cart not created')
390
-
391
- const response = await supertest(ctMock.app)
392
- .post(`/dummy/carts/${cart.id}`)
393
- .send({
394
- version: 1,
395
- actions: [{ action: 'setCustomerEmail', email: 'john@doe.com' }],
396
- })
397
- expect(response.status).toBe(200)
398
- expect(response.body.version).toBe(2)
399
- expect(response.body.customerEmail).toBe('john@doe.com')
400
- })
401
-
402
- test('setShippingAddress', async () => {
403
- assert(cart, 'cart not created')
404
-
405
- const address: Address = {
406
- streetName: 'Street name',
407
- city: 'Utrecht',
408
- country: 'NL',
409
- }
410
-
411
- const response = await supertest(ctMock.app)
412
- .post(`/dummy/carts/${cart.id}`)
413
- .send({
414
- version: 1,
415
- actions: [{ action: 'setShippingAddress', address }],
416
- })
417
- expect(response.status).toBe(200)
418
- expect(response.body.version).toBe(2)
419
- expect(response.body.shippingAddress).toEqual(address)
420
- })
81
+ const ctMock = new CommercetoolsMock()
82
+ let cart: Cart | undefined
83
+
84
+ const createCart = async (currency: string) => {
85
+ const response = await supertest(ctMock.app).post('/dummy/carts').send({
86
+ currency,
87
+ })
88
+ expect(response.status).toBe(201)
89
+ cart = response.body
90
+ }
91
+
92
+ const productDraft: ProductDraft = {
93
+ name: {
94
+ 'nl-NL': 'test product',
95
+ },
96
+ productType: {
97
+ typeId: 'product-type',
98
+ id: 'some-uuid',
99
+ },
100
+ masterVariant: {
101
+ sku: '1337',
102
+ prices: [
103
+ {
104
+ value: {
105
+ type: 'centPrecision',
106
+ currencyCode: 'EUR',
107
+ centAmount: 14900,
108
+ fractionDigits: 2,
109
+ } as CentPrecisionMoney,
110
+ },
111
+ {
112
+ value: {
113
+ type: 'centPrecision',
114
+ currencyCode: 'GBP',
115
+ centAmount: 18900,
116
+ fractionDigits: 2,
117
+ } as CentPrecisionMoney,
118
+ },
119
+ ],
120
+
121
+ attributes: [
122
+ {
123
+ name: 'test',
124
+ value: 'test',
125
+ },
126
+ ],
127
+ },
128
+ variants: [
129
+ {
130
+ sku: '1338',
131
+ prices: [
132
+ {
133
+ value: {
134
+ type: 'centPrecision',
135
+ currencyCode: 'EUR',
136
+ centAmount: 14900,
137
+ fractionDigits: 2,
138
+ } as CentPrecisionMoney,
139
+ },
140
+ ],
141
+ attributes: [
142
+ {
143
+ name: 'test2',
144
+ value: 'test2',
145
+ },
146
+ ],
147
+ },
148
+ ],
149
+ slug: {
150
+ 'nl-NL': 'test-product',
151
+ },
152
+ publish: true,
153
+ }
154
+
155
+ beforeEach(async () => {
156
+ await createCart('EUR')
157
+ })
158
+
159
+ afterEach(() => {
160
+ ctMock.clear()
161
+ })
162
+
163
+ test('no update', async () => {
164
+ assert(cart, 'cart not created')
165
+
166
+ const response = await supertest(ctMock.app)
167
+ .post(`/dummy/carts/${cart.id}`)
168
+ .send({
169
+ version: 1,
170
+ actions: [{ action: 'setLocale', locale: 'nl-NL' }],
171
+ })
172
+ expect(response.status).toBe(200)
173
+ expect(response.body.version).toBe(2)
174
+ expect(response.body.locale).toBe('nl-NL')
175
+
176
+ const responseAgain = await supertest(ctMock.app)
177
+ .post(`/dummy/carts/${cart.id}`)
178
+ .send({
179
+ version: 2,
180
+ actions: [{ action: 'setLocale', locale: 'nl-NL' }],
181
+ })
182
+ expect(responseAgain.status).toBe(200)
183
+ expect(responseAgain.body.version).toBe(2)
184
+ expect(responseAgain.body.locale).toBe('nl-NL')
185
+ })
186
+
187
+ test('addLineItem', async () => {
188
+ const product = await supertest(ctMock.app)
189
+ .post(`/dummy/products`)
190
+ .send(productDraft)
191
+ .then((x) => x.body)
192
+
193
+ assert(cart, 'cart not created')
194
+ assert(product, 'product not created')
195
+
196
+ const response = await supertest(ctMock.app)
197
+ .post(`/dummy/carts/${cart.id}`)
198
+ .send({
199
+ version: 1,
200
+ actions: [
201
+ {
202
+ action: 'addLineItem',
203
+ productId: product.id,
204
+ variantId: product.masterData.current.variants[0].id,
205
+ },
206
+ ],
207
+ })
208
+ expect(response.status).toBe(200)
209
+ expect(response.body.version).toBe(2)
210
+ expect(response.body.lineItems).toHaveLength(1)
211
+ expect(response.body.totalPrice.centAmount).toEqual(14900)
212
+ })
213
+
214
+ test('addLineItem by SKU', async () => {
215
+ const product = await supertest(ctMock.app)
216
+ .post(`/dummy/products`)
217
+ .send(productDraft)
218
+ .then((x) => x.body)
219
+
220
+ assert(cart, 'cart not created')
221
+ assert(product, 'product not created')
222
+
223
+ const response = await supertest(ctMock.app)
224
+ .post(`/dummy/carts/${cart.id}`)
225
+ .send({
226
+ version: 1,
227
+ actions: [{ action: 'addLineItem', sku: '1337', quantity: 2 }],
228
+ })
229
+ expect(response.status).toBe(200)
230
+ expect(response.body.version).toBe(2)
231
+ expect(response.body.lineItems).toHaveLength(1)
232
+ expect(response.body.totalPrice.centAmount).toEqual(29800)
233
+ })
234
+
235
+ test.each([
236
+ ['EUR', 29800],
237
+ ['GBP', 37800],
238
+ ])('addLineItem with price selection', async (currency, total) => {
239
+ await createCart(currency)
240
+
241
+ const product = await supertest(ctMock.app)
242
+ .post(`/dummy/products`)
243
+ .send(productDraft)
244
+ .then((x) => x.body)
245
+
246
+ assert(cart, 'cart not created')
247
+ assert(product, 'product not created')
248
+
249
+ const response = await supertest(ctMock.app)
250
+ .post(`/dummy/carts/${cart.id}`)
251
+ .send({
252
+ version: 1,
253
+ actions: [{ action: 'addLineItem', sku: '1337', quantity: 2 }],
254
+ })
255
+ expect(response.status).toBe(200)
256
+ expect(response.body.version).toBe(2)
257
+ expect(response.body.lineItems).toHaveLength(1)
258
+ expect(response.body.lineItems[0].price.value.currencyCode).toBe(currency)
259
+ expect(response.body.totalPrice.centAmount).toEqual(total)
260
+ })
261
+
262
+ test('addLineItem unknown product', async () => {
263
+ assert(cart, 'cart not created')
264
+
265
+ const response = await supertest(ctMock.app)
266
+ .post(`/dummy/carts/${cart.id}`)
267
+ .send({
268
+ version: 1,
269
+ actions: [{ action: 'addLineItem', productId: '123', variantId: 1 }],
270
+ })
271
+ expect(response.status).toBe(400)
272
+ expect(response.body.message).toBe("A product with ID '123' not found.")
273
+ })
274
+
275
+ test('removeLineItem', async () => {
276
+ const product = await supertest(ctMock.app)
277
+ .post(`/dummy/products`)
278
+ .send(productDraft)
279
+ .then((x) => x.body)
280
+
281
+ assert(cart, 'cart not created')
282
+ assert(product, 'product not created')
283
+
284
+ const updatedCart = await supertest(ctMock.app)
285
+ .post(`/dummy/carts/${cart.id}`)
286
+ .send({
287
+ version: 1,
288
+ actions: [
289
+ {
290
+ action: 'addLineItem',
291
+ productId: product.id,
292
+ variantId: product.masterData.current.variants[0].id,
293
+ },
294
+ ],
295
+ })
296
+ const lineItem = updatedCart.body.lineItems[0]
297
+ assert(lineItem, 'lineItem not created')
298
+
299
+ expect(updatedCart.body.lineItems).toHaveLength(1)
300
+
301
+ const response = await supertest(ctMock.app)
302
+ .post(`/dummy/carts/${cart.id}`)
303
+ .send({
304
+ version: updatedCart.body.version,
305
+ actions: [{ action: 'removeLineItem', lineItemId: lineItem.id }],
306
+ })
307
+ expect(response.status).toBe(200)
308
+ expect(response.body.version).toBe(3)
309
+ expect(response.body.lineItems).toHaveLength(0)
310
+ })
311
+
312
+ test('removeLineItem decrease quantity', async () => {
313
+ const product = await supertest(ctMock.app)
314
+ .post(`/dummy/products`)
315
+ .send(productDraft)
316
+ .then((x) => x.body)
317
+
318
+ assert(cart, 'cart not created')
319
+ assert(product, 'product not created')
320
+
321
+ const updatedCart = await supertest(ctMock.app)
322
+ .post(`/dummy/carts/${cart.id}`)
323
+ .send({
324
+ version: 1,
325
+ actions: [
326
+ {
327
+ action: 'addLineItem',
328
+ productId: product.id,
329
+ variantId: product.masterData.current.variants[0].id,
330
+ quantity: 2,
331
+ },
332
+ ],
333
+ })
334
+ const lineItem = updatedCart.body.lineItems[0]
335
+ assert(lineItem, 'lineItem not created')
336
+
337
+ expect(updatedCart.body.lineItems).toHaveLength(1)
338
+ expect(updatedCart.body.lineItems[0].quantity).toBe(2)
339
+
340
+ const response = await supertest(ctMock.app)
341
+ .post(`/dummy/carts/${cart.id}`)
342
+ .send({
343
+ version: updatedCart.body.version,
344
+ actions: [
345
+ { action: 'removeLineItem', lineItemId: lineItem.id, quantity: 1 },
346
+ ],
347
+ })
348
+ expect(response.status).toBe(200)
349
+ expect(response.body.version).toBe(3)
350
+ expect(response.body.lineItems).toHaveLength(1)
351
+ expect(response.body.lineItems[0].quantity).toBe(1)
352
+ })
353
+
354
+ test('setBillingAddress', async () => {
355
+ assert(cart, 'cart not created')
356
+
357
+ const address: Address = {
358
+ streetName: 'Street name',
359
+ city: 'Utrecht',
360
+ country: 'NL',
361
+ }
362
+
363
+ const response = await supertest(ctMock.app)
364
+ .post(`/dummy/carts/${cart.id}`)
365
+ .send({
366
+ version: 1,
367
+ actions: [{ action: 'setBillingAddress', address }],
368
+ })
369
+ expect(response.status).toBe(200)
370
+ expect(response.body.version).toBe(2)
371
+ expect(response.body.billingAddress).toEqual(address)
372
+ })
373
+
374
+ test('setCountry', async () => {
375
+ assert(cart, 'cart not created')
376
+
377
+ const response = await supertest(ctMock.app)
378
+ .post(`/dummy/carts/${cart.id}`)
379
+ .send({
380
+ version: 1,
381
+ actions: [{ action: 'setCountry', country: 'NL' }],
382
+ })
383
+ expect(response.status).toBe(200)
384
+ expect(response.body.version).toBe(2)
385
+ expect(response.body.country).toBe('NL')
386
+ })
387
+
388
+ test('setCustomerEmail', async () => {
389
+ assert(cart, 'cart not created')
390
+
391
+ const response = await supertest(ctMock.app)
392
+ .post(`/dummy/carts/${cart.id}`)
393
+ .send({
394
+ version: 1,
395
+ actions: [{ action: 'setCustomerEmail', email: 'john@doe.com' }],
396
+ })
397
+ expect(response.status).toBe(200)
398
+ expect(response.body.version).toBe(2)
399
+ expect(response.body.customerEmail).toBe('john@doe.com')
400
+ })
401
+
402
+ test('setShippingAddress', async () => {
403
+ assert(cart, 'cart not created')
404
+
405
+ const address: Address = {
406
+ streetName: 'Street name',
407
+ city: 'Utrecht',
408
+ country: 'NL',
409
+ }
410
+
411
+ const response = await supertest(ctMock.app)
412
+ .post(`/dummy/carts/${cart.id}`)
413
+ .send({
414
+ version: 1,
415
+ actions: [{ action: 'setShippingAddress', address }],
416
+ })
417
+ expect(response.status).toBe(200)
418
+ expect(response.body.version).toBe(2)
419
+ expect(response.body.shippingAddress).toEqual(address)
420
+ })
421
421
  })