@labdigital/commercetools-mock 1.6.1 → 1.6.2

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "1.6.1",
4
+ "version": "1.6.2",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -2,7 +2,9 @@ import type {
2
2
  CartDiscount,
3
3
  CartDiscountChangeIsActiveAction,
4
4
  CartDiscountChangeSortOrderAction,
5
+ CartDiscountChangeTargetAction,
5
6
  CartDiscountDraft,
7
+ CartDiscountSetCustomFieldAction,
6
8
  CartDiscountSetDescriptionAction,
7
9
  CartDiscountSetKeyAction,
8
10
  CartDiscountSetValidFromAction,
@@ -14,6 +16,7 @@ import type {
14
16
  CartDiscountValueFixed,
15
17
  CartDiscountValueGiftLineItem,
16
18
  CartDiscountValueRelative,
19
+ InvalidOperationError,
17
20
  } from '@commercetools/platform-sdk'
18
21
  import { getBaseResourceProperties } from '../helpers.js'
19
22
  import type { Writable } from '../types.js'
@@ -21,7 +24,12 @@ import {
21
24
  AbstractResourceRepository,
22
25
  type RepositoryContext,
23
26
  } from './abstract.js'
24
- import { createTypedMoney, getStoreKeyReference } from './helpers.js'
27
+ import {
28
+ createCustomFields,
29
+ createTypedMoney,
30
+ getStoreKeyReference,
31
+ } from './helpers.js'
32
+ import { CommercetoolsError } from '../exceptions.js'
25
33
 
26
34
  export class CartDiscountRepository extends AbstractResourceRepository<'cart-discount'> {
27
35
  getTypeId() {
@@ -48,6 +56,11 @@ export class CartDiscountRepository extends AbstractResourceRepository<'cart-dis
48
56
  validFrom: draft.validFrom,
49
57
  validUntil: draft.validUntil,
50
58
  value: this.transformValueDraft(draft.value),
59
+ custom: createCustomFields(
60
+ draft.custom,
61
+ context.projectKey,
62
+ this._storage
63
+ ),
51
64
  }
52
65
  this.saveNew(context, resource)
53
66
  return resource
@@ -142,5 +155,40 @@ export class CartDiscountRepository extends AbstractResourceRepository<'cart-dis
142
155
  ) => {
143
156
  resource.isActive = isActive
144
157
  },
158
+ changeTarget: (
159
+ context: RepositoryContext,
160
+ resource: Writable<CartDiscount>,
161
+ { target }: CartDiscountChangeTargetAction
162
+ ) => {
163
+ resource.target = target
164
+ },
165
+
166
+ setCustomField: (
167
+ context: RepositoryContext,
168
+ resource: Writable<CartDiscount>,
169
+ { name, value }: CartDiscountSetCustomFieldAction
170
+ ) => {
171
+ if (!resource.custom) {
172
+ return
173
+ }
174
+ if (value === null) {
175
+ if (name in resource.custom.fields) {
176
+ delete resource.custom.fields[name]
177
+ } else {
178
+ throw new CommercetoolsError<InvalidOperationError>(
179
+ {
180
+ code: 'InvalidOperation',
181
+ message:
182
+ 'Cannot remove custom field ' +
183
+ name +
184
+ ' because it does not exist.',
185
+ },
186
+ 400
187
+ )
188
+ }
189
+ } else {
190
+ resource.custom.fields[name] = value
191
+ }
192
+ },
145
193
  }
146
194
  }
