|
1
|
import { IPricingModuleService } from "@medusajs/framework/types" |
|
2
|
import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { Price } from "../../../../src/models" |
|
5
|
import { createPrices } from "../../../__fixtures__/price" |
|
6
|
import { createPriceRules } from "../../../__fixtures__/price-rule" |
|
7
|
import { createPriceSets } from "../../../__fixtures__/price-set" |
|
8
|
import { Modules, toMikroORMEntity } from "@medusajs/framework/utils" |
|
9
|
|
|
10
|
jest.setTimeout(30000) |
|
11
|
|
|
12
|
moduleIntegrationTestRunner<IPricingModuleService>({ |
|
13
|
moduleName: Modules.PRICING, |
|
14
|
testSuite: ({ MikroOrmWrapper, service }) => { |
|
15
|
describe("PricingModule Service - PriceRule", () => { |
|
16
|
let testManager: SqlEntityManager |
|
17
|
beforeEach(async () => { |
|
18
|
testManager = await MikroOrmWrapper.forkManager() |
|
19
|
|
|
20
|
await createPriceSets(testManager) |
|
21
|
await createPrices(testManager) |
|
22
|
await createPriceRules(testManager) |
|
23
|
}) |
|
24
|
|
|
25
|
describe("list", () => { |
|
26
|
it("should list priceRules", async () => { |
|
27
|
const PriceRulesResult = await service.listPriceRules() |
|
28
|
const serialized = JSON.parse(JSON.stringify(PriceRulesResult)) |
|
29
|
|
|
30
|
expect(serialized).toEqual([ |
|
31
|
expect.objectContaining({ |
|
32
|
id: "price-rule-1", |
|
33
|
}), |
|
34
|
expect.objectContaining({ |
|
35
|
id: "price-rule-2", |
|
36
|
}), |
|
37
|
]) |
|
38
|
}) |
|
39
|
|
|
40
|
it("should list priceRules by id", async () => { |
|
41
|
const priceRuleResult = await service.listPriceRules({ |
|
42
|
id: ["price-rule-1"], |
|
43
|
}) |
|
44
|
|
|
45
|
expect(priceRuleResult).toEqual([ |
|
46
|
expect.objectContaining({ |
|
47
|
id: "price-rule-1", |
|
48
|
}), |
|
49
|
]) |
|
50
|
}) |
|
51
|
|
|
52
|
it("should list priceRules with relations and selects", async () => { |
|
53
|
const priceRulesResult = await service.listPriceRules( |
|
54
|
{ |
|
55
|
id: ["price-rule-1"], |
|
56
|
}, |
|
57
|
{ |
|
58
|
select: ["id", "price.id"], |
|
59
|
relations: ["price"], |
|
60
|
} |
|
61
|
) |
|
62
|
|
|
63
|
const serialized = JSON.parse(JSON.stringify(priceRulesResult)) |
|
64
|
|
|
65
|
expect(serialized).toEqual([ |
|
66
|
{ |
|
67
|
id: "price-rule-1", |
|
68
|
price: { |
|
69
|
id: "price-set-money-amount-USD", |
|
70
|
}, |
|
71
|
price_id: "price-set-money-amount-USD", |
|
72
|
}, |
|
73
|
]) |
|
74
|
}) |
|
75
|
|
|
76
|
describe("listAndCount", () => { |
|
77
|
it("should return priceRules and count", async () => { |
|
78
|
const [priceRulesResult, count] = |
|
79
|
await service.listAndCountPriceRules() |
|
80
|
|
|
81
|
expect(count).toEqual(2) |
|
82
|
expect(priceRulesResult).toEqual([ |
|
83
|
expect.objectContaining({ |
|
84
|
id: "price-rule-1", |
|
85
|
}), |
|
86
|
expect.objectContaining({ |
|
87
|
id: "price-rule-2", |
|
88
|
}), |
|
89
|
]) |
|
90
|
}) |
|
91
|
|
|
92
|
it("should return priceRules and count when filtered", async () => { |
|
93
|
const [priceRulesResult, count] = |
|
94
|
await service.listAndCountPriceRules({ |
|
95
|
id: ["price-rule-1"], |
|
96
|
}) |
|
97
|
|
|
98
|
expect(count).toEqual(1) |
|
99
|
expect(priceRulesResult).toEqual([ |
|
100
|
expect.objectContaining({ |
|
101
|
id: "price-rule-1", |
|
102
|
}), |
|
103
|
]) |
|
104
|
}) |
|
105
|
|
|
106
|
it("should list PriceRules with relations and selects", async () => { |
|
107
|
const [PriceRulesResult, count] = |
|
108
|
await service.listAndCountPriceRules( |
|
109
|
{ |
|
110
|
id: ["price-rule-1"], |
|
111
|
}, |
|
112
|
{ |
|
113
|
select: ["id", "price.id"], |
|
114
|
relations: ["price"], |
|
115
|
} |
|
116
|
) |
|
117
|
|
|
118
|
const serialized = JSON.parse(JSON.stringify(PriceRulesResult)) |
|
119
|
|
|
120
|
expect(count).toEqual(1) |
|
121
|
expect(serialized).toEqual([ |
|
122
|
{ |
|
123
|
id: "price-rule-1", |
|
124
|
price: { |
|
125
|
id: "price-set-money-amount-USD", |
|
126
|
}, |
|
127
|
price_id: "price-set-money-amount-USD", |
|
128
|
}, |
|
129
|
]) |
|
130
|
}) |
|
131
|
|
|
132
|
it("should return PriceRules and count when using skip and take", async () => { |
|
133
|
const [PriceRulesResult, count] = |
|
134
|
await service.listAndCountPriceRules({}, { skip: 1, take: 1 }) |
|
135
|
|
|
136
|
expect(count).toEqual(2) |
|
137
|
expect(PriceRulesResult).toEqual([ |
|
138
|
expect.objectContaining({ |
|
139
|
id: "price-rule-2", |
|
140
|
}), |
|
141
|
]) |
|
142
|
}) |
|
143
|
|
|
144
|
it("should return requested fields", async () => { |
|
145
|
const [PriceRulesResult, count] = |
|
146
|
await service.listAndCountPriceRules( |
|
147
|
{}, |
|
148
|
{ |
|
149
|
take: 1, |
|
150
|
select: ["id"], |
|
151
|
} |
|
152
|
) |
|
153
|
|
|
154
|
const serialized = JSON.parse(JSON.stringify(PriceRulesResult)) |
|
155
|
|
|
156
|
expect(count).toEqual(2) |
|
157
|
expect(serialized).toEqual([ |
|
158
|
{ |
|
159
|
id: "price-rule-1", |
|
160
|
}, |
|
161
|
]) |
|
162
|
}) |
|
163
|
}) |
|
164
|
|
|
165
|
describe("retrieve", () => { |
|
166
|
const id = "price-rule-1" |
|
167
|
|
|
168
|
it("should return PriceRule for the given id", async () => { |
|
169
|
const PriceRule = await service.retrievePriceRule(id) |
|
170
|
|
|
171
|
expect(PriceRule).toEqual( |
|
172
|
expect.objectContaining({ |
|
173
|
id, |
|
174
|
}) |
|
175
|
) |
|
176
|
}) |
|
177
|
|
|
178
|
it("should throw an error when PriceRule with id does not exist", async () => { |
|
179
|
let error |
|
180
|
|
|
181
|
try { |
|
182
|
await service.retrievePriceRule("does-not-exist") |
|
183
|
} catch (e) { |
|
184
|
error = e |
|
185
|
} |
|
186
|
|
|
187
|
expect(error.message).toEqual( |
|
188
|
"PriceRule with id: does-not-exist was not found" |
|
189
|
) |
|
190
|
}) |
|
191
|
|
|
192
|
it("should throw an error when a id is not provided", async () => { |
|
193
|
let error |
|
194
|
|
|
195
|
try { |
|
196
|
await service.retrievePriceRule(undefined as unknown as string) |
|
197
|
} catch (e) { |
|
198
|
error = e |
|
199
|
} |
|
200
|
|
|
201
|
expect(error.message).toEqual("priceRule - id must be defined") |
|
202
|
}) |
|
203
|
|
|
204
|
it("should return PriceRule based on config select param", async () => { |
|
205
|
const PriceRule = await service.retrievePriceRule(id, { |
|
206
|
select: ["id"], |
|
207
|
}) |
|
208
|
|
|
209
|
const serialized = JSON.parse(JSON.stringify(PriceRule)) |
|
210
|
|
|
211
|
expect(serialized).toEqual({ |
|
212
|
id, |
|
213
|
}) |
|
214
|
}) |
|
215
|
}) |
|
216
|
|
|
217
|
describe("delete", () => { |
|
218
|
const id = "price-set-1" |
|
219
|
|
|
220
|
it("should delete the PriceRules given an id successfully", async () => { |
|
221
|
await service.deletePriceRules([id]) |
|
222
|
|
|
223
|
const PriceRules = await service.listPriceRules({ |
|
224
|
id: [id], |
|
225
|
}) |
|
226
|
|
|
227
|
expect(PriceRules).toHaveLength(0) |
|
228
|
}) |
|
229
|
}) |
|
230
|
|
|
231
|
describe("update", () => { |
|
232
|
const id = "price-set-1" |
|
233
|
|
|
234
|
it("should throw an error when a id does not exist", async () => { |
|
235
|
let error |
|
236
|
|
|
237
|
try { |
|
238
|
await service.updatePriceRules([ |
|
239
|
{ |
|
240
|
id: "does-not-exist", |
|
241
|
}, |
|
242
|
]) |
|
243
|
} catch (e) { |
|
244
|
error = e |
|
245
|
} |
|
246
|
|
|
247
|
expect(error.message).toEqual( |
|
248
|
'PriceRule with id "does-not-exist" not found' |
|
249
|
) |
|
250
|
}) |
|
251
|
}) |
|
252
|
|
|
253
|
describe("create", () => { |
|
254
|
it("should throw an error when a id does not exist", async () => { |
|
255
|
let error |
|
256
|
|
|
257
|
try { |
|
258
|
await service.updatePriceRules([ |
|
259
|
{ |
|
260
|
random: "does-not-exist", |
|
261
|
} as any, |
|
262
|
]) |
|
263
|
} catch (e) { |
|
264
|
error = e |
|
265
|
} |
|
266
|
|
|
267
|
expect(error.message).toEqual('PriceRule with id "" not found') |
|
268
|
}) |
|
269
|
|
|
270
|
it("should create a PriceRule successfully", async () => { |
|
271
|
const price = testManager.create(toMikroORMEntity(Price), { |
|
272
|
currency_code: "EUR", |
|
273
|
amount: 100, |
|
274
|
price_set_id: "price-set-1", |
|
275
|
title: "test", |
|
276
|
rules_count: 0, |
|
277
|
}) |
|
278
|
|
|
279
|
await testManager.persist(price).flush() |
|
280
|
|
|
281
|
await service.createPriceRules({ |
|
282
|
id: "price-rule-new", |
|
283
|
price_set_id: "price-set-1", |
|
284
|
attribute: "region_id", |
|
285
|
value: "region_1", |
|
286
|
price_list_id: "test", |
|
287
|
price_id: price.id, |
|
288
|
}) |
|
289
|
|
|
290
|
const [priceRule] = await service.listPriceRules({ |
|
291
|
id: ["price-rule-new"], |
|
292
|
}) |
|
293
|
|
|
294
|
expect(priceRule).toEqual( |
|
295
|
expect.objectContaining({ |
|
296
|
id: "price-rule-new", |
|
297
|
}) |
|
298
|
) |
|
299
|
}) |
|
300
|
}) |
|
301
|
}) |
|
302
|
}) |
|
303
|
}, |
|
304
|
}) |