@labdigital/commercetools-mock 2.5.0 → 2.7.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/README.md +21 -8
- package/dist/index.cjs +59 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +59 -19
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ctMock.ts +29 -22
- package/src/repositories/index.ts +1 -0
- package/src/services/index.ts +5 -0
- package/src/services/my-customer.test.ts +56 -1
- package/src/services/my-customer.ts +20 -0
- package/src/services/my-shopping-list.ts +16 -0
package/package.json
CHANGED
package/src/ctMock.ts
CHANGED
|
@@ -78,7 +78,6 @@ export class CommercetoolsMock {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
clear() {
|
|
81
|
-
this._mswServer?.resetHandlers()
|
|
82
81
|
this._storage.clear()
|
|
83
82
|
}
|
|
84
83
|
|
|
@@ -165,25 +164,18 @@ export class CommercetoolsMock {
|
|
|
165
164
|
return app
|
|
166
165
|
}
|
|
167
166
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
console.warn("Server wasn't stopped properly, clearing")
|
|
175
|
-
_globalListeners.forEach((listener) => listener.close())
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const server = this.app
|
|
180
|
-
this._mswServer = setupServer(
|
|
167
|
+
// registerHandlers is an alternative way to work with commercetools-mock, it
|
|
168
|
+
// allows you to manage msw server yourself and register the handlers needed
|
|
169
|
+
// for commercetools-mock to work.
|
|
170
|
+
public registerHandlers(server: SetupServer) {
|
|
171
|
+
const app = this.app
|
|
172
|
+
server.use(
|
|
181
173
|
http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
|
|
182
174
|
const body = await request.text()
|
|
183
175
|
const url = new URL(request.url)
|
|
184
176
|
const headers = copyHeaders(request.headers)
|
|
185
177
|
|
|
186
|
-
const res = await inject(
|
|
178
|
+
const res = await inject(app)
|
|
187
179
|
.post(url.pathname + '?' + url.searchParams.toString())
|
|
188
180
|
.body(body)
|
|
189
181
|
.headers(headers)
|
|
@@ -198,7 +190,7 @@ export class CommercetoolsMock {
|
|
|
198
190
|
const url = new URL(request.url)
|
|
199
191
|
const headers = copyHeaders(request.headers)
|
|
200
192
|
|
|
201
|
-
const res = await inject(
|
|
193
|
+
const res = await inject(app)
|
|
202
194
|
.get(url.pathname + '?' + url.searchParams.toString())
|
|
203
195
|
.body(body)
|
|
204
196
|
.headers(headers)
|
|
@@ -229,7 +221,7 @@ export class CommercetoolsMock {
|
|
|
229
221
|
const url = new URL(request.url)
|
|
230
222
|
const headers = copyHeaders(request.headers)
|
|
231
223
|
|
|
232
|
-
const res = await inject(
|
|
224
|
+
const res = await inject(app)
|
|
233
225
|
.get(url.pathname + '?' + url.searchParams.toString())
|
|
234
226
|
.body(body)
|
|
235
227
|
.headers(headers)
|
|
@@ -244,7 +236,7 @@ export class CommercetoolsMock {
|
|
|
244
236
|
const url = new URL(request.url)
|
|
245
237
|
const headers = copyHeaders(request.headers)
|
|
246
238
|
|
|
247
|
-
const res = await inject(
|
|
239
|
+
const res = await inject(app)
|
|
248
240
|
.post(url.pathname + '?' + url.searchParams.toString())
|
|
249
241
|
.body(body)
|
|
250
242
|
.headers(headers)
|
|
@@ -259,7 +251,7 @@ export class CommercetoolsMock {
|
|
|
259
251
|
const url = new URL(request.url)
|
|
260
252
|
const headers = copyHeaders(request.headers)
|
|
261
253
|
|
|
262
|
-
const res = await inject(
|
|
254
|
+
const res = await inject(app)
|
|
263
255
|
.delete(url.pathname + '?' + url.searchParams.toString())
|
|
264
256
|
.body(body)
|
|
265
257
|
.headers(headers)
|
|
@@ -270,7 +262,22 @@ export class CommercetoolsMock {
|
|
|
270
262
|
})
|
|
271
263
|
})
|
|
272
264
|
)
|
|
273
|
-
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private startServer() {
|
|
268
|
+
// Check if there are any other servers running
|
|
269
|
+
if (_globalListeners.length > 0) {
|
|
270
|
+
if (this._mswServer !== undefined) {
|
|
271
|
+
throw new Error('Server already started')
|
|
272
|
+
} else {
|
|
273
|
+
console.warn("Server wasn't stopped properly, clearing")
|
|
274
|
+
_globalListeners.forEach((listener) => listener.close())
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const server = setupServer()
|
|
279
|
+
this.registerHandlers(server)
|
|
280
|
+
server.listen({
|
|
274
281
|
// We need to allow requests done by supertest
|
|
275
282
|
onUnhandledRequest: (request, print) => {
|
|
276
283
|
const url = new URL(request.url)
|
|
@@ -280,7 +287,7 @@ export class CommercetoolsMock {
|
|
|
280
287
|
print.error()
|
|
281
288
|
},
|
|
282
289
|
})
|
|
283
|
-
|
|
284
|
-
|
|
290
|
+
_globalListeners.push(server)
|
|
291
|
+
this._mswServer = server
|
|
285
292
|
}
|
|
286
293
|
}
|
|
@@ -59,6 +59,7 @@ export const createRepositories = (storage: AbstractStorage) => ({
|
|
|
59
59
|
'my-order': new MyOrderRepository(storage),
|
|
60
60
|
'my-customer': new CustomerRepository(storage),
|
|
61
61
|
'my-payment': new PaymentRepository(storage),
|
|
62
|
+
'my-shopping-list': new ShoppingListRepository(storage),
|
|
62
63
|
product: new ProductRepository(storage),
|
|
63
64
|
'product-type': new ProductTypeRepository(storage),
|
|
64
65
|
'product-discount': new ProductDiscountRepository(storage),
|
package/src/services/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { MyCartService } from './my-cart.js'
|
|
|
15
15
|
import { MyCustomerService } from './my-customer.js'
|
|
16
16
|
import { MyOrderService } from './my-order.js'
|
|
17
17
|
import { MyPaymentService } from './my-payment.js'
|
|
18
|
+
import { MyShoppingListService } from './my-shopping-list.js'
|
|
18
19
|
import { OrderService } from './order.js'
|
|
19
20
|
import { PaymentService } from './payment.js'
|
|
20
21
|
import { ProductDiscountService } from './product-discount.js'
|
|
@@ -61,6 +62,10 @@ export const createServices = (router: any, repos: any) => ({
|
|
|
61
62
|
'my-order': new MyOrderService(router, repos['my-order']),
|
|
62
63
|
'my-customer': new MyCustomerService(router, repos['my-customer']),
|
|
63
64
|
'my-payment': new MyPaymentService(router, repos['my-payment']),
|
|
65
|
+
'my-shopping-list': new MyShoppingListService(
|
|
66
|
+
router,
|
|
67
|
+
repos['my-shopping-list']
|
|
68
|
+
),
|
|
64
69
|
'shipping-method': new ShippingMethodService(
|
|
65
70
|
router,
|
|
66
71
|
repos['shipping-method']
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MyCustomerDraft } from '@commercetools/platform-sdk'
|
|
2
2
|
import supertest from 'supertest'
|
|
3
|
-
import { afterEach, describe, expect, test } from 'vitest'
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
|
|
4
4
|
import { CommercetoolsMock } from '../index.js'
|
|
5
5
|
|
|
6
6
|
const ctMock = new CommercetoolsMock()
|
|
@@ -51,3 +51,58 @@ describe('Me', () => {
|
|
|
51
51
|
expect(response.body).toEqual(createResponse.body.customer)
|
|
52
52
|
})
|
|
53
53
|
})
|
|
54
|
+
|
|
55
|
+
describe('/me', () => {
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
ctMock.clear()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
ctMock.project('dummy').add('customer', {
|
|
62
|
+
id: '123',
|
|
63
|
+
createdAt: '2021-03-18T14:00:00.000Z',
|
|
64
|
+
version: 2,
|
|
65
|
+
lastModifiedAt: '2021-03-18T14:00:00.000Z',
|
|
66
|
+
email: 'foo@example.org',
|
|
67
|
+
addresses: [],
|
|
68
|
+
isEmailVerified: true,
|
|
69
|
+
authenticationMode: 'password',
|
|
70
|
+
custom: { type: { typeId: 'type', id: '' }, fields: {} },
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('Get me', async () => {
|
|
75
|
+
const response = await supertest(ctMock.app).get('/dummy/me')
|
|
76
|
+
|
|
77
|
+
expect(response.status).toBe(200)
|
|
78
|
+
expect(response.body).toEqual({
|
|
79
|
+
id: '123',
|
|
80
|
+
createdAt: '2021-03-18T14:00:00.000Z',
|
|
81
|
+
version: 2,
|
|
82
|
+
lastModifiedAt: '2021-03-18T14:00:00.000Z',
|
|
83
|
+
email: 'foo@example.org',
|
|
84
|
+
addresses: [],
|
|
85
|
+
isEmailVerified: true,
|
|
86
|
+
authenticationMode: 'password',
|
|
87
|
+
custom: {
|
|
88
|
+
fields: {},
|
|
89
|
+
type: {
|
|
90
|
+
id: '',
|
|
91
|
+
typeId: 'type',
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('setCustomField', async () => {
|
|
98
|
+
const response = await supertest(ctMock.app)
|
|
99
|
+
.post(`/dummy/me`)
|
|
100
|
+
.send({
|
|
101
|
+
version: 2,
|
|
102
|
+
actions: [{ action: 'setCustomField', name: 'foobar', value: true }],
|
|
103
|
+
})
|
|
104
|
+
expect(response.status).toBe(200)
|
|
105
|
+
expect(response.body.version).toBe(3)
|
|
106
|
+
expect(response.body.custom.fields.foobar).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
@@ -3,6 +3,7 @@ import { CustomerRepository } from '../repositories/customer.js'
|
|
|
3
3
|
import { getRepositoryContext } from '../repositories/helpers.js'
|
|
4
4
|
import AbstractService from './abstract.js'
|
|
5
5
|
import { hashPassword } from '../lib/password.js'
|
|
6
|
+
import { Update } from '@commercetools/platform-sdk'
|
|
6
7
|
|
|
7
8
|
export class MyCustomerService extends AbstractService {
|
|
8
9
|
public repository: CustomerRepository
|
|
@@ -24,6 +25,7 @@ export class MyCustomerService extends AbstractService {
|
|
|
24
25
|
this.extraRoutes(router)
|
|
25
26
|
|
|
26
27
|
router.get('', this.getMe.bind(this))
|
|
28
|
+
router.post('', this.updateMe.bind(this))
|
|
27
29
|
|
|
28
30
|
router.post('/signup', this.signUp.bind(this))
|
|
29
31
|
|
|
@@ -40,6 +42,24 @@ export class MyCustomerService extends AbstractService {
|
|
|
40
42
|
return response.status(200).send(resource)
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
updateMe(request: Request, response: Response) {
|
|
46
|
+
const resource = this.repository.getMe(getRepositoryContext(request))
|
|
47
|
+
|
|
48
|
+
if (!resource) {
|
|
49
|
+
return response.status(404).send('Not found')
|
|
50
|
+
}
|
|
51
|
+
const updateRequest: Update = request.body
|
|
52
|
+
const updatedResource = this.repository.processUpdateActions(
|
|
53
|
+
getRepositoryContext(request),
|
|
54
|
+
resource,
|
|
55
|
+
updateRequest.version,
|
|
56
|
+
updateRequest.actions
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
const result = this._expandWithId(request, updatedResource.id)
|
|
60
|
+
return response.status(200).send(result)
|
|
61
|
+
}
|
|
62
|
+
|
|
43
63
|
signUp(request: Request, response: Response) {
|
|
44
64
|
const draft = request.body
|
|
45
65
|
const resource = this.repository.create(
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Router } from 'express'
|
|
2
|
+
import { ShoppingListRepository } from '../repositories/shopping-list.js'
|
|
3
|
+
import AbstractService from './abstract.js'
|
|
4
|
+
|
|
5
|
+
export class MyShoppingListService extends AbstractService {
|
|
6
|
+
public repository: ShoppingListRepository
|
|
7
|
+
|
|
8
|
+
constructor(parent: Router, repository: ShoppingListRepository) {
|
|
9
|
+
super(parent)
|
|
10
|
+
this.repository = repository
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getBasePath() {
|
|
14
|
+
return 'me/shopping-lists'
|
|
15
|
+
}
|
|
16
|
+
}
|