@labdigital/commercetools-mock 0.6.5 → 0.8.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 (118) hide show
  1. package/dist/index.d.ts +409 -3
  2. package/dist/index.global.js +49983 -0
  3. package/dist/index.global.js.map +1 -0
  4. package/dist/index.js +4835 -6
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +4803 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +36 -17
  9. package/src/ctMock.ts +5 -0
  10. package/src/helpers.ts +39 -0
  11. package/src/lib/projectionSearchFilter.test.ts +183 -0
  12. package/src/lib/projectionSearchFilter.ts +347 -0
  13. package/src/priceSelector.test.ts +96 -0
  14. package/src/priceSelector.ts +109 -0
  15. package/src/product-projection-search.ts +345 -0
  16. package/src/projectAPI.ts +19 -20
  17. package/src/repositories/category.ts +36 -0
  18. package/src/repositories/channel.ts +104 -0
  19. package/src/repositories/customer-group.ts +37 -0
  20. package/src/repositories/discount-code.ts +37 -0
  21. package/src/repositories/helpers.ts +46 -4
  22. package/src/repositories/product-discount.ts +181 -0
  23. package/src/repositories/product-projection.ts +30 -59
  24. package/src/repositories/product-type.ts +88 -6
  25. package/src/repositories/product.ts +49 -9
  26. package/src/repositories/shipping-method.ts +31 -0
  27. package/src/repositories/store.ts +43 -3
  28. package/src/repositories/type.ts +19 -0
  29. package/src/services/custom-object.test.ts +2 -2
  30. package/src/services/product-discount.ts +33 -0
  31. package/src/services/product-projection.test.ts +329 -107
  32. package/src/services/product.test.ts +12 -0
  33. package/src/storage.ts +116 -58
  34. package/src/types.ts +9 -2
  35. package/dist/commercetools-mock.cjs.development.js +0 -4382
  36. package/dist/commercetools-mock.cjs.development.js.map +0 -1
  37. package/dist/commercetools-mock.cjs.production.min.js +0 -2
  38. package/dist/commercetools-mock.cjs.production.min.js.map +0 -1
  39. package/dist/commercetools-mock.esm.js +0 -4374
  40. package/dist/commercetools-mock.esm.js.map +0 -1
  41. package/dist/constants.d.ts +0 -2
  42. package/dist/ctMock.d.ts +0 -32
  43. package/dist/exceptions.d.ts +0 -12
  44. package/dist/helpers.d.ts +0 -6
  45. package/dist/lib/expandParser.d.ts +0 -15
  46. package/dist/lib/filterParser.d.ts +0 -1
  47. package/dist/lib/haversine.d.ts +0 -8
  48. package/dist/lib/masking.d.ts +0 -1
  49. package/dist/lib/predicateParser.d.ts +0 -11
  50. package/dist/lib/proxy.d.ts +0 -1
  51. package/dist/oauth/errors.d.ts +0 -8
  52. package/dist/oauth/helpers.d.ts +0 -2
  53. package/dist/oauth/server.d.ts +0 -12
  54. package/dist/oauth/store.d.ts +0 -14
  55. package/dist/projectAPI.d.ts +0 -12
  56. package/dist/repositories/abstract.d.ts +0 -33
  57. package/dist/repositories/cart-discount.d.ts +0 -9
  58. package/dist/repositories/cart.d.ts +0 -21
  59. package/dist/repositories/category.d.ts +0 -18
  60. package/dist/repositories/channel.d.ts +0 -6
  61. package/dist/repositories/custom-object.d.ts +0 -8
  62. package/dist/repositories/customer-group.d.ts +0 -11
  63. package/dist/repositories/customer.d.ts +0 -11
  64. package/dist/repositories/discount-code.d.ts +0 -8
  65. package/dist/repositories/errors.d.ts +0 -2
  66. package/dist/repositories/extension.d.ts +0 -8
  67. package/dist/repositories/helpers.d.ts +0 -10
  68. package/dist/repositories/inventory-entry.d.ts +0 -14
  69. package/dist/repositories/my-order.d.ts +0 -6
  70. package/dist/repositories/order.d.ts +0 -26
  71. package/dist/repositories/payment.d.ts +0 -23
  72. package/dist/repositories/product-projection.d.ts +0 -10
  73. package/dist/repositories/product-type.d.ts +0 -10
  74. package/dist/repositories/product.d.ts +0 -11
  75. package/dist/repositories/project.d.ts +0 -8
  76. package/dist/repositories/shipping-method.d.ts +0 -10
  77. package/dist/repositories/shopping-list.d.ts +0 -6
  78. package/dist/repositories/state.d.ts +0 -8
  79. package/dist/repositories/store.d.ts +0 -10
  80. package/dist/repositories/subscription.d.ts +0 -6
  81. package/dist/repositories/tax-category.d.ts +0 -10
  82. package/dist/repositories/type.d.ts +0 -8
  83. package/dist/repositories/zone.d.ts +0 -8
  84. package/dist/server.d.ts +0 -1
  85. package/dist/services/abstract.d.ts +0 -20
  86. package/dist/services/cart-discount.d.ts +0 -9
  87. package/dist/services/cart.d.ts +0 -12
  88. package/dist/services/category.d.ts +0 -9
  89. package/dist/services/channel.d.ts +0 -9
  90. package/dist/services/custom-object.d.ts +0 -13
  91. package/dist/services/customer-group.d.ts +0 -9
  92. package/dist/services/customer.d.ts +0 -10
  93. package/dist/services/discount-code.d.ts +0 -9
  94. package/dist/services/extension.d.ts +0 -9
  95. package/dist/services/inventory-entry.d.ts +0 -9
  96. package/dist/services/my-cart.d.ts +0 -11
  97. package/dist/services/my-customer.d.ts +0 -13
  98. package/dist/services/my-order.d.ts +0 -10
  99. package/dist/services/my-payment.d.ts +0 -9
  100. package/dist/services/order.d.ts +0 -12
  101. package/dist/services/payment.d.ts +0 -9
  102. package/dist/services/product-projection.d.ts +0 -11
  103. package/dist/services/product-type.d.ts +0 -11
  104. package/dist/services/product.d.ts +0 -9
  105. package/dist/services/project.d.ts +0 -11
  106. package/dist/services/shipping-method.d.ts +0 -10
  107. package/dist/services/shopping-list.d.ts +0 -9
  108. package/dist/services/state.d.ts +0 -9
  109. package/dist/services/store.d.ts +0 -11
  110. package/dist/services/subscription.d.ts +0 -9
  111. package/dist/services/tax-category.d.ts +0 -11
  112. package/dist/services/type.d.ts +0 -9
  113. package/dist/services/zone.d.ts +0 -9
  114. package/dist/storage.d.ts +0 -56
  115. package/dist/types.d.ts +0 -89
  116. package/dist/validate.d.ts +0 -7482
  117. package/src/lib/filterParser.test.ts +0 -15
  118. package/src/lib/filterParser.ts +0 -17