@@ -1,8 +1,12 @@
1
1
  import type {
2
+ Asset,
3
+ AssetDraft,
2
4
  Category,
5
+ CategoryAddAssetAction,
3
6
  CategoryChangeAssetNameAction,
4
7
  CategoryChangeSlugAction,
5
8
  CategoryDraft,
9
+ CategoryRemoveAssetAction,
6
10
  CategorySetAssetDescriptionAction,
7
11
  CategorySetAssetSourcesAction,
8
12
  CategorySetCustomFieldAction,
@@ -27,6 +31,15 @@ export class CategoryRepository extends AbstractResourceRepository<'category'> {
27
31
  return 'category' as const
28
32
  }
29
33
 
34
+ assetFromAssetDraft = (
35
+ draft: AssetDraft,
36
+ context: RepositoryContext
37
+ ): Asset => ({
38
+ ...draft,
39
+ id: uuidv4(),
40
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
41
+ })
42
+
30
43
  create(context: RepositoryContext, draft: CategoryDraft): Category {
31
44
  const resource: Category = {
32
45
  ...getBaseResourceProperties(),
@@ -177,5 +190,41 @@ export class CategoryRepository extends AbstractResourceRepository<'category'> {
177
190
  resource.custom.fields[name] = value
178
191
  }
179
192
  },
193
+ removeAsset: (
194
+ context: RepositoryContext,
195
+ resource: Writable<Category>,
196
+ { assetId, assetKey }: CategoryRemoveAssetAction
197
+ ) => {
198
+ if (!resource.assets) {
199
+ return
200
+ }
201
+
202
+ if (assetId) {
203
+ resource.assets = resource.assets.filter(function (obj) {
204
+ return obj.id !== assetId
205
+ })
206
+
207
+ return
208
+ }
209
+
210
+ if (assetKey) {
211
+ resource.assets = resource.assets.filter(function (obj) {
212
+ return obj.key !== assetKey
213
+ })
214
+
215
+ return
216
+ }
217
+ },
218
+ addAsset: (
219
+ context: RepositoryContext,
220
+ resource: Writable<Category>,
221
+ { asset }: CategoryAddAssetAction
222
+ ) => {
223
+ if (!resource.assets) {
224
+ resource.assets = [this.assetFromAssetDraft(asset, context)]
225
+ } else {
226
+ resource.assets.push(this.assetFromAssetDraft(asset, context))
227
+ }
228
+ },
180
229
  }
181
230
  }
@@ -3,6 +3,7 @@ import type {
3
3
  ChannelResourceIdentifier,
4
4
  Store,
5
5
  StoreDraft,
6
+ StoreSetCountriesAction,
6
7
  StoreSetCustomFieldAction,
7
8
  StoreSetCustomTypeAction,
8
9
  StoreSetDistributionChannelsAction,
@@ -124,5 +125,12 @@ export class StoreRepository extends AbstractResourceRepository<'store'> {
124
125
  resource.custom.fields[name] = value
125
126
  }
126
127
  },
128
+ setCountries: (
129
+ context: RepositoryContext,
130
+ resource: Writable<Store>,
131
+ { countries }: StoreSetCountriesAction
132
+ ) => {
133
+ resource.countries = countries ?? []
134
+ },
127
135
  }
128
136
  }
