@labdigital/commercetools-mock 1.6.0 → 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.0",
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
  }
@@ -5,12 +5,16 @@ import type {
5
5
  QueryParam,
6
6
  } from '@commercetools/platform-sdk'
7
7
  import { ParsedQs } from 'qs'
8
+ import { CommercetoolsError } from '../exceptions.js'
8
9
  import { QueryParamsAsArray } from '../helpers.js'
10
+ import { parseQueryExpression } from '../lib/predicateParser.js'
9
11
  import { ProductProjectionSearch } from '../product-projection-search.js'
10
12
  import { type AbstractStorage } from '../storage/index.js'
11
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
12
- import { parseQueryExpression } from '../lib/predicateParser.js'
13
- import { CommercetoolsError } from '../exceptions.js'
13
+ import {
14
+ AbstractResourceRepository,
15
+ GetParams,
16
+ RepositoryContext,
17
+ } from './abstract.js'
14
18
 
15
19
  type ProductProjectionQueryParams = {
16
20
  staged?: boolean
@@ -45,6 +49,23 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
45
49
  throw new Error('No valid action')
46
50
  }
47
51
 
52
+ get(
53
+ context: RepositoryContext,
54
+ id: string,
55
+ params: GetParams = {}
56
+ ): ProductProjection | null {
57
+ const resource = this._storage.get(
58
+ context.projectKey,
59
+ 'product',
60
+ id,
61
+ params
62
+ )
63
+ if (resource) {
64
+ return this._searchService.transform(resource, false)
65
+ }
66
+ return null
67
+ }
68
+
48
69
  query(context: RepositoryContext, params: ProductProjectionQueryParams = {}) {
49
70
  let resources = this._storage
50
71
  .all(context.projectKey, 'product')
@@ -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
+ })