@labdigital/commercetools-mock 2.4.0 → 2.6.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 +43 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ctMock.ts +59 -20
- package/src/services/custom-object.test.ts +17 -1
- package/src/services/customer.test.ts +20 -0
package/package.json
CHANGED
package/src/ctMock.ts
CHANGED
|
@@ -165,25 +165,18 @@ export class CommercetoolsMock {
|
|
|
165
165
|
return app
|
|
166
166
|
}
|
|
167
167
|
|
|
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(
|
|
168
|
+
// registerHandlers is an alternative way to work with commercetools-mock, it
|
|
169
|
+
// allows you to manage msw server yourself and register the handlers needed
|
|
170
|
+
// for commercetools-mock to work.
|
|
171
|
+
public registerHandlers(server: SetupServer) {
|
|
172
|
+
const app = this.app
|
|
173
|
+
server.use(
|
|
181
174
|
http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
|
|
182
175
|
const body = await request.text()
|
|
183
176
|
const url = new URL(request.url)
|
|
184
177
|
const headers = copyHeaders(request.headers)
|
|
185
178
|
|
|
186
|
-
const res = await inject(
|
|
179
|
+
const res = await inject(app)
|
|
187
180
|
.post(url.pathname + '?' + url.searchParams.toString())
|
|
188
181
|
.body(body)
|
|
189
182
|
.headers(headers)
|
|
@@ -193,12 +186,43 @@ export class CommercetoolsMock {
|
|
|
193
186
|
headers: mapHeaderType(res.headers),
|
|
194
187
|
})
|
|
195
188
|
}),
|
|
189
|
+
http.head(`${this.options.apiHost}/*`, async ({ request }) => {
|
|
190
|
+
const body = await request.text()
|
|
191
|
+
const url = new URL(request.url)
|
|
192
|
+
const headers = copyHeaders(request.headers)
|
|
193
|
+
|
|
194
|
+
const res = await inject(app)
|
|
195
|
+
.get(url.pathname + '?' + url.searchParams.toString())
|
|
196
|
+
.body(body)
|
|
197
|
+
.headers(headers)
|
|
198
|
+
.end()
|
|
199
|
+
|
|
200
|
+
if (res.statusCode === 200) {
|
|
201
|
+
const parsedBody = JSON.parse(res.body)
|
|
202
|
+
// Check if we have a count property (e.g. for query-lookups)
|
|
203
|
+
// or if we have a result object (e.g. for single lookups)
|
|
204
|
+
const resultCount =
|
|
205
|
+
'count' in parsedBody
|
|
206
|
+
? parsedBody.count
|
|
207
|
+
: Object.keys(parsedBody).length
|
|
208
|
+
|
|
209
|
+
return new HttpResponse(null, {
|
|
210
|
+
status: resultCount > 0 ? 200 : 404,
|
|
211
|
+
headers: mapHeaderType(res.headers),
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return new HttpResponse(null, {
|
|
216
|
+
status: res.statusCode,
|
|
217
|
+
headers: mapHeaderType(res.headers),
|
|
218
|
+
})
|
|
219
|
+
}),
|
|
196
220
|
http.get(`${this.options.apiHost}/*`, async ({ request }) => {
|
|
197
221
|
const body = await request.text()
|
|
198
222
|
const url = new URL(request.url)
|
|
199
223
|
const headers = copyHeaders(request.headers)
|
|
200
224
|
|
|
201
|
-
const res = await inject(
|
|
225
|
+
const res = await inject(app)
|
|
202
226
|
.get(url.pathname + '?' + url.searchParams.toString())
|
|
203
227
|
.body(body)
|
|
204
228
|
.headers(headers)
|
|
@@ -213,7 +237,7 @@ export class CommercetoolsMock {
|
|
|
213
237
|
const url = new URL(request.url)
|
|
214
238
|
const headers = copyHeaders(request.headers)
|
|
215
239
|
|
|
216
|
-
const res = await inject(
|
|
240
|
+
const res = await inject(app)
|
|
217
241
|
.post(url.pathname + '?' + url.searchParams.toString())
|
|
218
242
|
.body(body)
|
|
219
243
|
.headers(headers)
|
|
@@ -228,7 +252,7 @@ export class CommercetoolsMock {
|
|
|
228
252
|
const url = new URL(request.url)
|
|
229
253
|
const headers = copyHeaders(request.headers)
|
|
230
254
|
|
|
231
|
-
const res = await inject(
|
|
255
|
+
const res = await inject(app)
|
|
232
256
|
.delete(url.pathname + '?' + url.searchParams.toString())
|
|
233
257
|
.body(body)
|
|
234
258
|
.headers(headers)
|
|
@@ -239,7 +263,22 @@ export class CommercetoolsMock {
|
|
|
239
263
|
})
|
|
240
264
|
})
|
|
241
265
|
)
|
|
242
|
-
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private startServer() {
|
|
269
|
+
// Check if there are any other servers running
|
|
270
|
+
if (_globalListeners.length > 0) {
|
|
271
|
+
if (this._mswServer !== undefined) {
|
|
272
|
+
throw new Error('Server already started')
|
|
273
|
+
} else {
|
|
274
|
+
console.warn("Server wasn't stopped properly, clearing")
|
|
275
|
+
_globalListeners.forEach((listener) => listener.close())
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const server = setupServer()
|
|
280
|
+
this.registerHandlers(server)
|
|
281
|
+
server.listen({
|
|
243
282
|
// We need to allow requests done by supertest
|
|
244
283
|
onUnhandledRequest: (request, print) => {
|
|
245
284
|
const url = new URL(request.url)
|
|
@@ -249,7 +288,7 @@ export class CommercetoolsMock {
|
|
|
249
288
|
print.error()
|
|
250
289
|
},
|
|
251
290
|
})
|
|
252
|
-
|
|
253
|
-
|
|
291
|
+
_globalListeners.push(server)
|
|
292
|
+
this._mswServer = server
|
|
254
293
|
}
|
|
255
294
|
}
|
|
@@ -47,7 +47,23 @@ describe('CustomObject retrieve', () => {
|
|
|
47
47
|
ctMock.clear()
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
-
test('
|
|
50
|
+
test('exists', async () => {
|
|
51
|
+
const response = await supertest(ctMock.app)
|
|
52
|
+
.head('/dummy/custom-objects/my-container/my-key')
|
|
53
|
+
.send()
|
|
54
|
+
|
|
55
|
+
expect(response.status).toBe(200)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('non-existent', async () => {
|
|
59
|
+
const response = await supertest(ctMock.app)
|
|
60
|
+
.head('/dummy/custom-objects/invalid-container/invalid')
|
|
61
|
+
.send()
|
|
62
|
+
|
|
63
|
+
expect(response.status).toBe(404)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('get', async () => {
|
|
51
67
|
const response = await supertest(ctMock.app)
|
|
52
68
|
.get('/dummy/custom-objects/my-container/my-key')
|
|
53
69
|
.send()
|
|
@@ -26,6 +26,26 @@ describe('Customer Update Actions', () => {
|
|
|
26
26
|
ctMock.clear()
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
+
test('exists', async () => {
|
|
30
|
+
assert(customer, 'customer not created')
|
|
31
|
+
|
|
32
|
+
const response = await supertest(ctMock.app)
|
|
33
|
+
.head(`/dummy/customers/${customer.id}`)
|
|
34
|
+
.send()
|
|
35
|
+
|
|
36
|
+
expect(response.status).toBe(200)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('non-existent', async () => {
|
|
40
|
+
assert(customer, 'customer not created')
|
|
41
|
+
|
|
42
|
+
const response = await supertest(ctMock.app)
|
|
43
|
+
.head(`/dummy/customers/invalid-id`)
|
|
44
|
+
.send()
|
|
45
|
+
|
|
46
|
+
expect(response.status).toBe(404)
|
|
47
|
+
})
|
|
48
|
+
|
|
29
49
|
test('changeEmail', async () => {
|
|
30
50
|
assert(customer, 'customer not created')
|
|
31
51
|
|