@@ -0,0 +1,362 @@
1
+ import assert from 'assert'
2
+ import supertest from 'supertest'
3
+ import { afterEach, beforeEach, describe, expect, test } from 'vitest'
4
+ import { CommercetoolsMock } from '../index.js'
5
+ import { CartDiscount, TypeDraft } from '@commercetools/platform-sdk'
6
+
7
+ const typeDraft: TypeDraft = {
8
+ key: 'my-type',
9
+ name: {
10
+ en: 'TestType',
11
+ },
12
+ description: {
13
+ en: 'Test Type',
14
+ },
15
+ resourceTypeIds: ['cart-discount'],
16
+ fieldDefinitions: [
17
+ {
18
+ name: 'discount_name',
19
+ label: {
20
+ en: 'Discount name',
21
+ },
22
+ required: false,
23
+ type: {
24
+ name: 'String',
25
+ },
26
+ inputHint: 'SingleLine',
27
+ },
28
+ {
29
+ name: 'fixedAmount',
30
+ label: {
31
+ en: 'Fixed Amount',
32
+ },
33
+ required: true,
34
+ type: {
35
+ name: 'Money',
36
+ },
37
+ inputHint: 'SingleLine',
38
+ },
39
+ ],
40
+ }
41
+
42
+ const getCartDiscountDraft = (typeId: string) => ({
43
+ version: 1,
44
+ key: 'my-relative-cart-discount',
45
+ name: { en: 'myRelativeCartDiscount' },
46
+ value: {
47
+ type: 'relative',
48
+ permyriad: 1000,
49
+ },
50
+ description: { en: 'My relative cart discount' },
51
+ target: { type: 'lineItems', predicate: '1=1' },
52
+ isActive: false,
53
+ custom: {
54
+ type: {
55
+ typeId: 'type',
56
+ id: typeId,
57
+ },
58
+ fields: {
59
+ discount_name: 'MyDiscount',
60
+ fixedAmount: {
61
+ type: 'centPrecision',
62
+ currencyCode: 'USD',
63
+ centAmount: 15000,
64
+ fractionDigits: 2,
65
+ },
66
+ },
67
+ },
68
+ validFrom: '2000-01-01T00:00:01.000Z',
69
+ validUntil: '2000-12-31T23:59:59.999Z',
70
+ sortOrder: '0.1',
71
+ })
72
+
73
+ describe('Cart Discounts Query', () => {
74
+ const ctMock = new CommercetoolsMock()
75
+
76
+ beforeEach(async () => {
77
+ let response
78
+ response = await supertest(ctMock.app).post('/dummy/types').send(typeDraft)
79
+ expect(response.status).toBe(201)
80
+ const typeId = response._body.id
81
+
82
+ response = await supertest(ctMock.app)
83
+ .post('/dummy/cart-discounts')
84
+ .send(getCartDiscountDraft(typeId))
85
+ expect(response.status).toBe(201)
86
+ })
87
+
88
+ test('no filter', async () => {
89
+ const response = await supertest(ctMock.app)
90
+ .get('/dummy/cart-discounts')
91
+ .send()
92
+
93
+ expect(response.status).toBe(200)
94
+ expect(response.body.count).toBe(1)
95
+
96
+ const myRelativeCartDiscount = response.body.results[0] as CartDiscount
97
+ expect(myRelativeCartDiscount.key).toBe('my-relative-cart-discount')
98
+ expect(myRelativeCartDiscount.description).toStrictEqual({
99
+ en: 'My relative cart discount',
100
+ })
101
+ expect(myRelativeCartDiscount.isActive).toBe(false)
102
+ expect(myRelativeCartDiscount.name).toStrictEqual({
103
+ en: 'myRelativeCartDiscount',
104
+ })
105
+ expect(myRelativeCartDiscount.stores).toStrictEqual([])
106
+ expect(myRelativeCartDiscount.references).toStrictEqual([])
107
+ expect(myRelativeCartDiscount.target).toStrictEqual({
108
+ type: 'lineItems',
109
+ predicate: '1=1',
110
+ })
111
+ expect(myRelativeCartDiscount.requiresDiscountCode).toBe(false)
112
+ expect(myRelativeCartDiscount.stackingMode).toBe('Stacking')
113
+ expect(myRelativeCartDiscount.value).toStrictEqual({
114
+ type: 'relative',
115
+ permyriad: 1000,
116
+ })
117
+
118
+ expect(myRelativeCartDiscount.custom?.type.id).not.toBeUndefined()
119
+ expect(myRelativeCartDiscount.custom?.type.typeId).toBe('type')
120
+ expect(myRelativeCartDiscount.custom?.fields).toStrictEqual({
121
+ discount_name: 'MyDiscount',
122
+ fixedAmount: {
123
+ centAmount: 15000,
124
+ currencyCode: 'USD',
125
+ fractionDigits: 2,
126
+ type: 'centPrecision',
127
+ },
128
+ })
129
+ })
130
+ })
131
+
132
+ describe('Cart Discounts Update Actions', () => {
133
+ const ctMock = new CommercetoolsMock()
134
+ let cartDiscount: CartDiscount | undefined
135
+
136
+ const createType = async () => {
137
+ const response = await supertest(ctMock.app)
138
+ .post('/dummy/types')
139
+ .send(typeDraft)
140
+ expect(response.status).toBe(201)
141
+ return response.body.id
142
+ }
143
+
144
+ const createCartDiscount = async (typeId) => {
145
+ const cartDiscountDraft = getCartDiscountDraft(typeId)
146
+ const response = await supertest(ctMock.app)
147
+ .post('/dummy/cart-discounts')
148
+ .send(cartDiscountDraft)
149
+ expect(response.status).toBe(201)
150
+ cartDiscount = response.body
151
+ }
152
+
153
+ beforeEach(async () => {
154
+ const typeId = await createType()
155
+ await createCartDiscount(typeId)
156
+ })
157
+
158
+ afterEach(() => {
159
+ ctMock.clear()
160
+ })
161
+
162
+ test('set key', async () => {
163
+ assert(cartDiscount, 'cart discount not created')
164
+
165
+ const response = await supertest(ctMock.app)
166
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
167
+ .send({
168
+ version: 1,
169
+ actions: [{ action: 'setKey', key: 'my-cart-discount' }],
170
+ })
171
+ expect(response.status).toBe(200)
172
+ expect(response.body.version).toBe(2)
173
+ expect(response.body.key).toBe('my-cart-discount')
174
+ })
175
+
176
+ test('set description', async () => {
177
+ assert(cartDiscount, 'cart discount not created')
178
+
179
+ const response = await supertest(ctMock.app)
180
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
181
+ .send({
182
+ version: 1,
183
+ actions: [
184
+ { action: 'setDescription', description: { en: 'Description' } },
185
+ ],
186
+ })
187
+ expect(response.status).toBe(200)
188
+ expect(response.body.version).toBe(2)
189
+ expect(response.body.description.en).toBe('Description')
190
+ })
191
+
192
+ test('set valid from', async () => {
193
+ assert(cartDiscount, 'cart discount not created')
194
+
195
+ const response = await supertest(ctMock.app)
196
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
197
+ .send({
198
+ version: 1,
199
+ actions: [
200
+ { action: 'setValidFrom', validFrom: '2020-01-01T00:00:01.000Z' },
201
+ ],
202
+ })
203
+ expect(response.status).toBe(200)
204
+ expect(response.body.version).toBe(2)
205
+ expect(response.body.validFrom).toBe('2020-01-01T00:00:01.000Z')
206
+ })
207
+
208
+ test('set valid until', async () => {
209
+ assert(cartDiscount, 'cart discount not created')
210
+
211
+ const response = await supertest(ctMock.app)
212
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
213
+ .send({
214
+ version: 1,
215
+ actions: [
216
+ { action: 'setValidUntil', validUntil: '2020-12-31T23:59:59.999Z' },
217
+ ],
218
+ })
219
+ expect(response.status).toBe(200)
220
+ expect(response.body.version).toBe(2)
221
+ expect(response.body.validUntil).toBe('2020-12-31T23:59:59.999Z')
222
+ })
223
+
224
+ test('set valid from and until', async () => {
225
+ assert(cartDiscount, 'cart discount not created')
226
+
227
+ const response = await supertest(ctMock.app)
228
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
229
+ .send({
230
+ version: 1,
231
+ actions: [
232
+ {
233
+ action: 'setValidFromAndUntil',
234
+ validFrom: '2020-01-01T00:00:01.000Z',
235
+ validUntil: '2020-12-31T23:59:59.999Z',
236
+ },
237
+ ],
238
+ })
239
+ expect(response.status).toBe(200)
240
+ expect(response.body.version).toBe(2)
241
+ expect(response.body.validFrom).toBe('2020-01-01T00:00:01.000Z')
242
+ expect(response.body.validUntil).toBe('2020-12-31T23:59:59.999Z')
243
+ })
244
+
245
+ test('change sort order', async () => {
246
+ assert(cartDiscount, 'cart discount not created')
247
+
248
+ const response = await supertest(ctMock.app)
249
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
250
+ .send({
251
+ version: 1,
252
+ actions: [{ action: 'changeSortOrder', sortOrder: '0.2' }],
253
+ })
254
+ expect(response.status).toBe(200)
255
+ expect(response.body.version).toBe(2)
256
+ expect(response.body.sortOrder).toBe('0.2')
257
+ })
258
+
259
+ test('change isActive', async () => {
260
+ assert(cartDiscount, 'cart discount not created')
261
+
262
+ const response = await supertest(ctMock.app)
263
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
264
+ .send({
265
+ version: 1,
266
+ actions: [{ action: 'changeIsActive', isActive: true }],
267
+ })
268
+ expect(response.status).toBe(200)
269
+ expect(response.body.version).toBe(2)
270
+ expect(response.body.isActive).toBe(true)
271
+ })
272
+
273
+ test('change target', async () => {
274
+ assert(cartDiscount, 'cart discount not created')
275
+
276
+ const response = await supertest(ctMock.app)
277
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
278
+ .send({
279
+ version: 1,
280
+ actions: [
281
+ {
282
+ action: 'changeTarget',
283
+ target: { type: 'shippingInfo', predicate: '2=2' },
284
+ },
285
+ ],
286
+ })
287
+ expect(response.status).toBe(200)
288
+ expect(response.body.version).toBe(2)
289
+ expect(response.body.target).toStrictEqual({
290
+ type: 'shippingInfo',
291
+ predicate: '2=2',
292
+ })
293
+ })
294
+
295
+ test('set custom field', async () => {
296
+ assert(cartDiscount, 'cart discount not created')
297
+
298
+ const response = await supertest(ctMock.app)
299
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
300
+ .send({
301
+ version: 1,
302
+ actions: [
303
+ {
304
+ action: 'setCustomField',
305
+ name: 'fixedAmount',
306
+ value: {
307
+ type: 'centPrecision',
308
+ currencyCode: 'EUR',
309
+ centAmount: 15,
310
+ fractionDigits: 2,
311
+ },
312
+ },
313
+ ],
314
+ })
315
+ expect(response.status).toBe(200)
316
+ expect(response.body.version).toBe(2)
317
+ expect(response.body.custom.fields.fixedAmount).toStrictEqual({
318
+ type: 'centPrecision',
319
+ currencyCode: 'EUR',
320
+ centAmount: 15,
321
+ fractionDigits: 2,
322
+ })
323
+ })
324
+
325
+ test('reset custom field', async () => {
326
+ assert(cartDiscount, 'cart discount not created')
327
+
328
+ const response = await supertest(ctMock.app)
329
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
330
+ .send({
331
+ version: 1,
332
+ actions: [
333
+ {
334
+ action: 'setCustomField',
335
+ name: 'fixedAmount',
336
+ value: null,
337
+ },
338
+ ],
339
+ })
340
+ expect(response.status).toBe(200)
341
+ expect(response.body.version).toBe(2)
342
+ expect(response.body.custom.fields.fixedAmount).toBeUndefined()
343
+ })
344
+
345
+ test('reset non-existing custom field', async () => {
346
+ assert(cartDiscount, 'cart discount not created')
347
+
348
+ const response = await supertest(ctMock.app)
349
+ .post(`/dummy/cart-discounts/${cartDiscount.id}`)
350
+ .send({
351
+ version: 1,
352
+ actions: [
353
+ {
354
+ action: 'setCustomField',
355
+ name: 'nonExistingField',
356
+ value: null,
357
+ },
358
+ ],
359
+ })
360
+ expect(response.status).toBe(400)
361
+ })
362
+ })
@@ -1,10 +1,16 @@
1
- import type { Category } from '@commercetools/platform-sdk'
1
+ import type {
2
+ Category,
3
+ CategoryAddAssetAction,
4
+ CategoryRemoveAssetAction,
5
+ } from '@commercetools/platform-sdk'
2
6
  import supertest from 'supertest'
