@labdigital/commercetools-mock 0.14.0 → 1.0.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "0.14.0",
4
+ "version": "1.0.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/commercetools--mock.esm.js",
@@ -17,10 +17,10 @@
17
17
  }
18
18
  },
19
19
  "engines": {
20
- "node": ">=14",
21
- "pnpm": ">=7.13.2"
20
+ "node": ">=16",
21
+ "pnpm": ">=8.1.1"
22
22
  },
23
- "packageManager": "pnpm@7.13.2",
23
+ "packageManager": "pnpm@8.1.1",
24
24
  "publishConfig": {
25
25
  "access": "public"
26
26
  },
@@ -51,6 +51,7 @@
51
51
  "@changesets/cli": "^2.26.0",
52
52
  "@changesets/changelog-github": "^0.4.8",
53
53
  "@commercetools/platform-sdk": "4.0.0",
54
+ "@labdigital/eslint-config-node": "0.0.5",
54
55
  "@types/basic-auth": "^1.1.3",
55
56
  "@types/body-parser": "^1.19.2",
56
57
  "@types/deep-equal": "^1.0.1",
@@ -71,7 +72,6 @@
71
72
  "got": "^11.8.3",
72
73
  "husky": "^7.0.4",
73
74
  "jest": "^28.1.3",
74
- "nodemon": "^2.0.15",
75
75
  "prettier": "^2.7.1",
76
76
  "timekeeper": "^2.2.0",
77
77
  "ts-node": "^10.4.0",
@@ -84,9 +84,10 @@
84
84
  "@commercetools/platform-sdk": "^2.4.1"
85
85
  },
