@labdigital/commercetools-mock 2.51.0 → 2.52.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 (39) hide show
  1. package/dist/index.d.ts +13 -2
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +109 -7
  4. package/dist/index.js.map +1 -1
  5. package/package.json +1 -1
  6. package/src/lib/product-review-statistics.test.ts +349 -0
  7. package/src/lib/review-statistics.ts +58 -0
  8. package/src/product-projection-search.ts +17 -2
  9. package/src/repositories/as-associate.test.ts +126 -0
  10. package/src/repositories/attribute-group.test.ts +221 -0
  11. package/src/repositories/business-unit.test.ts +282 -0
  12. package/src/repositories/business-unit.ts +5 -1
  13. package/src/repositories/channel.test.ts +374 -0
  14. package/src/repositories/customer-group.test.ts +262 -0
  15. package/src/repositories/extension.test.ts +306 -0
  16. package/src/repositories/index.test.ts +17 -0
  17. package/src/repositories/product/index.ts +22 -1
  18. package/src/repositories/product-projection.ts +8 -2
  19. package/src/repositories/review.test.ts +636 -0
  20. package/src/repositories/review.ts +145 -4
  21. package/src/repositories/subscription.test.ts +207 -0
  22. package/src/repositories/zone.test.ts +278 -0
  23. package/src/services/as-associate-cart.test.ts +58 -0
  24. package/src/services/as-associate.test.ts +34 -0
  25. package/src/services/attribute-group.test.ts +114 -0
  26. package/src/services/channel.test.ts +90 -0
  27. package/src/services/customer-group.test.ts +85 -0
  28. package/src/services/discount-code.test.ts +120 -0
  29. package/src/services/extension.test.ts +130 -0
  30. package/src/services/my-business-unit.test.ts +113 -0
  31. package/src/services/my-business-unit.ts +6 -0
  32. package/src/services/my-customer.test.ts +24 -0
  33. package/src/services/order.test.ts +18 -0
  34. package/src/services/product-discount.test.ts +146 -0
  35. package/src/services/project.test.ts +17 -0
  36. package/src/services/reviews.test.ts +230 -0
  37. package/src/services/subscription.test.ts +151 -0
  38. package/src/services/type.test.ts +127 -0
  39. package/src/services/zone.test.ts +117 -0