3
- import { beforeEach, describe, expect, test } from 'vitest'
7
+ import { beforeEach, afterEach, describe, expect, test } from 'vitest'
4
8
  import { CommercetoolsMock } from '../index.js'
9
+ import assert from 'assert'
5
10
 
6
11
  describe('Categories Query', () => {
7
12
  const ctMock = new CommercetoolsMock()
13
+ let category: Category | undefined
8
14
 
9
15
  beforeEach(async () => {
10
16
  const response = await supertest(ctMock.app)
@@ -19,6 +25,12 @@ describe('Categories Query', () => {
19
25
  orderHint: '0.1',
20
26
  })
21
27
  expect(response.status).toBe(201)
28
+
29
+ category = response.body as Category
30
+ })
31
+
32
+ afterEach(() => {
33
+ ctMock.clear()
22
34
  })
23
35
 
24
36
  test('no filter', async () => {
@@ -35,3 +47,106 @@ describe('Categories Query', () => {
35
47
  expect(category.name.en).toBe('Top hat')
36
48
  })
37
49
  })
50
+
51
+ describe('Categories add asset', () => {
52
+ const ctMock = new CommercetoolsMock()
53
+ let category: Category | undefined
54
+
55
+ beforeEach(async () => {
56
+ const response = await supertest(ctMock.app)
57
+ .post('/dummy/categories')
58
+ .send({
59
+ name: {
60
+ en: 'Top hat',
61
+ },
62
+ slug: {
63
+ en: 'top-hat',
64
+ },
65
+ orderHint: '0.1',
66
+ assets: [
67
+ {
68
+ key: 'some-key',
69
+ },
70
+ ],
71
+ })
72
+ expect(response.status).toBe(201)
73
+
74
+ category = response.body as Category
75
+ })
76
+
77
+ test('add second asset', async () => {
78
+ assert(category, 'category not created')
79
+
80
+ const response = await supertest(ctMock.app)
81
+ .post(`/dummy/categories/${category.id}`)
82
+ .send({
83
+ version: 1,
84
+ actions: [
85
+ {
86
+ action: 'addAsset',
87
+ asset: {
88
+ key: 'some-other-key',
89
+ },
90
+ } as CategoryAddAssetAction,
91
+ ],
92
+ })
93
+
94
+ expect(response.status).toBe(200)
95
+ expect(response.body.assets).toHaveLength(2)
96
+ expect(response.body.assets[0].key).toEqual('some-key')
97
+ expect(response.body.assets[1].key).toEqual('some-other-key')
98
+ })
99
+ })
100
+
101
+ describe('Categories remove asset', () => {
102
+ const ctMock = new CommercetoolsMock()
103
+ let category: Category | undefined
104
+
105
+ beforeEach(async () => {
106
+ const response = await supertest(ctMock.app)
107
+ .post('/dummy/categories')
108
+ .send({
109
+ name: {
110
+ en: 'Top hat',
111
+ },
112
+ slug: {
113
+ en: 'top-hat',
114
+ },
115
+ orderHint: '0.1',
116
+ assets: [
117
+ {
118
+ key: 'some-key',
119
+ },
120
+ {
121
+ key: 'some-other-key',
122
+ },
123
+ ],
124
+ })
125
+ expect(response.status).toBe(201)
126
+
127
+ category = response.body as Category
128
+ })
129
+
130
+ test('remove assets by id and key', async () => {
131
+ assert(category, 'category not created')
132
+
133
+ const response = await supertest(ctMock.app)
134
+ .post(`/dummy/categories/${category.id}`)
135
+ .send({
136
+ version: 1,
137
+ actions: [
138
+ {
139
+ action: 'removeAsset',
140
+ assetKey: category.assets[1].key,
141
+ } as CategoryRemoveAssetAction,
142
+ {
143
+ action: 'removeAsset',
144
+ assetId: category.assets[0].id,
145
+ } as CategoryRemoveAssetAction,
146
+ ],
147
+ })
148
+
149
+ expect(response.status).toBe(200)
150
+ expect(response.body.assets).toHaveLength(0)
151
+ })
152
+ })