@labdigital/commercetools-mock 2.34.0 → 2.34.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.34.0",
3
+ "version": "2.34.2",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -23,6 +23,7 @@
23
23
  "body-parser": "^1.20.2",
24
24
  "deep-equal": "^2.2.3",
25
25
  "express": "^4.19.2",
26
+ "light-my-request": "^5.11.1",
26
27
  "lodash.isequal": "^4.5.0",
27
28
  "morgan": "^1.10.0",
28
29
  "msw": "^2.2.1",
package/src/ctMock.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import express, { NextFunction, Request, Response } from "express";
2
+ import inject from "light-my-request";
2
3
  import morgan from "morgan";
3
4
  import { http, HttpResponse } from "msw";
4
5
  import { setupServer, SetupServer, SetupServerApi } from "msw/node";
5
- import supertest from "supertest";
6
6
  import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from "./constants";
7
7
  import { CommercetoolsError } from "./exceptions";
8
8
  import { copyHeaders } from "./lib/proxy";
@@ -193,12 +193,12 @@ export class CommercetoolsMock {
193
193
  const url = new URL(request.url);
194
194
  const headers = copyHeaders(request.headers);
195
195
 
196
- const res = await supertest(app)
196
+ const res = await inject(app)
197
197
  .post(url.pathname + "?" + url.searchParams.toString())
198
- .send(body)
199
- .set(headers);
200
-
201
- return new HttpResponse(JSON.stringify(res.body), {
198
+ .body(body)
199
+ .headers(headers)
200
+ .end();
201
+ return new HttpResponse(res.body, {
202
202
  status: res.statusCode,
203
203
  headers: mapHeaderType(res.headers),
204
204
  });
@@ -208,10 +208,11 @@ export class CommercetoolsMock {
208
208
  const url = new URL(request.url);
209
209
  const headers = copyHeaders(request.headers);
210
210
 
211
- const res = await supertest(app)
211
+ const res = await inject(app)
212
212
  .get(url.pathname + "?" + url.searchParams.toString())
213
- .send(body)
214
- .set(headers);
213
+ .body(body)
214
+ .headers(headers)
215
+ .end();
215
216
 
216
217
  if (res.statusCode === 200) {
217
218
  const parsedBody = JSON.parse(res.body);
@@ -238,11 +239,12 @@ export class CommercetoolsMock {
238
239
  const url = new URL(request.url);
239
240
  const headers = copyHeaders(request.headers);
240
241
 
241
- const res = await supertest(app)
242
+ const res = await inject(app)
242
243
  .get(url.pathname + "?" + url.searchParams.toString())
243
- .send(body)
244
- .set(headers);
245
- return new HttpResponse(JSON.stringify(res.body), {
244
+ .body(body)
245
+ .headers(headers)
246
+ .end();
247
+ return new HttpResponse(res.body, {
246
248
  status: res.statusCode,
247
249
  headers: mapHeaderType(res.headers),
248
250
  });
@@ -252,10 +254,11 @@ export class CommercetoolsMock {
252
254
  const url = new URL(request.url);
253
255
  const headers = copyHeaders(request.headers);
254
256
 
255
- const res = await supertest(app)
257
+ const res = await inject(app)
256
258
  .post(url.pathname + "?" + url.searchParams.toString())
257
- .send(body)
258
- .set(headers);
259
+ .body(body)
260
+ .headers(headers)
261
+ .end();
259
262
  return new HttpResponse(res.body, {
260
263
  status: res.statusCode,
261
264
  headers: mapHeaderType(res.headers),
@@ -266,10 +269,11 @@ export class CommercetoolsMock {
266
269
  const url = new URL(request.url);
267
270
  const headers = copyHeaders(request.headers);
268
271
 
269
- const res = await supertest(app)
272
+ const res = await inject(app)
270
273
  .delete(url.pathname + "?" + url.searchParams.toString())
271
- .send(body)
272
- .set(headers);
274
+ .body(body)
275
+ .headers(headers)
276
+ .end();
273
277
  return new HttpResponse(res.body, {
274
278
  status: res.statusCode,
275
279
  headers: mapHeaderType(res.headers),
package/src/index.test.ts CHANGED
@@ -1,16 +1,10 @@
1
1
  import { type InvalidTokenError } from "@commercetools/platform-sdk";
2
2
  import got from "got";
3
- import { afterEach, expect, test } from "vitest";
3
+ import { expect, test } from "vitest";
4
4
  import { CommercetoolsMock } from "./index";
5
5
 
6
- let ctMock: CommercetoolsMock;
7
-
8
- afterEach(() => {
9
- ctMock.stop();
10
- });
11
-
12
6
  test("node:fetch client", async () => {
13
- ctMock = new CommercetoolsMock({
7
+ const ctMock = new CommercetoolsMock({
14
8
  enableAuthentication: true,
15
9
  validateCredentials: true,
16
10
  apiHost: "https://localhost",
@@ -50,10 +44,11 @@ test("node:fetch client", async () => {
50
44
  limit: 20,
51
45
  results: [],
52
46
  });
47
+ ctMock.stop();
53
48
  });
54
49
 
55
50
  test("got client", async () => {
56
- ctMock = new CommercetoolsMock({
51
+ const ctMock = new CommercetoolsMock({
57
52
  enableAuthentication: true,
58
53
  validateCredentials: true,
59
54
  apiHost: "https://localhost",
@@ -91,10 +86,11 @@ test("got client", async () => {
91
86
  limit: 20,
92
87
  results: [],
93
88
  });
89
+ ctMock.stop();
94
90
  });
95
91
 
96
92
  test("Options.validateCredentials: true (error)", async () => {
97
- ctMock = new CommercetoolsMock({
93
+ const ctMock = new CommercetoolsMock({
98
94
  enableAuthentication: true,
99
95
  validateCredentials: true,
100
96
  });
@@ -112,10 +108,11 @@ test("Options.validateCredentials: true (error)", async () => {
112
108
  );
113
109
  expect(response.statusCode).toBe(401);
114
110
  expect(response.body.message).toBe("invalid_token");
111
+ ctMock.stop();
115
112
  });
116
113
 
117
114
  test("Options.validateCredentials: false", async () => {
118
- ctMock = new CommercetoolsMock({
115
+ const ctMock = new CommercetoolsMock({
119
116
  enableAuthentication: true,
120
117
  validateCredentials: false,
121
118
  });
@@ -138,6 +135,7 @@ test("Options.validateCredentials: false", async () => {
138
135
  limit: 20,
139
136
  results: [],
140
137
  });
138
+ ctMock.stop();
141
139
  });
142
140
 
143
141
  test("Options.enableAuthentication: false", async () => {
@@ -161,10 +159,11 @@ test("Options.enableAuthentication: false", async () => {
161
159
  limit: 20,
162
160
  results: [],
163
161
  });
162
+ ctMock.stop();
164
163
  });
165
164
 
166
165
  test("Options.apiHost: is overridden is set", async () => {
167
- ctMock = new CommercetoolsMock({
166
+ const ctMock = new CommercetoolsMock({
168
167
  enableAuthentication: false,
169
168
  validateCredentials: false,
170
169
  apiHost: "http://api.localhost",
@@ -182,10 +181,11 @@ test("Options.apiHost: is overridden is set", async () => {
182
181
  limit: 20,
183
182
  results: [],
184
183
  });
184
+ ctMock.stop();
185
185
  });
186
186
 
187
187
  test("Options.authHost: is set", async () => {
188
- ctMock = new CommercetoolsMock({
188
+ const ctMock = new CommercetoolsMock({
189
189
  enableAuthentication: true,
190
190
  validateCredentials: true,
191
191
  authHost: "http://auth.localhost",
@@ -211,7 +211,7 @@ test("Options.authHost: is set", async () => {
211
211
  });
212
212
 
213
213
  test("apiHost mock proxy: querystring", async () => {
214
- ctMock = new CommercetoolsMock({
214
+ const ctMock = new CommercetoolsMock({
215
215
  enableAuthentication: false,
216
216
  validateCredentials: false,
217
217
  apiHost: "http://api.localhost",
@@ -234,4 +234,5 @@ test("apiHost mock proxy: querystring", async () => {
234
234
  limit: 20,
235
235
  results: [],
236
236
  });
237
+ ctMock.stop();
237
238
  });
@@ -1,4 +1,8 @@
1
- import type { Cart, OrderImportDraft } from "@commercetools/platform-sdk";
1
+ import type {
2
+ Cart,
3
+ LineItem,
4
+ OrderImportDraft,
5
+ } from "@commercetools/platform-sdk";
2
6
  import { describe, expect, test } from "vitest";
3
7
  import { InMemoryStorage } from "~src/storage";
4
8
  import { OrderRepository } from "./index";
@@ -17,7 +21,14 @@ describe("Order repository", () => {
17
21
  directDiscounts: [],
18
22
  inventoryMode: "None",
19
23
  itemShippingAddresses: [],
20
- lineItems: [],
24
+ lineItems: [
25
+ {
26
+ id: "15fc56ba-a74e-4cf8-b4b0-bada5c101541",
27
+ productId: "PRODUCTID",
28
+ variantId: 1,
29
+ quantity: 1,
30
+ } as unknown as LineItem,
31
+ ],
21
32
  customLineItems: [],
22
33
  totalPrice: {
23
34
  type: "centPrecision",
@@ -33,6 +44,90 @@ describe("Order repository", () => {
33
44
  taxCalculationMode: "UnitPriceLevel",
34
45
  refusedGifts: [],
35
46
  origin: "Customer",
47
+ anonymousId: "1234567890",
48
+ billingAddress: {
49
+ id: "1234567890",
50
+ country: "NL",
51
+ firstName: "John",
52
+ lastName: "Doe",
53
+ streetName: "Main Street",
54
+ streetNumber: "123",
55
+ postalCode: "123456",
56
+ },
57
+ customerEmail: "john.doe@example.com",
58
+ customerGroup: {
59
+ id: "1234567890",
60
+ typeId: "customer-group",
61
+ },
62
+ customerId: "1234567890",
63
+ custom: {
64
+ type: {
65
+ typeId: "type",
66
+ id: "1234567890",
67
+ },
68
+ fields: {
69
+ description: "example description",
70
+ },
71
+ },
72
+
73
+ shippingAddress: {
74
+ id: "1234567890",
75
+ country: "NL",
76
+ firstName: "John",
77
+ lastName: "Doe",
78
+ streetName: "Main Street",
79
+ streetNumber: "123",
80
+ postalCode: "123456",
81
+ },
82
+ shippingInfo: {
83
+ shippingMethodName: "Standard Shipping",
84
+ price: {
85
+ type: "centPrecision",
86
+ currencyCode: "EUR",
87
+ centAmount: 1000,
88
+ fractionDigits: 2,
89
+ },
90
+ shippingRate: {
91
+ price: {
92
+ type: "centPrecision",
93
+ currencyCode: "EUR",
94
+ centAmount: 1000,
95
+ fractionDigits: 2,
96
+ },
97
+ tiers: [],
98
+ },
99
+ shippingMethodState: "Shipped",
100
+ },
101
+ taxedPrice: {
102
+ totalNet: {
103
+ type: "centPrecision",
104
+ currencyCode: "EUR",
105
+ centAmount: 1000,
106
+ fractionDigits: 2,
107
+ },
108
+ taxPortions: [],
109
+ totalGross: {
110
+ type: "centPrecision",
111
+ currencyCode: "EUR",
112
+ centAmount: 1210,
113
+ fractionDigits: 2,
114
+ },
115
+ },
116
+ taxedShippingPrice: {
117
+ totalNet: {
118
+ type: "centPrecision",
119
+ currencyCode: "EUR",
120
+ centAmount: 100,
121
+ fractionDigits: 2,
122
+ },
123
+ taxPortions: [],
124
+ totalGross: {
125
+ type: "centPrecision",
126
+ currencyCode: "EUR",
127
+ centAmount: 121,
128
+ fractionDigits: 2,
129
+ },
130
+ },
36
131
  };
37
132
 
38
133
  storage.add("dummy", "cart", cart);
@@ -49,50 +144,37 @@ describe("Order repository", () => {
49
144
 
50
145
  const items = repository.query(ctx);
51
146
  expect(items.count).toBe(1);
52
- });
53
-
54
- test("create from cart - in store", async () => {
55
- const cart: Cart = {
56
- id: "b3875a58-4ab2-4aaa-b399-184ce7561c27",
57
- version: 1,
58
- createdAt: "2021-09-02T12:23:30.036Z",
59
- lastModifiedAt: "2021-09-02T12:23:30.546Z",
60
- discountCodes: [],
61
- directDiscounts: [],
62
- inventoryMode: "None",
63
- itemShippingAddresses: [],
64
- lineItems: [],
65
- customLineItems: [],
66
- totalPrice: {
67
- type: "centPrecision",
68
- currencyCode: "EUR",
69
- centAmount: 10000,
70
- fractionDigits: 2,
71
- },
72
- cartState: "Active",
73
- shippingMode: "Single",
74
- shipping: [],
75
- taxMode: "Platform",
76
- taxRoundingMode: "HalfEven",
77
- taxCalculationMode: "UnitPriceLevel",
78
- refusedGifts: [],
79
- origin: "Customer",
80
- };
81
-
82
- storage.add("dummy", "cart", cart);
83
147
 
84
- const result = repository.create(
85
- { projectKey: "dummy", storeKey: "some-store" },
86
- {
87
- cart: {
88
- id: cart.id,
89
- typeId: "cart",
90
- },
91
- version: cart.version,
92
- },
93
- );
94
- expect(result.cart?.id).toBe(cart.id);
95
- expect(result.store?.key).toBe("some-store");
148
+ expect(result.orderNumber).not.toBeUndefined();
149
+ expect(result.anonymousId).toEqual(cart.anonymousId);
150
+ expect(result.billingAddress).toEqual(cart.billingAddress);
151
+ expect(result.cart?.id).toEqual(cart.id);
152
+ expect(result.country).toEqual(cart.country);
153
+ expect(result.custom).toEqual(cart.custom);
154
+ expect(result.customerEmail).toEqual(cart.customerEmail);
155
+ expect(result.customerGroup).toEqual(cart.customerGroup);
156
+ expect(result.customerId).toEqual(cart.customerId);
157
+ expect(result.customLineItems).toEqual(cart.customLineItems);
158
+ expect(result.directDiscounts).toEqual(cart.directDiscounts);
159
+ expect(result.discountCodes).toEqual(cart.discountCodes);
160
+ expect(result.discountOnTotalPrice).toEqual(cart.discountOnTotalPrice);
161
+ expect(result.lineItems).toEqual(cart.lineItems);
162
+ expect(result.locale).toEqual(cart.locale);
163
+ expect(result.orderState).toEqual("Open");
164
+ expect(result.origin).toEqual(cart.origin);
165
+ expect(result.paymentInfo).toEqual(cart.paymentInfo);
166
+ expect(result.refusedGifts).toEqual(cart.refusedGifts);
167
+ expect(result.shipping).toEqual(cart.shipping);
168
+ expect(result.shippingAddress).toEqual(cart.shippingAddress);
169
+ expect(result.shippingMode).toEqual(cart.shippingMode);
170
+ expect(result.syncInfo).toEqual([]);
171
+ expect(result.taxCalculationMode).toEqual(cart.taxCalculationMode);
172
+ expect(result.taxedPrice).toEqual(cart.taxedPrice);
173
+ expect(result.taxedShippingPrice).toEqual(cart.taxedShippingPrice);
174
+ expect(result.taxMode).toEqual(cart.taxMode);
175
+ expect(result.taxRoundingMode).toEqual(cart.taxRoundingMode);
176
+ expect(result.totalPrice).toEqual(cart.totalPrice);
177
+ expect(result.store).toEqual(cart.store);
96
178
  });
97
179
 
98
180
  test("import exiting product", async () => {
@@ -15,7 +15,7 @@ import type {
15
15
  } from "@commercetools/platform-sdk";
16
16
  import assert from "assert";
17
17
  import { CommercetoolsError } from "~src/exceptions";
18
- import { getBaseResourceProperties } from "~src/helpers";
18
+ import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
19
19
  import { AbstractStorage } from "~src/storage/abstract";
20
20
  import {
21
21
  AbstractResourceRepository,
@@ -65,25 +65,37 @@ export class OrderRepository extends AbstractResourceRepository<"order"> {
65
65
 
66
66
  const resource: Order = {
67
67
  ...getBaseResourceProperties(),
68
- orderNumber,
68
+ anonymousId: cart.anonymousId,
69
+ billingAddress: cart.billingAddress,
69
70
  cart: cartReference,
70
- orderState: "Open",
71
- lineItems: [],
71
+ country: cart.country,
72
+ custom: cart.custom,
73
+ customerEmail: cart.customerEmail,
74
+ customerGroup: cart.customerGroup,
75
+ customerId: cart.customerId,
72
76
  customLineItems: [],
73
- totalPrice: cart.totalPrice,
74
- refusedGifts: [],
77
+ directDiscounts: cart.directDiscounts,
78
+ discountCodes: cart.discountCodes,
79
+ discountOnTotalPrice: cart.discountOnTotalPrice,
80
+ lastMessageSequenceNumber: 0,
81
+ lineItems: cart.lineItems,
82
+ locale: cart.locale,
83
+ orderNumber: orderNumber ?? generateRandomString(10),
84
+ orderState: "Open",
75
85
  origin: "Customer",
76
- syncInfo: [],
77
- shippingMode: cart.shippingMode,
86
+ paymentInfo: cart.paymentInfo,
87
+ refusedGifts: [],
78
88
  shipping: cart.shipping,
79
- store: context.storeKey
80
- ? {
81
- key: context.storeKey,
82
- typeId: "store",
83
- }
84
- : undefined,
85
- custom: cart.custom,
86
- lastMessageSequenceNumber: 0,
89
+ shippingAddress: cart.shippingAddress,
90
+ shippingMode: cart.shippingMode,
91
+ syncInfo: [],
92
+ taxCalculationMode: cart.taxCalculationMode,
93
+ taxedPrice: cart.taxedPrice,
94
+ taxedShippingPrice: cart.taxedShippingPrice,
95
+ taxMode: cart.taxMode,
96
+ taxRoundingMode: cart.taxRoundingMode,
97
+ totalPrice: cart.totalPrice,
98
+ store: cart.store,
87
99
  };
88
100
  return this.saveNew(context, resource);
89
101
  }