@@ -0,0 +1,221 @@
1
+ import type {
2
+ AttributeGroupChangeNameAction,
3
+ AttributeGroupDraft,
4
+ AttributeGroupSetAttributesAction,
5
+ AttributeGroupSetDescriptionAction,
6
+ AttributeGroupSetKeyAction,
7
+ } from "@commercetools/platform-sdk";
8
+ import { describe, expect, test } from "vitest";
9
+ import type { Config } from "~src/config";
10
+ import { InMemoryStorage } from "~src/storage";
11
+ import { AttributeGroupRepository } from "./attribute-group";
12
+
13
+ describe("AttributeGroup Repository", () => {
14
+ const storage = new InMemoryStorage();
15
+ const config: Config = { storage, strict: false };
16
+ const repository = new AttributeGroupRepository(config);
17
+
18
+ test("create attribute group", () => {
19
+ const draft: AttributeGroupDraft = {
20
+ name: { "en-US": "Size Attributes", "de-DE": "Größenattribute" },
21
+ description: { "en-US": "Attributes related to product size" },
22
+ key: "size-attributes",
23
+ attributes: [
24
+ {
25
+ key: "size",
26
+ },
27
+ {
28
+ key: "weight",
29
+ },
30
+ ],
31
+ };
32
+
33
+ const ctx = { projectKey: "dummy" };
34
+ const result = repository.create(ctx, draft);
35
+
36
+ expect(result.id).toBeDefined();
37
+ expect(result.version).toBe(1);
38
+ expect(result.name).toEqual(draft.name);
39
+ expect(result.description).toEqual(draft.description);
40
+ expect(result.key).toBe(draft.key);
41
+ expect(result.attributes).toEqual(draft.attributes);
42
+
43
+ // Test that the attribute group is stored
44
+ const items = repository.query(ctx);
45
+ expect(items.count).toBe(1);
46
+ expect(items.results[0].id).toBe(result.id);
47
+ });
48
+
49
+ test("create attribute group with minimal data", () => {
50
+ const draft: AttributeGroupDraft = {
51
+ name: { "en-US": "Minimal Attributes" },
52
+ attributes: [],
53
+ };
54
+
55
+ const ctx = { projectKey: "dummy" };
56
+ const result = repository.create(ctx, draft);
57
+
58
+ expect(result.id).toBeDefined();
59
+ expect(result.name).toEqual(draft.name);
60
+ expect(result.description).toBeUndefined();
61
+ expect(result.key).toBeUndefined();
62
+ expect(result.attributes).toEqual([]);
63
+ });
64
+
65
+ test("update attribute group - changeName", () => {
66
+ const draft: AttributeGroupDraft = {
67
+ name: { "en-US": "Original Name" },
68
+ key: "test-attributes",
69
+ attributes: [],
70
+ };
71
+
72
+ const ctx = { projectKey: "dummy" };
73
+ const attributeGroup = repository.create(ctx, draft);
74
+
75
+ const result = repository.processUpdateActions(
76
+ ctx,
77
+ attributeGroup,
78
+ attributeGroup.version,
79
+ [
80
+ {
81
+ action: "changeName",
82
+ name: { "en-US": "Updated Name", "de-DE": "Aktualisierter Name" },
83
+ } as AttributeGroupChangeNameAction,
84
+ ],
85
+ );
86
+
87
+ expect(result.name).toEqual({
88
+ "en-US": "Updated Name",
89
+ "de-DE": "Aktualisierter Name",
90
+ });
91
+ expect(result.version).toBe(attributeGroup.version + 1);
92
+ });
93
+
94
+ test("update attribute group - setDescription", () => {
95
+ const draft: AttributeGroupDraft = {
96
+ name: { "en-US": "Test Attributes" },
97
+ key: "test-attributes-2",
98
+ attributes: [],
99
+ };
100
+
101
+ const ctx = { projectKey: "dummy" };
102
+ const attributeGroup = repository.create(ctx, draft);
103
+
104
+ const result = repository.processUpdateActions(
105
+ ctx,
106
+ attributeGroup,
107
+ attributeGroup.version,
108
+ [
109
+ {
110
+ action: "setDescription",
111
+ description: {
112
+ "en-US": "New description",
113
+ "de-DE": "Neue Beschreibung",
114
+ },
115
+ } as AttributeGroupSetDescriptionAction,
116
+ ],
117
+ );
118
+
119
+ expect(result.description).toEqual({
120
+ "en-US": "New description",
121
+ "de-DE": "Neue Beschreibung",
122
+ });
123
+ expect(result.version).toBe(attributeGroup.version + 1);
124
+ });
125
+
126
+ test("update attribute group - setKey", () => {
127
+ const draft: AttributeGroupDraft = {
128
+ name: { "en-US": "Key Test Attributes" },
129
+ key: "original-key",
130
+ attributes: [],
131
+ };
132
+
133
+ const ctx = { projectKey: "dummy" };
134
+ const attributeGroup = repository.create(ctx, draft);
135
+
136
+ const result = repository.processUpdateActions(
137
+ ctx,
138
+ attributeGroup,
139
+ attributeGroup.version,
140
+ [
141
+ {
142
+ action: "setKey",
143
+ key: "updated-key",
144
+ } as AttributeGroupSetKeyAction,
145
+ ],
146
+ );
147
+
148
+ expect(result.key).toBe("updated-key");
149
+ expect(result.version).toBe(attributeGroup.version + 1);
150
+ });
151
+
152
+ test("update attribute group - setAttributes", () => {
153
+ const draft: AttributeGroupDraft = {
154
+ name: { "en-US": "Attributes Test" },
155
+ key: "attributes-test",
156
+ attributes: [
157
+ {
158
+ key: "original-attribute",
159
+ },
160
+ ],
161
+ };
162
+
163
+ const ctx = { projectKey: "dummy" };
164
+ const attributeGroup = repository.create(ctx, draft);
165
+
166
+ const result = repository.processUpdateActions(
167
+ ctx,
168
+ attributeGroup,
169
+ attributeGroup.version,
170
+ [
171
+ {
172
+ action: "setAttributes",
173
+ attributes: [
174
+ {
175
+ key: "new-attribute-1",
176
+ },
177
+ {
178
+ key: "new-attribute-2",
179
+ },
180
+ ],
181
+ } as AttributeGroupSetAttributesAction,
182
+ ],
183
+ );
184
+
185
+ expect(result.attributes).toEqual([
186
+ { key: "new-attribute-1" },
187
+ { key: "new-attribute-2" },
188
+ ]);
189
+ expect(result.version).toBe(attributeGroup.version + 1);
190
+ });
191
+
192
+ test("get and delete attribute group", () => {
193
+ const draft: AttributeGroupDraft = {
194
+ name: { "en-US": "Delete Test Attributes" },
195
+ key: "delete-test",
196
+ attributes: [],
197
+ };
198
+
199
+ const ctx = { projectKey: "dummy" };
200
+ const attributeGroup = repository.create(ctx, draft);
201
+
202
+ // Test get
203
+ const retrieved = repository.get(ctx, attributeGroup.id);
204
+ expect(retrieved).toBeDefined();
205
+ expect(retrieved?.id).toBe(attributeGroup.id);
206
+
207
+ // Test getByKey
208
+ const retrievedByKey = repository.getByKey(ctx, attributeGroup.key!);
209
+ expect(retrievedByKey).toBeDefined();
210
+ expect(retrievedByKey?.key).toBe(attributeGroup.key);
211
+
212
+ // Test delete
213
+ const deleted = repository.delete(ctx, attributeGroup.id);
214
+ expect(deleted).toBeDefined();
215
+ expect(deleted?.id).toBe(attributeGroup.id);
216
+
217
+ // Verify it's deleted
218
+ const notFound = repository.get(ctx, attributeGroup.id);
219
+ expect(notFound).toBeNull();
220
+ });
221
+ });
@@ -0,0 +1,282 @@
1
+ import type {
2
+ BusinessUnitChangeNameAction,
3
+ BusinessUnitChangeStatusAction,
4
+ BusinessUnitSetContactEmailAction,
5
+ CompanyDraft,
6
+ DivisionDraft,
7
+ } from "@commercetools/platform-sdk";
8
+ import { describe, expect, test } from "vitest";
9
+ import type { Config } from "~src/config";
10
+ import { getBaseResourceProperties } from "~src/helpers";
11
+ import { InMemoryStorage } from "~src/storage";
12
+ import { BusinessUnitRepository } from "./business-unit";
13
+ import { CustomerRepository } from "./customer";
14
+
15
+ describe("BusinessUnit Repository", () => {
16
+ const storage = new InMemoryStorage();
17
+ const config: Config = { storage, strict: false };
18
+ const repository = new BusinessUnitRepository(config);
19
+
20
+ // Add required dependencies for testing
21
+ storage.add("dummy", "store", {
22
+ ...getBaseResourceProperties(),
23
+ id: "store-123",
24
+ key: "test-store",
25
+ name: { "en-US": "Test Store" },
26
+ languages: ["en-US"],
27
+ countries: [{ code: "US" }],
28
+ distributionChannels: [],
29
+ supplyChannels: [],
30
+ productSelections: [],
31
+ });
32
+
33
+ // Create a proper customer using the customer repository
34
+ const customerRepository = new CustomerRepository(config);
35
+ const customer = customerRepository.create(
36
+ { projectKey: "dummy" },
37
+ {
38
+ email: "associate@example.com",
39
+ password: "password123",
40
+ firstName: "John",
41
+ lastName: "Associate",
42
+ },
43
+ );
44
+
45
+ test("create company business unit", () => {
46
+ const draft: CompanyDraft = {
47
+ key: "test-company",
48
+ unitType: "Company",
49
+ status: "Active",
50
+ name: "Test Company Inc.",
51
+ contactEmail: "contact@testcompany.com",
52
+ storeMode: "Explicit",
53
+ associateMode: "Explicit",
54
+ approvalRuleMode: "ExplicitApproval",
55
+ stores: [
56
+ {
57
+ typeId: "store",
58
+ key: "test-store",
59
+ },
60
+ ],
61
+ addresses: [
62
+ {
63
+ country: "US",
64
+ city: "New York",
65
+ streetName: "5th Avenue",
66
+ streetNumber: "123",
67
+ postalCode: "10001",
68
+ },
69
+ ],
70
+ defaultBillingAddress: 0,
71
+ defaultShippingAddress: 0,
72
+ billingAddresses: [0],
73
+ shippingAddresses: [0],
74
+ associates: [
75
+ {
76
+ customer: {
77
+ typeId: "customer",
78
+ id: customer.id,
79
+ },
80
+ associateRoleAssignments: [
81
+ {
82
+ associateRole: {
83
+ typeId: "associate-role",
84
+ key: "admin",
85
+ },
86
+ inheritance: "Enabled",
87
+ },
88
+ ],
89
+ },
90
+ ],
91
+ };
92
+
93
+ const ctx = { projectKey: "dummy" };
94
+ const result = repository.create(ctx, draft);
95
+
96
+ expect(result.id).toBeDefined();
97
+ expect(result.version).toBe(1);
98
+ expect(result.key).toBe(draft.key);
99
+ expect(result.unitType).toBe("Company");
100
+ expect(result.name).toBe(draft.name);
101
+ expect(result.contactEmail).toBe(draft.contactEmail);
102
+ expect(result.status).toBe(draft.status);
103
+ expect(result.storeMode).toBe(draft.storeMode);
104
+ expect(result.associateMode).toBe(draft.associateMode);
105
+ expect(result.approvalRuleMode).toBe(draft.approvalRuleMode);
106
+ expect(result.addresses).toHaveLength(1);
107
+ expect(result.addresses[0].country).toBe("US");
108
+ expect(result.stores).toHaveLength(1);
109
+ expect(result.associates).toHaveLength(1);
110
+
111
+ // Test that the business unit is stored
112
+ const items = repository.query(ctx);
113
+ expect(items.count).toBe(1);
114
+ expect(items.results[0].id).toBe(result.id);
115
+ });
116
+
117
+ test("create division business unit", () => {
118
+ // First create a company to be the parent
119
+ const companyDraft: CompanyDraft = {
120
+ key: "parent-company",
121
+ unitType: "Company",
122
+ status: "Active",
123
+ name: "Parent Company",
124
+ };
125
+
126
+ const company = repository.create({ projectKey: "dummy" }, companyDraft);
127
+
128
+ const draft: DivisionDraft = {
129
+ key: "test-division",
130
+ unitType: "Division",
131
+ status: "Active",
132
+ name: "Test Division",
133
+ contactEmail: "division@testcompany.com",
134
+ parentUnit: {
135
+ typeId: "business-unit",
136
+ key: "parent-company",
137
+ },
138
+ };
139
+
140
+ const ctx = { projectKey: "dummy" };
141
+ const result = repository.create(ctx, draft);
142
+
143
+ expect(result.id).toBeDefined();
144
+ expect(result.key).toBe(draft.key);
145
+ expect(result.unitType).toBe("Division");
146
+ expect(result.name).toBe(draft.name);
147
+ expect(result.contactEmail).toBe(draft.contactEmail);
148
+
149
+ // Check division-specific properties
150
+ if (result.unitType === "Division") {
151
+ expect(result.parentUnit?.key).toBe("parent-company");
152
+ }
153
+ });
154
+
155
+ test("create business unit with minimal data", () => {
156
+ const draft: CompanyDraft = {
157
+ key: "minimal-company",
158
+ unitType: "Company",
159
+ name: "Minimal Company",
160
+ };
161
+
162
+ const ctx = { projectKey: "dummy" };
163
+ const result = repository.create(ctx, draft);
164
+
165
+ expect(result.id).toBeDefined();
166
+ expect(result.key).toBe(draft.key);
167
+ expect(result.name).toBe(draft.name);
168
+ expect(result.unitType).toBe("Company");
169
+ expect(result.addresses).toEqual([]);
170
+ expect(result.associates).toEqual([]);
171
+ expect(result.stores).toBeUndefined();
172
+ });
173
+
174
+ test("update business unit - changeName", () => {
175
+ const draft: CompanyDraft = {
176
+ key: "update-test-company",
177
+ unitType: "Company",
178
+ name: "Original Company Name",
179
+ };
180
+
181
+ const ctx = { projectKey: "dummy" };
182
+ const businessUnit = repository.create(ctx, draft);
183
+
184
+ const result = repository.processUpdateActions(
185
+ ctx,
186
+ businessUnit,
187
+ businessUnit.version,
188
+ [
189
+ {
190
+ action: "changeName",
191
+ name: "Updated Company Name",
192
+ } as BusinessUnitChangeNameAction,
193
+ ],
194
+ );
195
+
196
+ expect(result.name).toBe("Updated Company Name");
197
+ expect(result.version).toBe(businessUnit.version + 1);
198
+ });
199
+
200
+ test("update business unit - setContactEmail", () => {
201
+ const draft: CompanyDraft = {
202
+ key: "email-test-company",
203
+ unitType: "Company",
204
+ name: "Email Test Company",
205
+ };
206
+
207
+ const ctx = { projectKey: "dummy" };
208
+ const businessUnit = repository.create(ctx, draft);
209
+
210
+ const result = repository.processUpdateActions(
211
+ ctx,
212
+ businessUnit,
213
+ businessUnit.version,
214
+ [
215
+ {
216
+ action: "setContactEmail",
217
+ contactEmail: "newemail@company.com",
218
+ } as BusinessUnitSetContactEmailAction,
219
+ ],
220
+ );
221
+
222
+ expect(result.contactEmail).toBe("newemail@company.com");
223
+ expect(result.version).toBe(businessUnit.version + 1);
224
+ });
225
+
226
+ test("update business unit - changeStatus", () => {
227
+ const draft: CompanyDraft = {
228
+ key: "status-test-company",
229
+ unitType: "Company",
230
+ name: "Status Test Company",
231
+ status: "Active",
232
+ };
233
+
234
+ const ctx = { projectKey: "dummy" };
235
+ const businessUnit = repository.create(ctx, draft);
236
+
237
+ const result = repository.processUpdateActions(
238
+ ctx,
239
+ businessUnit,
240
+ businessUnit.version,
241
+ [
242
+ {
243
+ action: "changeStatus",
244
+ status: "Inactive",
245
+ } as BusinessUnitChangeStatusAction,
246
+ ],
247
+ );
248
+
249
+ expect(result.status).toBe("Inactive");
250
+ expect(result.version).toBe(businessUnit.version + 1);
251
+ });
252
+
253
+ test("get and delete business unit", () => {
254
+ const draft: CompanyDraft = {
255
+ key: "delete-test",
256
+ unitType: "Company",
257
+ name: "Delete Test Company",
258
+ };
259
+
260
+ const ctx = { projectKey: "dummy" };
261
+ const businessUnit = repository.create(ctx, draft);
262
+
263
+ // Test get
264
+ const retrieved = repository.get(ctx, businessUnit.id);
265
+ expect(retrieved).toBeDefined();
266
+ expect(retrieved?.id).toBe(businessUnit.id);
267
+
268
+ // Test getByKey
269
+ const retrievedByKey = repository.getByKey(ctx, businessUnit.key!);
270
+ expect(retrievedByKey).toBeDefined();
271
+ expect(retrievedByKey?.key).toBe(businessUnit.key);
272
+
273
+ // Test delete
274
+ const deleted = repository.delete(ctx, businessUnit.id);
275
+ expect(deleted).toBeDefined();
276
+ expect(deleted?.id).toBe(businessUnit.id);
277
+
278
+ // Verify it's deleted
279
+ const notFound = repository.get(ctx, businessUnit.id);
280
+ expect(notFound).toBeNull();
281
+ });
282
+ });
@@ -101,6 +101,7 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
101
101
  if (this._isDivisionDraft(draft)) {
102
102
  const division = {
103
103
  ...resource,
104
+ unitType: "Division" as const,
104
105
  parentUnit: getBusinessUnitKeyReference(
105
106
  draft.parentUnit,
106
107
  context.projectKey,
@@ -112,7 +113,10 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
112
113
  return division;
113
114
  }
114
115
  if (this._isCompanyDraft(draft)) {
115
- const company = resource as Company;
116
+ const company = {
117
+ ...resource,
118
+ unitType: "Company" as const,
119
+ } as Company;
116
120
 
117
121
  this.saveNew(context, company);
118
122
  return company;