@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.
- package/dist/index.d.ts +13 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +109 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/product-review-statistics.test.ts +349 -0
- package/src/lib/review-statistics.ts +58 -0
- package/src/product-projection-search.ts +17 -2
- package/src/repositories/as-associate.test.ts +126 -0
- package/src/repositories/attribute-group.test.ts +221 -0
- package/src/repositories/business-unit.test.ts +282 -0
- package/src/repositories/business-unit.ts +5 -1
- package/src/repositories/channel.test.ts +374 -0
- package/src/repositories/customer-group.test.ts +262 -0
- package/src/repositories/extension.test.ts +306 -0
- package/src/repositories/index.test.ts +17 -0
- package/src/repositories/product/index.ts +22 -1
- package/src/repositories/product-projection.ts +8 -2
- package/src/repositories/review.test.ts +636 -0
- package/src/repositories/review.ts +145 -4
- package/src/repositories/subscription.test.ts +207 -0
- package/src/repositories/zone.test.ts +278 -0
- package/src/services/as-associate-cart.test.ts +58 -0
- package/src/services/as-associate.test.ts +34 -0
- package/src/services/attribute-group.test.ts +114 -0
- package/src/services/channel.test.ts +90 -0
- package/src/services/customer-group.test.ts +85 -0
- package/src/services/discount-code.test.ts +120 -0
- package/src/services/extension.test.ts +130 -0
- package/src/services/my-business-unit.test.ts +113 -0
- package/src/services/my-business-unit.ts +6 -0
- package/src/services/my-customer.test.ts +24 -0
- package/src/services/order.test.ts +18 -0
- package/src/services/product-discount.test.ts +146 -0
- package/src/services/project.test.ts +17 -0
- package/src/services/reviews.test.ts +230 -0
- package/src/services/subscription.test.ts +151 -0
- package/src/services/type.test.ts +127 -0
- package/src/services/zone.test.ts +117 -0
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ChannelReference,
|
|
3
|
+
CustomerReference,
|
|
3
4
|
ProductReference,
|
|
4
5
|
} from "@commercetools/platform-sdk";
|
|
5
6
|
import type {
|
|
6
7
|
Review,
|
|
7
8
|
ReviewDraft,
|
|
9
|
+
ReviewSetAuthorNameAction,
|
|
10
|
+
ReviewSetCustomFieldAction,
|
|
11
|
+
ReviewSetCustomTypeAction,
|
|
12
|
+
ReviewSetCustomerAction,
|
|
13
|
+
ReviewSetKeyAction,
|
|
14
|
+
ReviewSetLocaleAction,
|
|
15
|
+
ReviewSetRatingAction,
|
|
16
|
+
ReviewSetTargetAction,
|
|
17
|
+
ReviewSetTextAction,
|
|
18
|
+
ReviewSetTitleAction,
|
|
19
|
+
ReviewTransitionStateAction,
|
|
20
|
+
ReviewUpdateAction,
|
|
8
21
|
StateReference,
|
|
9
22
|
} from "@commercetools/platform-sdk";
|
|
10
23
|
import type { Config } from "~src/config";
|
|
11
24
|
import { getBaseResourceProperties } from "../helpers";
|
|
12
|
-
import type {
|
|
13
|
-
import {
|
|
25
|
+
import type { Writable } from "../types";
|
|
26
|
+
import type { RepositoryContext, UpdateHandlerInterface } from "./abstract";
|
|
27
|
+
import { AbstractResourceRepository, AbstractUpdateHandler } from "./abstract";
|
|
14
28
|
import {
|
|
15
29
|
createCustomFields,
|
|
16
30
|
getReferenceFromResourceIdentifier,
|
|
@@ -19,13 +33,14 @@ import {
|
|
|
19
33
|
export class ReviewRepository extends AbstractResourceRepository<"review"> {
|
|
20
34
|
constructor(config: Config) {
|
|
21
35
|
super("review", config);
|
|
36
|
+
this.actions = new ReviewUpdateHandler(config.storage);
|
|
22
37
|
}
|
|
23
38
|
|
|
24
39
|
create(context: RepositoryContext, draft: ReviewDraft): Review {
|
|
25
40
|
if (!draft.target) throw new Error("Missing target");
|
|
26
41
|
const resource: Review = {
|
|
27
42
|
...getBaseResourceProperties(),
|
|
28
|
-
|
|
43
|
+
key: draft.key,
|
|
29
44
|
locale: draft.locale,
|
|
30
45
|
authorName: draft.authorName,
|
|
31
46
|
title: draft.title,
|
|
@@ -44,7 +59,7 @@ export class ReviewRepository extends AbstractResourceRepository<"review"> {
|
|
|
44
59
|
ProductReference | ChannelReference
|
|
45
60
|
>(draft.target, context.projectKey, this._storage)
|
|
46
61
|
: undefined,
|
|
47
|
-
includedInStatistics:
|
|
62
|
+
includedInStatistics: true,
|
|
48
63
|
custom: createCustomFields(
|
|
49
64
|
draft.custom,
|
|
50
65
|
context.projectKey,
|
|
@@ -54,3 +69,129 @@ export class ReviewRepository extends AbstractResourceRepository<"review"> {
|
|
|
54
69
|
return this.saveNew(context, resource);
|
|
55
70
|
}
|
|
56
71
|
}
|
|
72
|
+
|
|
73
|
+
class ReviewUpdateHandler
|
|
74
|
+
extends AbstractUpdateHandler
|
|
75
|
+
implements UpdateHandlerInterface<Review, ReviewUpdateAction>
|
|
76
|
+
{
|
|
77
|
+
setAuthorName(
|
|
78
|
+
context: RepositoryContext,
|
|
79
|
+
resource: Writable<Review>,
|
|
80
|
+
{ authorName }: ReviewSetAuthorNameAction,
|
|
81
|
+
) {
|
|
82
|
+
resource.authorName = authorName;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
setCustomField(
|
|
86
|
+
context: RepositoryContext,
|
|
87
|
+
resource: Writable<Review>,
|
|
88
|
+
{ name, value }: ReviewSetCustomFieldAction,
|
|
89
|
+
) {
|
|
90
|
+
if (!resource.custom) {
|
|
91
|
+
throw new Error("Resource has no custom field");
|
|
92
|
+
}
|
|
93
|
+
resource.custom.fields[name] = value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setCustomType(
|
|
97
|
+
context: RepositoryContext,
|
|
98
|
+
resource: Writable<Review>,
|
|
99
|
+
{ type, fields }: ReviewSetCustomTypeAction,
|
|
100
|
+
) {
|
|
101
|
+
if (!type) {
|
|
102
|
+
resource.custom = undefined;
|
|
103
|
+
} else {
|
|
104
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
105
|
+
context.projectKey,
|
|
106
|
+
type,
|
|
107
|
+
);
|
|
108
|
+
if (!resolvedType) {
|
|
109
|
+
throw new Error(`Type ${type} not found`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
resource.custom = {
|
|
113
|
+
type: {
|
|
114
|
+
typeId: "type",
|
|
115
|
+
id: resolvedType.id,
|
|
116
|
+
},
|
|
117
|
+
fields: fields ?? {},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
setCustomer(
|
|
123
|
+
context: RepositoryContext,
|
|
124
|
+
resource: Writable<Review>,
|
|
125
|
+
{ customer }: ReviewSetCustomerAction,
|
|
126
|
+
) {
|
|
127
|
+
resource.customer = customer
|
|
128
|
+
? getReferenceFromResourceIdentifier<CustomerReference>(
|
|
129
|
+
customer,
|
|
130
|
+
context.projectKey,
|
|
131
|
+
this._storage,
|
|
132
|
+
)
|
|
133
|
+
: undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setKey(
|
|
137
|
+
context: RepositoryContext,
|
|
138
|
+
resource: Writable<Review>,
|
|
139
|
+
{ key }: ReviewSetKeyAction,
|
|
140
|
+
) {
|
|
141
|
+
resource.key = key;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
setLocale(
|
|
145
|
+
context: RepositoryContext,
|
|
146
|
+
resource: Writable<Review>,
|
|
147
|
+
{ locale }: ReviewSetLocaleAction,
|
|
148
|
+
) {
|
|
149
|
+
resource.locale = locale;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
setRating(
|
|
153
|
+
context: RepositoryContext,
|
|
154
|
+
resource: Writable<Review>,
|
|
155
|
+
{ rating }: ReviewSetRatingAction,
|
|
156
|
+
) {
|
|
157
|
+
resource.rating = rating;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
setTarget(
|
|
161
|
+
context: RepositoryContext,
|
|
162
|
+
resource: Writable<Review>,
|
|
163
|
+
{ target }: ReviewSetTargetAction,
|
|
164
|
+
) {
|
|
165
|
+
resource.target = getReferenceFromResourceIdentifier<
|
|
166
|
+
ProductReference | ChannelReference
|
|
167
|
+
>(target, context.projectKey, this._storage);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
setText(
|
|
171
|
+
context: RepositoryContext,
|
|
172
|
+
resource: Writable<Review>,
|
|
173
|
+
{ text }: ReviewSetTextAction,
|
|
174
|
+
) {
|
|
175
|
+
resource.text = text;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
setTitle(
|
|
179
|
+
context: RepositoryContext,
|
|
180
|
+
resource: Writable<Review>,
|
|
181
|
+
{ title }: ReviewSetTitleAction,
|
|
182
|
+
) {
|
|
183
|
+
resource.title = title;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
transitionState(
|
|
187
|
+
context: RepositoryContext,
|
|
188
|
+
resource: Writable<Review>,
|
|
189
|
+
{ state }: ReviewTransitionStateAction,
|
|
190
|
+
) {
|
|
191
|
+
resource.state = getReferenceFromResourceIdentifier<StateReference>(
|
|
192
|
+
state,
|
|
193
|
+
context.projectKey,
|
|
194
|
+
this._storage,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SubscriptionDraft,
|
|
3
|
+
SubscriptionSetKeyAction,
|
|
4
|
+
} from "@commercetools/platform-sdk";
|
|
5
|
+
import { describe, expect, test } from "vitest";
|
|
6
|
+
import type { Config } from "~src/config";
|
|
7
|
+
import { InMemoryStorage } from "~src/storage";
|
|
8
|
+
import { SubscriptionRepository } from "./subscription";
|
|
9
|
+
|
|
10
|
+
describe("Subscription Repository", () => {
|
|
11
|
+
const storage = new InMemoryStorage();
|
|
12
|
+
const config: Config = { storage, strict: false };
|
|
13
|
+
const repository = new SubscriptionRepository(config);
|
|
14
|
+
|
|
15
|
+
test("create subscription with SQS destination", () => {
|
|
16
|
+
const draft: SubscriptionDraft = {
|
|
17
|
+
key: "test-subscription",
|
|
18
|
+
destination: {
|
|
19
|
+
type: "SQS",
|
|
20
|
+
queueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue",
|
|
21
|
+
accessKey: "AKIAIOSFODNN7EXAMPLE",
|
|
22
|
+
accessSecret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
23
|
+
region: "us-east-1",
|
|
24
|
+
},
|
|
25
|
+
changes: [
|
|
26
|
+
{
|
|
27
|
+
resourceTypeId: "order",
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const ctx = { projectKey: "dummy" };
|
|
33
|
+
const result = repository.create(ctx, draft);
|
|
34
|
+
|
|
35
|
+
expect(result.id).toBeDefined();
|
|
36
|
+
expect(result.version).toBe(1);
|
|
37
|
+
expect(result.key).toBe(draft.key);
|
|
38
|
+
expect(result.destination.type).toBe("SQS");
|
|
39
|
+
expect(result.changes).toEqual(draft.changes);
|
|
40
|
+
expect(result.messages).toEqual([]);
|
|
41
|
+
expect(result.format).toEqual({ type: "Platform" });
|
|
42
|
+
expect(result.status).toBe("Healthy");
|
|
43
|
+
expect(result.events).toEqual([]);
|
|
44
|
+
|
|
45
|
+
// Test that the subscription is stored
|
|
46
|
+
const items = repository.query(ctx);
|
|
47
|
+
expect(items.count).toBe(1);
|
|
48
|
+
expect(items.results[0].id).toBe(result.id);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("create subscription with Google Cloud Pub/Sub destination", () => {
|
|
52
|
+
const draft: SubscriptionDraft = {
|
|
53
|
+
key: "pubsub-subscription",
|
|
54
|
+
destination: {
|
|
55
|
+
type: "GoogleCloudPubSub",
|
|
56
|
+
projectId: "my-project",
|
|
57
|
+
topic: "my-topic",
|
|
58
|
+
},
|
|
59
|
+
messages: [
|
|
60
|
+
{
|
|
61
|
+
resourceTypeId: "customer",
|
|
62
|
+
types: ["CustomerCreated", "CustomerEmailVerified"],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
format: {
|
|
66
|
+
type: "CloudEvents",
|
|
67
|
+
cloudEventsVersion: "1.0",
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const ctx = { projectKey: "dummy" };
|
|
72
|
+
const result = repository.create(ctx, draft);
|
|
73
|
+
|
|
74
|
+
expect(result.id).toBeDefined();
|
|
75
|
+
expect(result.key).toBe(draft.key);
|
|
76
|
+
expect(result.destination.type).toBe("GoogleCloudPubSub");
|
|
77
|
+
expect(result.messages).toEqual(draft.messages);
|
|
78
|
+
expect(result.format).toEqual(draft.format);
|
|
79
|
+
expect(result.status).toBe("Healthy");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("create subscription with Azure Event Grid destination", () => {
|
|
83
|
+
const draft: SubscriptionDraft = {
|
|
84
|
+
key: "azure-subscription",
|
|
85
|
+
destination: {
|
|
86
|
+
type: "EventGrid",
|
|
87
|
+
uri: "https://my-topic.westus2-1.eventgrid.azure.net/api/events",
|
|
88
|
+
accessKey: "example-access-key",
|
|
89
|
+
},
|
|
90
|
+
events: [
|
|
91
|
+
{
|
|
92
|
+
resourceTypeId: "product",
|
|
93
|
+
types: ["ProductCreated", "ProductPublished"],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const ctx = { projectKey: "dummy" };
|
|
99
|
+
const result = repository.create(ctx, draft);
|
|
100
|
+
|
|
101
|
+
expect(result.id).toBeDefined();
|
|
102
|
+
expect(result.key).toBe(draft.key);
|
|
103
|
+
expect(result.destination.type).toBe("EventGrid");
|
|
104
|
+
expect(result.events).toEqual(draft.events);
|
|
105
|
+
expect(result.status).toBe("Healthy");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("create subscription with Azure Service Bus destination", () => {
|
|
109
|
+
const draft: SubscriptionDraft = {
|
|
110
|
+
key: "servicebus-subscription",
|
|
111
|
+
destination: {
|
|
112
|
+
type: "AzureServiceBus",
|
|
113
|
+
connectionString:
|
|
114
|
+
"Endpoint=sb://example.servicebus.windows.net/;SharedAccessKeyName=example;SharedAccessKey=example",
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const ctx = { projectKey: "dummy" };
|
|
119
|
+
const result = repository.create(ctx, draft);
|
|
120
|
+
|
|
121
|
+
expect(result.id).toBeDefined();
|
|
122
|
+
expect(result.key).toBe(draft.key);
|
|
123
|
+
expect(result.destination.type).toBe("AzureServiceBus");
|
|
124
|
+
expect(result.status).toBe("Healthy");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("create subscription fails with invalid SQS account ID", () => {
|
|
128
|
+
const draft: SubscriptionDraft = {
|
|
129
|
+
key: "invalid-subscription",
|
|
130
|
+
destination: {
|
|
131
|
+
type: "SQS",
|
|
132
|
+
queueUrl: "https://sqs.us-east-1.amazonaws.com/0000000000/my-queue",
|
|
133
|
+
accessKey: "AKIAIOSFODNN7EXAMPLE",
|
|
134
|
+
accessSecret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
135
|
+
region: "us-east-1",
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const ctx = { projectKey: "dummy" };
|
|
140
|
+
|
|
141
|
+
expect(() => {
|
|
142
|
+
repository.create(ctx, draft);
|
|
143
|
+
}).toThrow("A test message could not be delivered to this destination");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("update subscription - setKey", () => {
|
|
147
|
+
const draft: SubscriptionDraft = {
|
|
148
|
+
key: "test-subscription-update",
|
|
149
|
+
destination: {
|
|
150
|
+
type: "GoogleCloudPubSub",
|
|
151
|
+
projectId: "my-project",
|
|
152
|
+
topic: "my-topic",
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const ctx = { projectKey: "dummy" };
|
|
157
|
+
const subscription = repository.create(ctx, draft);
|
|
158
|
+
|
|
159
|
+
const result = repository.processUpdateActions(
|
|
160
|
+
ctx,
|
|
161
|
+
subscription,
|
|
162
|
+
subscription.version,
|
|
163
|
+
[
|
|
164
|
+
{
|
|
165
|
+
action: "setKey",
|
|
166
|
+
key: "updated-subscription-key",
|
|
167
|
+
} as SubscriptionSetKeyAction,
|
|
168
|
+
],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
expect(result.key).toBe("updated-subscription-key");
|
|
172
|
+
expect(result.version).toBe(subscription.version + 1);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("get and delete subscription", () => {
|
|
176
|
+
const draft: SubscriptionDraft = {
|
|
177
|
+
key: "delete-test",
|
|
178
|
+
destination: {
|
|
179
|
+
type: "GoogleCloudPubSub",
|
|
180
|
+
projectId: "my-project",
|
|
181
|
+
topic: "my-topic",
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const ctx = { projectKey: "dummy" };
|
|
186
|
+
const subscription = repository.create(ctx, draft);
|
|
187
|
+
|
|
188
|
+
// Test get
|
|
189
|
+
const retrieved = repository.get(ctx, subscription.id);
|
|
190
|
+
expect(retrieved).toBeDefined();
|
|
191
|
+
expect(retrieved?.id).toBe(subscription.id);
|
|
192
|
+
|
|
193
|
+
// Test getByKey
|
|
194
|
+
const retrievedByKey = repository.getByKey(ctx, subscription.key!);
|
|
195
|
+
expect(retrievedByKey).toBeDefined();
|
|
196
|
+
expect(retrievedByKey?.key).toBe(subscription.key);
|
|
197
|
+
|
|
198
|
+
// Test delete
|
|
199
|
+
const deleted = repository.delete(ctx, subscription.id);
|
|
200
|
+
expect(deleted).toBeDefined();
|
|
201
|
+
expect(deleted?.id).toBe(subscription.id);
|
|
202
|
+
|
|
203
|
+
// Verify it's deleted
|
|
204
|
+
const notFound = repository.get(ctx, subscription.id);
|
|
205
|
+
expect(notFound).toBeNull();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ZoneAddLocationAction,
|
|
3
|
+
ZoneChangeNameAction,
|
|
4
|
+
ZoneDraft,
|
|
5
|
+
ZoneRemoveLocationAction,
|
|
6
|
+
ZoneSetDescriptionAction,
|
|
7
|
+
ZoneSetKeyAction,
|
|
8
|
+
} from "@commercetools/platform-sdk";
|
|
9
|
+
import { describe, expect, test } from "vitest";
|
|
10
|
+
import type { Config } from "~src/config";
|
|
11
|
+
import { InMemoryStorage } from "~src/storage";
|
|
12
|
+
import { ZoneRepository } from "./zone";
|
|
13
|
+
|
|
14
|
+
describe("Zone Repository", () => {
|
|
15
|
+
const storage = new InMemoryStorage();
|
|
16
|
+
const config: Config = { storage, strict: false };
|
|
17
|
+
const repository = new ZoneRepository(config);
|
|
18
|
+
|
|
19
|
+
test("create zone", () => {
|
|
20
|
+
const draft: ZoneDraft = {
|
|
21
|
+
key: "europe-zone",
|
|
22
|
+
name: "Europe Zone",
|
|
23
|
+
description: "European countries zone",
|
|
24
|
+
locations: [
|
|
25
|
+
{
|
|
26
|
+
country: "DE",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
country: "FR",
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const ctx = { projectKey: "dummy" };
|
|
35
|
+
const result = repository.create(ctx, draft);
|
|
36
|
+
|
|
37
|
+
expect(result.id).toBeDefined();
|
|
38
|
+
expect(result.version).toBe(1);
|
|
39
|
+
expect(result.key).toBe(draft.key);
|
|
40
|
+
expect(result.name).toBe(draft.name);
|
|
41
|
+
expect(result.description).toBe(draft.description);
|
|
42
|
+
expect(result.locations).toEqual(draft.locations);
|
|
43
|
+
|
|
44
|
+
// Test that the zone is stored
|
|
45
|
+
const items = repository.query(ctx);
|
|
46
|
+
expect(items.count).toBe(1);
|
|
47
|
+
expect(items.results[0].id).toBe(result.id);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("create zone without optional fields", () => {
|
|
51
|
+
const draft: ZoneDraft = {
|
|
52
|
+
name: "Simple Zone",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ctx = { projectKey: "dummy" };
|
|
56
|
+
const result = repository.create(ctx, draft);
|
|
57
|
+
|
|
58
|
+
expect(result.id).toBeDefined();
|
|
59
|
+
expect(result.name).toBe(draft.name);
|
|
60
|
+
expect(result.key).toBeUndefined();
|
|
61
|
+
expect(result.description).toBeUndefined();
|
|
62
|
+
expect(result.locations).toEqual([]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("update zone - changeName", () => {
|
|
66
|
+
const draft: ZoneDraft = {
|
|
67
|
+
key: "test-zone",
|
|
68
|
+
name: "Test Zone",
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const ctx = { projectKey: "dummy" };
|
|
72
|
+
const zone = repository.create(ctx, draft);
|
|
73
|
+
|
|
74
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
75
|
+
{
|
|
76
|
+
action: "changeName",
|
|
77
|
+
name: "Updated Test Zone",
|
|
78
|
+
} as ZoneChangeNameAction,
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
expect(result.name).toBe("Updated Test Zone");
|
|
82
|
+
expect(result.version).toBe(zone.version + 1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("update zone - setKey", () => {
|
|
86
|
+
const draft: ZoneDraft = {
|
|
87
|
+
key: "test-zone-2",
|
|
88
|
+
name: "Test Zone 2",
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const ctx = { projectKey: "dummy" };
|
|
92
|
+
const zone = repository.create(ctx, draft);
|
|
93
|
+
|
|
94
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
95
|
+
{
|
|
96
|
+
action: "setKey",
|
|
97
|
+
key: "new-zone-key",
|
|
98
|
+
} as ZoneSetKeyAction,
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
expect(result.key).toBe("new-zone-key");
|
|
102
|
+
expect(result.version).toBe(zone.version + 1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("update zone - setDescription", () => {
|
|
106
|
+
const draft: ZoneDraft = {
|
|
107
|
+
key: "test-zone-3",
|
|
108
|
+
name: "Test Zone 3",
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const ctx = { projectKey: "dummy" };
|
|
112
|
+
const zone = repository.create(ctx, draft);
|
|
113
|
+
|
|
114
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
115
|
+
{
|
|
116
|
+
action: "setDescription",
|
|
117
|
+
description: "New zone description",
|
|
118
|
+
} as ZoneSetDescriptionAction,
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
expect(result.description).toBe("New zone description");
|
|
122
|
+
expect(result.version).toBe(zone.version + 1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("update zone - addLocation", () => {
|
|
126
|
+
const draft: ZoneDraft = {
|
|
127
|
+
key: "test-zone-4",
|
|
128
|
+
name: "Test Zone 4",
|
|
129
|
+
locations: [
|
|
130
|
+
{
|
|
131
|
+
country: "DE",
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const ctx = { projectKey: "dummy" };
|
|
137
|
+
const zone = repository.create(ctx, draft);
|
|
138
|
+
|
|
139
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
140
|
+
{
|
|
141
|
+
action: "addLocation",
|
|
142
|
+
location: {
|
|
143
|
+
country: "FR",
|
|
144
|
+
},
|
|
145
|
+
} as ZoneAddLocationAction,
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
expect(result.locations).toHaveLength(2);
|
|
149
|
+
expect(result.locations).toContainEqual({ country: "DE" });
|
|
150
|
+
expect(result.locations).toContainEqual({ country: "FR" });
|
|
151
|
+
expect(result.version).toBe(zone.version + 1);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("update zone - addLocation with state", () => {
|
|
155
|
+
const draft: ZoneDraft = {
|
|
156
|
+
key: "test-zone-5",
|
|
157
|
+
name: "Test Zone 5",
|
|
158
|
+
locations: [],
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const ctx = { projectKey: "dummy" };
|
|
162
|
+
const zone = repository.create(ctx, draft);
|
|
163
|
+
|
|
164
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
165
|
+
{
|
|
166
|
+
action: "addLocation",
|
|
167
|
+
location: {
|
|
168
|
+
country: "US",
|
|
169
|
+
state: "CA",
|
|
170
|
+
},
|
|
171
|
+
} as ZoneAddLocationAction,
|
|
172
|
+
]);
|
|
173
|
+
|
|
174
|
+
expect(result.locations).toHaveLength(1);
|
|
175
|
+
expect(result.locations[0]).toEqual({ country: "US", state: "CA" });
|
|
176
|
+
expect(result.version).toBe(zone.version + 1);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("update zone - removeLocation", () => {
|
|
180
|
+
const draft: ZoneDraft = {
|
|
181
|
+
key: "test-zone-6",
|
|
182
|
+
name: "Test Zone 6",
|
|
183
|
+
locations: [
|
|
184
|
+
{
|
|
185
|
+
country: "DE",
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
country: "FR",
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
country: "US",
|
|
192
|
+
state: "CA",
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const ctx = { projectKey: "dummy" };
|
|
198
|
+
const zone = repository.create(ctx, draft);
|
|
199
|
+
|
|
200
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
201
|
+
{
|
|
202
|
+
action: "removeLocation",
|
|
203
|
+
location: {
|
|
204
|
+
country: "FR",
|
|
205
|
+
},
|
|
206
|
+
} as ZoneRemoveLocationAction,
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
expect(result.locations).toHaveLength(2);
|
|
210
|
+
expect(result.locations).toContainEqual({ country: "DE" });
|
|
211
|
+
expect(result.locations).toContainEqual({ country: "US", state: "CA" });
|
|
212
|
+
expect(result.locations).not.toContainEqual({ country: "FR" });
|
|
213
|
+
expect(result.version).toBe(zone.version + 1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("update zone - removeLocation with state", () => {
|
|
217
|
+
const draft: ZoneDraft = {
|
|
218
|
+
key: "test-zone-7",
|
|
219
|
+
name: "Test Zone 7",
|
|
220
|
+
locations: [
|
|
221
|
+
{
|
|
222
|
+
country: "US",
|
|
223
|
+
state: "CA",
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
country: "US",
|
|
227
|
+
state: "NY",
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const ctx = { projectKey: "dummy" };
|
|
233
|
+
const zone = repository.create(ctx, draft);
|
|
234
|
+
|
|
235
|
+
const result = repository.processUpdateActions(ctx, zone, zone.version, [
|
|
236
|
+
{
|
|
237
|
+
action: "removeLocation",
|
|
238
|
+
location: {
|
|
239
|
+
country: "US",
|
|
240
|
+
state: "CA",
|
|
241
|
+
},
|
|
242
|
+
} as ZoneRemoveLocationAction,
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
expect(result.locations).toHaveLength(1);
|
|
246
|
+
expect(result.locations[0]).toEqual({ country: "US", state: "NY" });
|
|
247
|
+
expect(result.version).toBe(zone.version + 1);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("get and delete zone", () => {
|
|
251
|
+
const draft: ZoneDraft = {
|
|
252
|
+
key: "delete-test",
|
|
253
|
+
name: "Delete Test Zone",
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const ctx = { projectKey: "dummy" };
|
|
257
|
+
const zone = repository.create(ctx, draft);
|
|
258
|
+
|
|
259
|
+
// Test get
|
|
260
|
+
const retrieved = repository.get(ctx, zone.id);
|
|
261
|
+
expect(retrieved).toBeDefined();
|
|
262
|
+
expect(retrieved?.id).toBe(zone.id);
|
|
263
|
+
|
|
264
|
+
// Test getByKey
|
|
265
|
+
const retrievedByKey = repository.getByKey(ctx, zone.key!);
|
|
266
|
+
expect(retrievedByKey).toBeDefined();
|
|
267
|
+
expect(retrievedByKey?.key).toBe(zone.key);
|
|
268
|
+
|
|
269
|
+
// Test delete
|
|
270
|
+
const deleted = repository.delete(ctx, zone.id);
|
|
271
|
+
expect(deleted).toBeDefined();
|
|
272
|
+
expect(deleted?.id).toBe(zone.id);
|
|
273
|
+
|
|
274
|
+
// Verify it's deleted
|
|
275
|
+
const notFound = repository.get(ctx, zone.id);
|
|
276
|
+
expect(notFound).toBeNull();
|
|
277
|
+
});
|
|
278
|
+
});
|