|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils" |
|
3
|
import { |
|
4
|
beginOrderEditOrderWorkflow, |
|
5
|
confirmOrderEditRequestWorkflow, |
|
6
|
orderEditUpdateItemQuantityWorkflow, |
|
7
|
requestOrderEditRequestWorkflow, |
|
8
|
} from "@medusajs/core-flows" |
|
9
|
|
|
10
|
jest.setTimeout(120000) |
|
11
|
|
|
12
|
// query.graph returns totals as BigNumber instances; coerce for numeric assertions. |
|
13
|
const num = (value) => Number(value) |
|
14
|
|
|
15
|
medusaIntegrationTestRunner({ |
|
16
|
testSuite: ({ getContainer }) => { |
|
17
|
describe("Order totals via Query (query.graph)", () => { |
|
18
|
let query |
|
19
|
let orderModule |
|
20
|
let orderId |
|
21
|
|
|
22
|
// 2 x $10 item + $10 shipping, no taxes/discounts => |
|
23
|
// total 30, subtotal 30, item_subtotal 20, item[0].quantity 2, item[0].total 20 |
|
24
|
const seedOrder = async () => { |
|
25
|
const created = await orderModule.createOrders({ |
|
26
|
currency_code: "usd", |
|
27
|
email: "test@medusajs.com", |
|
28
|
items: [ |
|
29
|
{ |
|
30
|
title: "Test item", |
|
31
|
product_title: "Cool Product", |
|
32
|
thumbnail: "https://example.com/thumb.png", |
|
33
|
quantity: 2, |
|
34
|
unit_price: 10, |
|
35
|
}, |
|
36
|
], |
|
37
|
shipping_methods: [{ name: "Standard", amount: 10 }], |
|
38
|
}) |
|
39
|
return created.id |
|
40
|
} |
|
41
|
|
|
42
|
beforeAll(() => { |
|
43
|
const container = getContainer() |
|
44
|
query = container.resolve(ContainerRegistrationKeys.QUERY) |
|
45
|
orderModule = container.resolve(Modules.ORDER) |
|
46
|
}) |
|
47
|
|
|
48
|
beforeEach(async () => { |
|
49
|
orderId = await seedOrder() |
|
50
|
}) |
|
51
|
|
|
52
|
afterEach(async () => { |
|
53
|
await orderModule.deleteOrders([orderId]) |
|
54
|
}) |
|
55
|
|
|
56
|
it("returns order totals when explicit total fields are requested", async () => { |
|
57
|
const { |
|
58
|
data: [order], |
|
59
|
} = await query.graph({ |
|
60
|
entity: "order", |
|
61
|
fields: [ |
|
62
|
"id", |
|
63
|
"currency_code", |
|
64
|
"total", |
|
65
|
"subtotal", |
|
66
|
"tax_total", |
|
67
|
"shipping_total", |
|
68
|
"item_subtotal", |
|
69
|
"items.*", |
|
70
|
], |
|
71
|
filters: { id: orderId }, |
|
72
|
}) |
|
73
|
|
|
74
|
expect(num(order.total)).toEqual(30) |
|
75
|
expect(num(order.subtotal)).toEqual(30) |
|
76
|
expect(num(order.tax_total)).toEqual(0) |
|
77
|
expect(num(order.shipping_total)).toEqual(10) |
|
78
|
expect(num(order.item_subtotal)).toEqual(20) |
|
79
|
|
|
80
|
expect(num(order.items[0].quantity)).toEqual(2) |
|
81
|
expect(num(order.items[0].unit_price)).toEqual(10) |
|
82
|
expect(num(order.items[0].total)).toEqual(20) |
|
83
|
expect(num(order.items[0].subtotal)).toEqual(20) |
|
84
|
}) |
|
85
|
|
|
86
|
it('returns order totals when fields is a bare wildcard ("*")', async () => { |
|
87
|
const { |
|
88
|
data: [order], |
|
89
|
} = await query.graph({ |
|
90
|
entity: "order", |
|
91
|
fields: ["*"], |
|
92
|
filters: { id: orderId }, |
|
93
|
}) |
|
94
|
|
|
95
|
// A bare "*" must NOT suppress the computed totals. |
|
96
|
expect(num(order.total)).toEqual(30) |
|
97
|
expect(num(order.subtotal)).toEqual(30) |
|
98
|
expect(num(order.item_subtotal)).toEqual(20) |
|
99
|
}) |
|
100
|
|
|
101
|
it('returns order totals when "*" is combined with explicit total fields', async () => { |
|
102
|
const { |
|
103
|
data: [order], |
|
104
|
} = await query.graph({ |
|
105
|
entity: "order", |
|
106
|
fields: ["*", "total", "subtotal"], |
|
107
|
filters: { id: orderId }, |
|
108
|
}) |
|
109
|
|
|
110
|
expect(num(order.total)).toEqual(30) |
|
111
|
expect(num(order.subtotal)).toEqual(30) |
|
112
|
}) |
|
113
|
|
|
114
|
it("computes correct order total when a scoped item field is requested alongside totals", async () => { |
|
115
|
// Regression: requesting `items.title` (a scoped scalar) next to a total |
|
116
|
// used to under-count the order total to shipping-only (10) because the |
|
117
|
// item's `quantity` was not loaded and its total computed as 0. |
|
118
|
const { |
|
119
|
data: [order], |
|
120
|
} = await query.graph({ |
|
121
|
entity: "order", |
|
122
|
fields: [ |
|
123
|
"total", |
|
124
|
"subtotal", |
|
125
|
"item_subtotal", |
|
126
|
"items.title", |
|
127
|
"items.product_title", |
|
128
|
], |
|
129
|
filters: { id: orderId }, |
|
130
|
}) |
|
131
|
|
|
132
|
expect(num(order.total)).toEqual(30) |
|
133
|
expect(num(order.subtotal)).toEqual(30) |
|
134
|
expect(num(order.item_subtotal)).toEqual(20) |
|
135
|
expect(order.items[0].product_title).toEqual("Cool Product") |
|
136
|
}) |
|
137
|
|
|
138
|
it("hydrates item quantity/unit_price and computes item totals with scoped item fields", async () => { |
|
139
|
const { |
|
140
|
data: [order], |
|
141
|
} = await query.graph({ |
|
142
|
entity: "order", |
|
143
|
fields: [ |
|
144
|
"total", |
|
145
|
"item_subtotal", |
|
146
|
"items.quantity", |
|
147
|
"items.unit_price", |
|
148
|
"items.total", |
|
149
|
"items.subtotal", |
|
150
|
], |
|
151
|
filters: { id: orderId }, |
|
152
|
}) |
|
153
|
|
|
154
|
expect(num(order.total)).toEqual(30) |
|
155
|
expect(num(order.item_subtotal)).toEqual(20) |
|
156
|
expect(num(order.items[0].quantity)).toEqual(2) |
|
157
|
expect(num(order.items[0].unit_price)).toEqual(10) |
|
158
|
expect(num(order.items[0].total)).toEqual(20) |
|
159
|
expect(num(order.items[0].subtotal)).toEqual(20) |
|
160
|
}) |
|
161
|
|
|
162
|
it("does not throw when requesting item detail fields alongside a total", async () => { |
|
163
|
// Regression: `items.detail.*` + a total used to throw |
|
164
|
// "Item version is required to load adjustments". |
|
165
|
const { |
|
166
|
data: [order], |
|
167
|
} = await query.graph({ |
|
168
|
entity: "order", |
|
169
|
fields: ["total", "items.detail.quantity"], |
|
170
|
filters: { id: orderId }, |
|
171
|
}) |
|
172
|
|
|
173
|
expect(num(order.total)).toEqual(30) |
|
174
|
expect(num(order.items[0].detail.quantity)).toEqual(2) |
|
175
|
}) |
|
176
|
|
|
177
|
it("returns correct totals for a versioned (order-edited) order", async () => { |
|
178
|
const container = getContainer() |
|
179
|
|
|
180
|
const { |
|
181
|
data: [preEdit], |
|
182
|
} = await query.graph({ |
|
183
|
entity: "order", |
|
184
|
fields: ["items.id"], |
|
185
|
filters: { id: orderId }, |
|
186
|
}) |
|
187
|
const lineItemId = preEdit.items[0].id |
|
188
|
|
|
189
|
// Bump the order to version 2 by changing the item quantity 2 -> 5. |
|
190
|
await beginOrderEditOrderWorkflow(container).run({ |
|
191
|
input: { order_id: orderId }, |
|
192
|
}) |
|
193
|
await orderEditUpdateItemQuantityWorkflow(container).run({ |
|
194
|
input: { |
|
195
|
order_id: orderId, |
|
196
|
items: [{ id: lineItemId, quantity: 5 }], |
|
197
|
}, |
|
198
|
}) |
|
199
|
await requestOrderEditRequestWorkflow(container).run({ |
|
200
|
input: { order_id: orderId }, |
|
201
|
}) |
|
202
|
await confirmOrderEditRequestWorkflow(container).run({ |
|
203
|
input: { order_id: orderId }, |
|
204
|
}) |
|
205
|
|
|
206
|
// 5 x $10 + $10 shipping => total 60, item_subtotal 50, item[0].quantity 5. |
|
207
|
// Use a scoped item field (not `items.*`) so this also guards the fix on a |
|
208
|
// versioned order: previously this under-counted the total to 10. |
|
209
|
const { |
|
210
|
data: [order], |
|
211
|
} = await query.graph({ |
|
212
|
entity: "order", |
|
213
|
fields: [ |
|
214
|
"version", |
|
215
|
"total", |
|
216
|
"subtotal", |
|
217
|
"item_subtotal", |
|
218
|
"items.quantity", |
|
219
|
"items.total", |
|
220
|
], |
|
221
|
filters: { id: orderId }, |
|
222
|
}) |
|
223
|
|
|
224
|
expect(order.version).toEqual(2) |
|
225
|
expect(num(order.total)).toEqual(60) |
|
226
|
expect(num(order.subtotal)).toEqual(60) |
|
227
|
expect(num(order.item_subtotal)).toEqual(50) |
|
228
|
expect(order.items).toHaveLength(1) |
|
229
|
expect(num(order.items[0].quantity)).toEqual(5) |
|
230
|
expect(num(order.items[0].total)).toEqual(50) |
|
231
|
}) |
|
232
|
}) |
|
233
|
}, |
|
234
|
}) |