86
86
  "scripts": {
87
- "start": "nodemon --watch src --exec 'node -r esbuild-register' src/server.ts",
87
+ "start": "tsup src --watch src/server.ts",
88
88
  "build": "tsup",
89
89
  "build:server": "esbuild src/server.ts --bundle --outfile=dist/server.js --platform=node",
90
+ "publish:ci": "pnpm build && pnpm changeset publish",
90
91
  "check": "eslint src && tsc",
91
92
  "test": "jest test --coverage",
92
93
  "lint": "eslint src"
@@ -1,36 +1,74 @@
1
1
  import {
2
- Review,
2
+ ChannelReference,
3
+ ChannelResourceIdentifier,
4
+ DiscountedPriceDraft,
3
5
  StandalonePrice,
4
- StandalonePriceUpdateAction,
6
+ StandalonePriceChangeActiveAction,
7
+ StandalonePriceChangeValueAction,
8
+ StandalonePriceDraft,
9
+ StandalonePriceSetDiscountedPriceAction,
5
10
  } from '@commercetools/platform-sdk'
6
11
  import { getBaseResourceProperties } from '../helpers'
7
12
  import { Writable } from '../types'
8
13
  import { AbstractResourceRepository, RepositoryContext } from './abstract'
14
+ import { createTypedMoney } from './helpers'
9
15
 
10
16
  export class StandAlonePriceRepository extends AbstractResourceRepository<'standalone-price'> {
11
17
  getTypeId() {
12
18
  return 'standalone-price' as const
13
19
  }
14
20
 
15
- create(context: RepositoryContext, draft: StandalonePrice): StandalonePrice {
21
+ create(context: RepositoryContext, draft: StandalonePriceDraft): StandalonePrice {
16
22
  const resource: StandalonePrice = {
17
23
  ...getBaseResourceProperties(),
18
- active: draft.active,
24
+ active: draft.active? draft.active : false,
19
25
  sku: draft.sku,
20
- value: draft.value,
26
+ value: createTypedMoney(draft.value),
27
+ country: draft.country,
28
+ discounted: draft.discounted ? this.transformDiscountDraft(draft.discounted) : undefined,
29
+ channel: draft.channel?.id ? this.transformChannelReferenceDraft(draft.channel) : undefined,
30
+ validFrom: draft.validFrom,
31
+ validUntil: draft.validUntil,
21
32
  }
22
33
  this.saveNew(context, resource)
23
34
  return resource
24
35
  }
25
36
 
26
- actions: Partial<
27
- Record<
28
- StandalonePriceUpdateAction['action'],
29
- (
30
- context: RepositoryContext,
31
- resource: Writable<Review>,
32
- action: any
33
- ) => void
34
- >
35
- > = {}
37
+ transformChannelReferenceDraft(channel: ChannelResourceIdentifier) : ChannelReference {
38
+ return {
39
+ typeId: channel.typeId,
40
+ id: channel.id as string,
41
+ }
42
+ }
43
+
44
+ transformDiscountDraft(discounted: DiscountedPriceDraft) {
45
+ return {
46
+ value: createTypedMoney(discounted.value),
47
+ discount: discounted.discount,
48
+ }
49
+ }
50
+
51
+ actions = {
52
+ setActive: (
53
+ context: RepositoryContext,
54
+ resource: Writable<StandalonePrice>,
55
+ action: StandalonePriceChangeActiveAction
56
+ ) => {
57
+ resource.active = action.active
58
+ },
59
+ changeValue: (
60
+ context: RepositoryContext,
61
+ resource: Writable<StandalonePrice>,
62
+ action: StandalonePriceChangeValueAction
63
+ ) => {
64
+ resource.value = createTypedMoney(action.value)
65
+ },
66
+ setDiscountedPrice: (
67
+ context: RepositoryContext,
68
+ resource: Writable<StandalonePrice>,
69
+ action: StandalonePriceSetDiscountedPriceAction
70
+ ) => {
71
+ resource.discounted = action.discounted ? this.transformDiscountDraft(action.discounted) : undefined
72
+ }
73
+ }
36
74
  }
@@ -4,34 +4,272 @@ import { CommercetoolsMock } from '../index'
4
4
 
5
5
  const ctMock = new CommercetoolsMock()
6
6
 
7
- describe('StandalonePrice', () => {
8
- test('Create standalone price', async () => {
7
+ describe('Standalone price Query', () => {
8
+ beforeAll(async () => {
9
9
  const draft: StandalonePriceDraft = {
10
- sku: 'test-sku',
11
10
  value: {
11
+ centAmount: 100,
12
12
  currencyCode: 'EUR',
13
- centAmount: 1000,
14
13
  },
14
+ country: 'DE',
15
+ sku: 'foo',
15
16
  active: true,
17
+ channel: {
18
+ typeId: 'channel',
19
+ id: 'bar',
20
+ },
21
+ discounted: {
22
+ value: {
23
+ centAmount: 80,
24
+ currencyCode: 'EUR',
25
+ },
26
+ discount: {
27
+ typeId: 'product-discount',
28
+ id: 'baz',
29
+ },
30
+ },
16
31
  }
32
+ const createResponse = await supertest(ctMock.app)
33
+ .post('/dummy/standalone-prices')
34
+ .send(draft)
35
+ expect(createResponse.status).toEqual(201)
36
+ })
17
37
 
18
- const response = await supertest(ctMock.app).post('/dummy/standalone-prices').send(draft)
19
- expect(response.status).toBe(201)
38
+ afterAll(async () => {
39
+ ctMock.clear()
20
40
  })
21
41
 
22
42
  test('Get standalone price', async () => {
43
+ const response = await supertest(ctMock.app).get('/dummy/standalone-prices?sku=foo')
44
+
45
+ expect(response.status).toBe(200)
46
+
47
+ expect(response.body.results).toEqual([
48
+ {
49
+ active: true,
50
+ channel: {
51
+ id: 'bar',
52
+ typeId: 'channel',
53
+ },
54
+ country: 'DE',
55
+ createdAt: expect.anything(),
56
+ discounted: {
57
+ discount: {
58
+ id: 'baz',
59
+ typeId: 'product-discount',
60
+ },
61
+ value: {
62
+ centAmount: 80,
63
+ currencyCode: 'EUR',
64
+ fractionDigits: 2,
65
+ type: 'centPrecision',
66
+ },
67
+ },
68
+ id: expect.anything(),
69
+ lastModifiedAt: expect.anything(),
70
+ sku: 'foo',
71
+ value: {
72
+ centAmount: 100,
73
+ currencyCode: 'EUR',
74
+ fractionDigits: 2,
75
+ type: 'centPrecision',
76
+ },
77
+ version: 1,
78
+ },
79
+ ])
80
+ })
81
+ })
82
+
83
+ describe('Standalone price Actions', () => {
84
+ let id: string | undefined
85
+ beforeEach(async () => {
23
86
  const draft: StandalonePriceDraft = {
24
- sku: 'test-sku',
25
87
  value: {
88
+ centAmount: 100,
26
89
  currencyCode: 'EUR',
27
- centAmount: 1000,
28
90
  },
91
+ country: 'DE',
92
+ sku: 'foo',
29
93
  active: true,
94
+ channel: {
95
+ typeId: 'channel',
96
+ id: 'bar',
97
+ },
30
98
  }
99
+ const createResponse = await supertest(ctMock.app)
100
+ .post('/dummy/standalone-prices')
101
+ .send(draft)
102
+ expect(createResponse.status).toEqual(201)
103
+ id = createResponse.body.id
104
+ })
105
+
106
+ afterEach(async () => {
107
+ ctMock.clear()
108
+ })
109
+
110
+ test('changeValue', async () => {
111
+ const response = await supertest(ctMock.app)
112
+ .post('/dummy/standalone-prices/' + id)
113
+ .send({
114
+ version: 1,
115
+ actions: [
116
+ {
117
+ action: 'changeValue',
118
+ value: {
119
+ centAmount: 200,
120
+ currencyCode: 'EUR',
121
+ },
122
+ },
123
+ ],
124
+ })
125
+
126
+ expect(response.status).toBe(200)
127
+
128
+ expect(response.body).toEqual({
129
+ active: true,
130
+ channel: {
131
+ id: 'bar',
132
+ typeId: 'channel',
133
+ },
134
+ country: 'DE',
135
+ createdAt: expect.anything(),
136
+ id: id,
137
+ lastModifiedAt: expect.anything(),
138
+ sku: 'foo',
139
+ value: {
140
+ centAmount: 200,
141
+ currencyCode: 'EUR',
142
+ fractionDigits: 2,
143
+ type: 'centPrecision',
144
+ },
145
+ version: 2,
146
+ })
147
+ })
31
148
 
32
- const createResponse = await supertest(ctMock.app).post('/dummy/standalone-prices').send(draft)
149
+ test('setActive', async () => {
150
+ const response = await supertest(ctMock.app)
151
+ .post('/dummy/standalone-prices/' + id)
152
+ .send({
153
+ version: 1,
154
+ actions: [
155
+ {
156
+ action: 'setActive',
157
+ active: false,
158
+ },
159
+ ],
160
+ })
33
161
 
34
- const response = await supertest(ctMock.app).get(`/dummy/standalone-prices/${createResponse.body.id}`)
35
162
  expect(response.status).toBe(200)
163
+
164
+ expect(response.body).toEqual({
165
+ active: false,
166
+ channel: {
167
+ id: 'bar',
168
+ typeId: 'channel',
169
+ },
170
+ country: 'DE',
171
+ createdAt: expect.anything(),
172
+ id: id,
173
+ lastModifiedAt: expect.anything(),
174
+ sku: 'foo',
175
+ value: {
176
+ centAmount: 100,
177
+ currencyCode: 'EUR',
178
+ fractionDigits: 2,
179
+ type: 'centPrecision',
180
+ },
181
+ version: 2,
182
+ })
183
+ })
184
+
185
+ test('setDiscounted', async () => {
186
+ const response = await supertest(ctMock.app)
187
+ .post('/dummy/standalone-prices/' + id)
188
+ .send({
189
+ version: 1,
190
+ actions: [
191
+ {
192
+ action: 'setDiscountedPrice',
193
+ discounted: {
194
+ value: {
195
+ centAmount: 80,
196
+ currencyCode: 'EUR',
197
+ },
198
+ discount: {
199
+ typeId: 'product-discount',
200
+ id: 'baz',
201
+ },
202
+ },
203
+ },
204
+ ],
205
+ })
206
+
207
+ expect(response.status).toBe(200)
208
+
209
+ expect(response.body).toEqual({
210
+ active: true,
211
+ channel: {
212
+ id: 'bar',
213
+ typeId: 'channel',
214
+ },
215
+ country: 'DE',
216
+ createdAt: expect.anything(),
217
+ discounted: {
218
+ discount: {
219
+ id: 'baz',
220
+ typeId: 'product-discount',
221
+ },
222
+ value: {
223
+ centAmount: 80,
224
+ currencyCode: 'EUR',
225
+ fractionDigits: 2,
226
+ type: 'centPrecision',
227
+ },
228
+ },
229
+ id: id,
230
+ lastModifiedAt: expect.anything(),
231
+ sku: 'foo',
232
+ value: {
233
+ centAmount: 100,
234
+ currencyCode: 'EUR',
235
+ fractionDigits: 2,
236
+ type: 'centPrecision',
237
+ },
238
+ version: 2,
239
+ })
240
+
241
+ const response2 = await supertest(ctMock.app)
242
+ .post('/dummy/standalone-prices/' + id)
243
+ .send({
244
+ version: 2,
245
+ actions: [
246
+ {
247
+ action: 'setDiscountedPrice',
248
+ discounted: null,
249
+ },
250
+ ],
251
+ })
252
+
253
+ expect(response2.status).toBe(200)
254
+
255
+ expect(response2.body).toEqual({
256
+ active: true,
257
+ channel: {
258
+ id: 'bar',
259
+ typeId: 'channel',
260
+ },
261
+ country: 'DE',
262
+ createdAt: expect.anything(),
263
+ id: id,
264
+ lastModifiedAt: expect.anything(),
265
+ sku: 'foo',
266
+ value: {
267
+ centAmount: 100,
268
+ currencyCode: 'EUR',
269
+ fractionDigits: 2,
270
+ type: 'centPrecision',
271
+ },
272
+ version: 3,
273
+ })
36
274
  })
37
275
  })