@@ -1,167 +1,389 @@
1
- import { ProductDraft, ProductProjection } from '@commercetools/platform-sdk'
1
+ import {
2
+ ProductDraft,
3
+ ProductProjection,
4
+ ProductProjectionPagedSearchResponse,
5
+ ProductType,
6
+ ProductTypeDraft,
7
+ } from '@commercetools/platform-sdk'
2
8
  import supertest from 'supertest'
9
+ import * as timekeeper from 'timekeeper'
3
10
  import { CommercetoolsMock } from '../index'
4
- import * as qs from 'querystring'
11
+ import { Writable } from 'types'
5
12
 
6
13
  const ctMock = new CommercetoolsMock()
7
14
 
8
- describe('Product Projection', () => {
9
- afterEach(() => {
10
- ctMock.clear()
11
- })
15
+ let productType: ProductType
16
+ let productProjection: ProductProjection
17
+
18
+ beforeEach(async () => {
19
+ timekeeper.freeze(new Date('2022-07-22T13:31:49.840Z'))
20
+
21
+ // Create the product type
22
+ {
23
+ const draft: ProductTypeDraft = {
24
+ name: 'Default Product Type',
25
+ description: 'Product type for testing',
26
+ }
27
+ const response = await supertest(ctMock.app)
28
+ .post('/dummy/product-types')
29
+ .send(draft)
12
30
 
13
- test('Create product projection', async () => {
14
- const draft: ProductDraft = {
15
- key: '1337357',
31
+ expect(response.ok).toBe(true)
32
+ productType = response.body
33
+ }
34
+
35
+ // Create the product
36
+ {
37
+ const productDraft: Writable<ProductDraft> = {
38
+ publish: true,
39
+ key: 'my-product-key',
16
40
  masterVariant: {
17
- sku: '1337',
41
+ sku: 'my-sku',
42
+ prices: [
43
+ {
44
+ value: {
45
+ currencyCode: 'EUR',
46
+ centAmount: 1789,
47
+ },
48
+ },
49
+ ],
50
+ attributes: [
51
+ {
52
+ name: 'number',
53
+ value: 4 as any,
54
+ },
55
+ ],
18
56
  },
57
+ variants: [
58
+ {
59
+ sku: 'my-other-sku',
60
+ prices: [
61
+ {
62
+ value: {
63
+ currencyCode: 'EUR',
64
+ centAmount: 91789,
65
+ },
66
+ },
67
+ ],
68
+ attributes: [
69
+ {
70
+ name: 'number',
71
+ value: 50 as any,
72
+ },
73
+ ],
74
+ },
75
+ ],
19
76
  name: {
20
77
  'nl-NL': 'test product',
21
78
  },
22
79
  productType: {
23
80
  typeId: 'product-type',
24
- id: 'some-uuid',
81
+ id: productType.id,
25
82
  },
26
83
  slug: {
27
84
  'nl-NL': 'test-product',
28
85
  },
29
86
  }
87
+
30
88
  const response = await supertest(ctMock.app)
31
- .post('/dummy/product-projections')
32
- .send(draft)
89
+ .post('/dummy/products')
90
+ .send(productDraft)
91
+ expect(response.ok).toBe(true)
92
+ const product = response.body
33
93
 
34
- const projection: ProductProjection = response.body
35
- expect(projection).toEqual({
36
- createdAt: expect.anything(),
37
- id: expect.anything(),
38
- lastModifiedAt: expect.anything(),
39
- name: {
40
- 'nl-NL': 'test product',
41
- },
42
- slug: {
43
- 'nl-NL': 'test-product',
44
- },
45
- categories: [],
94
+ // Create the expected ProductProjection object
95
+ productProjection = {
96
+ id: product.id,
97
+ createdAt: '2022-07-22T13:31:49.840Z',
98
+ lastModifiedAt: '2022-07-22T13:31:49.840Z',
46
99
  version: 1,
47
100
  masterVariant: {
48
- id: 0,
49
- sku: '1337',
101
+ id: 1,
102
+ sku: 'my-sku',
103
+ prices: [
104
+ {
105
+ id: product.masterData.current.masterVariant.prices[0].id,
106
+ value: {
107
+ type: 'centPrecision',
108
+ currencyCode: 'EUR',
109
+ centAmount: 1789,
110
+ fractionDigits: 2,
111
+ },
112
+ },
113
+ ],
114
+ assets: [],
115
+ images: [],
116
+ attributes: productDraft.masterVariant?.attributes,
50
117
  },
118
+ variants: [
119
+ {
120
+ id: 2,
121
+ sku: 'my-other-sku',
122
+ prices: [
123
+ {
124
+ id: product.masterData.current.variants[0].prices[0].id,
125
+ value: {
126
+ type: 'centPrecision',
127
+ currencyCode: 'EUR',
128
+ centAmount: 91789,
129
+ fractionDigits: 2,
130
+ },
131
+ },
132
+ ],
133
+ assets: [],
134
+ images: [],
135
+ attributes: productDraft.variants![0].attributes,
136
+ },
137
+ ],
138
+ name: productDraft.name,
139
+ slug: productDraft.slug,
140
+ categories: [],
51
141
  productType: {
52
- id: 'some-uuid',
53
142
  typeId: 'product-type',
143
+ id: productType.id,
54
144
  },
55
- variants: [],
56
- })
57
- })
145
+ }
146
+ }
147
+ })
148
+
149
+ afterEach(async () => {
150
+ timekeeper.reset()
58
151
 
59
- test('Get product projection by 404 when not found by key with expand', async () => {
60
- const response = await supertest(ctMock.app).get(
61
- '/dummy/product-projections/key=DOESNOTEXIST?' +
62
- qs.stringify({
63
- expand: ['categories[*]'],
152
+ const response = await supertest(ctMock.app)
153
+ .delete(`/dummy/products/${productProjection.id}`)
154
+ .send()
155
+ expect(response.ok).toBe(true)
156
+ const product = response.body
157
+ })
158
+
159
+ // Test the general product projection implementation
160
+ describe('Product Projection Search - Generic', () => {
161
+ test('Pagination', async () => {
162
+ {
163
+ const response = await supertest(ctMock.app)
164
+ .get('/dummy/product-projections/search')
165
+ .query({
166
+ limit: 50,
64
167
  })
65
- )
168
+
169
+ const result: ProductProjectionPagedSearchResponse = response.body
170
+ expect(result).toEqual({
171
+ count: 1,
172
+ limit: 50,
173
+ offset: 0,
174
+ total: 1,
175
+ facets: {},
176
+ results: [productProjection],
177
+ })
178
+ }
179
+ {
180
+ const response = await supertest(ctMock.app)
181
+ .get('/dummy/product-projections/search')
182
+ .query({
183
+ limit: 50,
184
+ offset: 50,
185
+ })
186
+
187
+ const projection: ProductProjection = response.body
188
+ expect(projection).toEqual({
189
+ count: 1,
190
+ limit: 50,
191
+ offset: 50,
192
+ total: 0,
193
+ facets: {},
194
+ results: [],
195
+ })
196
+ }
197
+ })
198
+
199
+ test('Get 404 when not found by key with expand', async () => {
200
+ const response = await supertest(ctMock.app)
201
+ .get('/dummy/product-projections/key=DOESNOTEXIST')
202
+ .query({
203
+ expand: ['categories[*]'],
204
+ })
66
205
 
67
206
  expect(response.status).toBe(404)
68
207
  })
69
208
  })
70
209
 
71
- describe('Product Projection Search', () => {
72
- beforeAll(() => {
73
- ctMock.project('dummy').add('product-projection', {
74
- id: '',
75
- version: 1,
76
- productType: {
77
- id: 'fake',
78
- typeId: 'product-type',
79
- },
80
- name: { 'nl-NL': 'test-prod' },
81
- slug: {},
82
- variants: [],
83
- masterVariant: { id: 1, sku: '1337' },
84
- createdAt: '',
85
- lastModifiedAt: '',
86
- categories: [],
210
+ describe('Product Projection Search - Filters', () => {
211
+ test('variants.sku', async () => {
212
+ const response = await supertest(ctMock.app)
213
+ .get('/dummy/product-projections/search')
214
+ .query({
215
+ filter: ['variants.sku:"my-sku"'],
216
+ })
217
+
218
+ const result: ProductProjectionPagedSearchResponse = response.body
219
+ expect(result).toMatchObject({
220
+ count: 1,
221
+ results: [
222
+ {
223
+ masterVariant: { sku: 'my-sku' },
224
+ },
225
+ ],
87
226
  })
88
227
  })
89
228
 
90
- test('Search product projection', async () => {
91
- const response = await supertest(ctMock.app).get(
92
- '/dummy/product-projections/search?' +
93
- qs.stringify({
94
- filter: ['masterVariant.sku:"1337"'],
95
- })
96
- )
229
+ test('variants.attributes.range - match', async () => {
230
+ const response = await supertest(ctMock.app)
231
+ .get('/dummy/product-projections/search')
232
+ .query({
233
+ filter: ['variants.attributes.number:range(0 TO 10)'],
234
+ })
97
235
 
98
- const projection: ProductProjection = response.body
99
- expect(projection).toEqual({
236
+ const result: ProductProjectionPagedSearchResponse = response.body
237
+ expect(result).toMatchObject({
100
238
  count: 1,
101
- limit: 20,
102
- offset: 0,
103
- total: 1,
104
239
  results: [
105
240
  {
106
- categories: [],
107
- createdAt: '',
108
- id: '',
109
- lastModifiedAt: '',
110
- masterVariant: { id: 1, sku: '1337' },
111
- name: { 'nl-NL': 'test-prod' },
112
- productType: { id: 'fake', typeId: 'product-type' },
113
- slug: {},
114
- variants: [],
115
- version: 1,
241
+ masterVariant: { sku: 'my-sku' },
116
242
  },
117
243
  ],
118
244
  })
119
245
  })
120
246
 
121
- test('Search product projection page 2', async () => {
122
- const response = await supertest(ctMock.app).get(
123
- '/dummy/product-projections/search?' +
124
- qs.stringify({
125
- filter: ['masterVariant.sku:"1337"'],
126
- limit: 50,
127
- offset: 100,
128
- })
129
- )
247
+ test('variants.attributes.range - mismatch', async () => {
248
+ const response = await supertest(ctMock.app)
249
+ .get('/dummy/product-projections/search')
250
+ .query({
251
+ filter: ['variants.attributes.number:range(5 TO 10)'],
252
+ })
130
253
 
131
- const projection: ProductProjection = response.body
132
- expect(projection).toEqual({
133
- count: 1,
134
- limit: 50,
135
- offset: 100,
136
- total: 0,
254
+ const result: ProductProjectionPagedSearchResponse = response.body
255
+ expect(result).toMatchObject({
256
+ count: 0,
137
257
  results: [],
138
258
  })
139
259
  })
260
+ })
140
261
 
141
- test('Search product projection with query.filter', async () => {
142
- const response = await supertest(ctMock.app).get(
143
- '/dummy/product-projections/search?' +
144
- qs.stringify({
145
- 'query.filter': ['masterVariant.sku:"1337"'],
146
- })
147
- )
262
+ describe('Product Projection Search - Facets', () => {
263
+ test('termExpr - variants.attributes.number', async () => {
264
+ const response = await supertest(ctMock.app)
265
+ .get('/dummy/product-projections/search')
266
+ .query({
267
+ facet: ['variants.attributes.number'],
268
+ })
148
269
 
149
- const projection: ProductProjection = response.body
150
- expect(projection).toMatchObject({
270
+ const result: ProductProjectionPagedSearchResponse = response.body
271
+ expect(result).toMatchObject({
151
272
  count: 1,
152
- results: [{ masterVariant: { id: 1, sku: '1337' } }],
273
+ facets: {
274
+ 'variants.attributes.number': {
275
+ type: 'terms',
276
+ dataType: 'text',
277
+ missing: 0,
278
+ total: 2,
279
+ terms: [
280
+ {
281
+ term: '4.0',
282
+ count: 1,
283
+ },
284
+ {
285
+ term: '50.0',
286
+ count: 1,
287
+ },
288
+ ],
289
+ },
290
+ },
291
+ results: [
292
+ {
293
+ masterVariant: { sku: 'my-sku' },
294
+ },
295
+ ],
153
296
  })
154
297
  })
155
298
 
156
- test('Search product projection without filter', async () => {
157
- const response = await supertest(ctMock.app).get(
158
- '/dummy/product-projections/search'
159
- )
299
+ test('filterExpr - variants.attributes.number', async () => {
300
+ const response = await supertest(ctMock.app)
301
+ .get('/dummy/product-projections/search')
302
+ .query({
303
+ facet: ['variants.attributes.number:3,4'],
304
+ })
160
305
 
161
- const projection: ProductProjection = response.body
162
- expect(projection).toMatchObject({
306
+ const result: ProductProjectionPagedSearchResponse = response.body
307
+ expect(result).toMatchObject({
163
308
  count: 1,
164
- results: [{ masterVariant: { id: 1, sku: '1337' } }],
309
+ facets: {
310
+ 'variants.attributes.number': {
311
+ type: 'filter',
312
+ count: 1,
313
+ },
314
+ },
315
+ results: [
316
+ {
317
+ masterVariant: { sku: 'my-sku' },
318
+ },
319
+ ],
320
+ })
321
+ })
322
+
323
+ test('rangeExpr - variants.attributes.number', async () => {
324
+ const response = await supertest(ctMock.app)
325
+ .get('/dummy/product-projections/search')
326
+ .query({
327
+ facet: [
328
+ 'variants.attributes.number:range(* TO 5), (5 TO 25), (25 TO 100)',
329
+ ],
330
+ })
331
+
332
+ const result: ProductProjectionPagedSearchResponse = response.body
333
+ expect(result).toMatchObject({
334
+ count: 1,
335
+ facets: {
336
+ 'variants.attributes.number': {
337
+ type: 'range',
338
+ dataType: 'number',
339
+ ranges: [
340
+ {
341
+ type: 'double',
342
+ from: 0.0,
343
+ fromStr: '',
344
+ to: 5.0,
345
+ toStr: '5.0',
346
+ count: 1,
347
+ // totalCount: 1,
348
+ total: 4.0,
349
+ min: 4.0,
350
+ max: 4.0,
351
+ mean: 4.0,
352
+ },
353
+ {
354
+ type: 'double',
355
+ from: 5.0,
356
+ fromStr: '5.0',
357
+ to: 25.0,
358
+ toStr: '25.0',
359
+ count: 0,
360
+ // totalCount: 0,
361
+ total: 0.0,
362
+ min: 0.0,
363
+ max: 0.0,
364
+ mean: 0.0,
365
+ },
366
+ {
367
+ type: 'double',
368
+ from: 25.0,
369
+ fromStr: '25.0',
370
+ to: 100.0,
371
+ toStr: '100.0',
372
+ count: 1,
373
+ // totalCount: 1,
374
+ total: 50,
375
+ min: 50.0,
376
+ max: 50.0,
377
+ mean: 50.0,
378
+ },
379
+ ],
380
+ },
381
+ },
382
+ results: [
383
+ {
384
+ masterVariant: { sku: 'my-sku' },
385
+ },
386
+ ],
165
387
  })
166
388
  })
167
389
  })
@@ -18,6 +18,7 @@ describe('Product', () => {
18
18
  slug: {
19
19
  'nl-NL': 'test-product',
20
20
  },
21
+ masterVariant: {},
21
22
  }
22
23
 
23
24
  const response = await supertest(ctMock.app)
@@ -37,10 +38,21 @@ describe('Product', () => {
37
38
  'nl-NL': 'test-product',
38
39
  },
39
40
  categories: [],
41
+ masterVariant: {
42
+ assets: [],
43
+ attributes: [],
44
+ id: 1,
45
+ images: [],
46
+ },
47
+ variants: [],
40
48
  },
41
49
  hasStagedChanges: true,
42
50
  published: false,
43
51
  },
52
+ productType: {
53
+ typeId: 'product-type',
54
+ id: 'some-uuid',
55
+ },
44
56
  version: 1,
45
57
  })
46
58
  })