|
1
|
import { |
|
2
|
calculateShippingOptionsPricesWorkflow, |
|
3
|
listShippingOptionsForCartWithPricingWorkflow, |
|
4
|
} from "@medusajs/core-flows" |
|
5
|
import { StepResponse } from "@medusajs/framework/workflows-sdk" |
|
6
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
7
|
import { |
|
8
|
createAdminUser, |
|
9
|
generatePublishableKey, |
|
10
|
generateStoreHeaders, |
|
11
|
} from "../../../../helpers/create-admin-user" |
|
12
|
import { ProductStatus } from "@medusajs/utils" |
|
13
|
|
|
14
|
jest.setTimeout(50000) |
|
15
|
|
|
16
|
const env = {} |
|
17
|
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } |
|
18
|
|
|
19
|
medusaIntegrationTestRunner({ |
|
20
|
env, |
|
21
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
22
|
describe("Store: Shipping Option API", () => { |
|
23
|
let appContainer |
|
24
|
let salesChannel |
|
25
|
let region |
|
26
|
let regionTwo |
|
27
|
let product |
|
28
|
let stockLocation |
|
29
|
let shippingProfile |
|
30
|
let fulfillmentSet |
|
31
|
let cart |
|
32
|
let shippingOptionCalculated |
|
33
|
let shippingOptionFlat |
|
34
|
let storeHeaders |
|
35
|
|
|
36
|
// Toggled per test so the hooks can be registered once and stay inert for |
|
37
|
// the tests that don't opt into an additional pricing context. |
|
38
|
let calculatedShippingPricingContext: Record<string, unknown> | undefined |
|
39
|
let calculatedShippingPricingContextHookInput: any |
|
40
|
|
|
41
|
beforeAll(async () => { |
|
42
|
appContainer = getContainer() |
|
43
|
|
|
44
|
const hook = (input: any) => { |
|
45
|
calculatedShippingPricingContextHookInput = input |
|
46
|
|
|
47
|
if (!calculatedShippingPricingContext) { |
|
48
|
return |
|
49
|
} |
|
50
|
|
|
51
|
return new StepResponse(calculatedShippingPricingContext) |
|
52
|
} |
|
53
|
|
|
54
|
calculateShippingOptionsPricesWorkflow.hooks.setCalculatedShippingPricingContext( |
|
55
|
hook, |
|
56
|
() => {} |
|
57
|
) |
|
58
|
listShippingOptionsForCartWithPricingWorkflow.hooks.setCalculatedShippingPricingContext( |
|
59
|
hook, |
|
60
|
() => {} |
|
61
|
) |
|
62
|
}) |
|
63
|
|
|
64
|
afterEach(() => { |
|
65
|
calculatedShippingPricingContext = undefined |
|
66
|
calculatedShippingPricingContextHookInput = undefined |
|
67
|
}) |
|
68
|
|
|
69
|
beforeAll(async () => { |
|
70
|
const publishableKey = await generatePublishableKey(appContainer) |
|
71
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
72
|
|
|
73
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
74
|
|
|
75
|
region = ( |
|
76
|
await api.post( |
|
77
|
"/admin/regions", |
|
78
|
{ name: "US", currency_code: "usd", countries: ["US"] }, |
|
79
|
adminHeaders |
|
80
|
) |
|
81
|
).data.region |
|
82
|
|
|
83
|
regionTwo = ( |
|
84
|
await api.post( |
|
85
|
"/admin/regions", |
|
86
|
{ |
|
87
|
name: "Test region two", |
|
88
|
currency_code: "dkk", |
|
89
|
countries: ["DK"], |
|
90
|
is_tax_inclusive: true, |
|
91
|
}, |
|
92
|
adminHeaders |
|
93
|
) |
|
94
|
).data.region |
|
95
|
|
|
96
|
shippingProfile = ( |
|
97
|
await api.post( |
|
98
|
`/admin/shipping-profiles`, |
|
99
|
{ name: "Test", type: "default" }, |
|
100
|
adminHeaders |
|
101
|
) |
|
102
|
).data.shipping_profile |
|
103
|
|
|
104
|
salesChannel = ( |
|
105
|
await api.post( |
|
106
|
"/admin/sales-channels", |
|
107
|
{ name: "first channel", description: "channel" }, |
|
108
|
adminHeaders |
|
109
|
) |
|
110
|
).data.sales_channel |
|
111
|
|
|
112
|
product = ( |
|
113
|
await api.post( |
|
114
|
"/admin/products", |
|
115
|
{ |
|
116
|
title: "Test fixture", |
|
117
|
shipping_profile_id: shippingProfile.id, |
|
118
|
status: ProductStatus.PUBLISHED, |
|
119
|
options: [ |
|
120
|
{ title: "size", values: ["large", "small"] }, |
|
121
|
{ title: "color", values: ["green"] }, |
|
122
|
], |
|
123
|
variants: [ |
|
124
|
{ |
|
125
|
title: "Test variant", |
|
126
|
manage_inventory: false, |
|
127
|
prices: [ |
|
128
|
{ |
|
129
|
currency_code: "usd", |
|
130
|
amount: 100, |
|
131
|
}, |
|
132
|
{ |
|
133
|
currency_code: "dkk", |
|
134
|
amount: 100, |
|
135
|
}, |
|
136
|
], |
|
137
|
options: { |
|
138
|
size: "large", |
|
139
|
color: "green", |
|
140
|
}, |
|
141
|
}, |
|
142
|
], |
|
143
|
}, |
|
144
|
adminHeaders |
|
145
|
) |
|
146
|
).data.product |
|
147
|
|
|
148
|
stockLocation = ( |
|
149
|
await api.post( |
|
150
|
`/admin/stock-locations`, |
|
151
|
{ name: "test location" }, |
|
152
|
adminHeaders |
|
153
|
) |
|
154
|
).data.stock_location |
|
155
|
|
|
156
|
await api.post( |
|
157
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
158
|
{ add: [salesChannel.id] }, |
|
159
|
adminHeaders |
|
160
|
) |
|
161
|
|
|
162
|
const fulfillmentSets = ( |
|
163
|
await api.post( |
|
164
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
165
|
{ |
|
166
|
name: "Test", |
|
167
|
type: "test-type", |
|
168
|
}, |
|
169
|
adminHeaders |
|
170
|
) |
|
171
|
).data.stock_location.fulfillment_sets |
|
172
|
|
|
173
|
fulfillmentSet = ( |
|
174
|
await api.post( |
|
175
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
176
|
{ |
|
177
|
name: "Test", |
|
178
|
geo_zones: [ |
|
179
|
{ type: "country", country_code: "us" }, |
|
180
|
{ type: "country", country_code: "dk" }, |
|
181
|
], |
|
182
|
}, |
|
183
|
adminHeaders |
|
184
|
) |
|
185
|
).data.fulfillment_set |
|
186
|
|
|
187
|
await api.post( |
|
188
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
189
|
{ |
|
190
|
add: [ |
|
191
|
"manual_test-provider", |
|
192
|
"manual-calculated_test-provider-calculated", |
|
193
|
], |
|
194
|
}, |
|
195
|
adminHeaders |
|
196
|
) |
|
197
|
|
|
198
|
shippingOptionCalculated = ( |
|
199
|
await api.post( |
|
200
|
`/admin/shipping-options`, |
|
201
|
{ |
|
202
|
name: "Calculated shipping option", |
|
203
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
204
|
shipping_profile_id: shippingProfile.id, |
|
205
|
provider_id: "manual-calculated_test-provider-calculated", |
|
206
|
price_type: "calculated", |
|
207
|
type: { |
|
208
|
label: "Test type", |
|
209
|
description: "Test description", |
|
210
|
code: "test-code", |
|
211
|
}, |
|
212
|
prices: [], // TODO: Update endpoint validator to not require prices if type is calculated |
|
213
|
rules: [], |
|
214
|
}, |
|
215
|
adminHeaders |
|
216
|
) |
|
217
|
).data.shipping_option |
|
218
|
|
|
219
|
shippingOptionFlat = ( |
|
220
|
await api.post( |
|
221
|
`/admin/shipping-options`, |
|
222
|
{ |
|
223
|
name: "Flat rate shipping option", |
|
224
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
225
|
shipping_profile_id: shippingProfile.id, |
|
226
|
provider_id: "manual_test-provider", |
|
227
|
price_type: "flat", |
|
228
|
type: { |
|
229
|
label: "Test type", |
|
230
|
description: "Test description", |
|
231
|
code: "test-code", |
|
232
|
}, |
|
233
|
prices: [ |
|
234
|
{ |
|
235
|
currency_code: "usd", |
|
236
|
amount: 1000, |
|
237
|
}, |
|
238
|
{ |
|
239
|
region_id: region.id, |
|
240
|
amount: 1100, |
|
241
|
}, |
|
242
|
{ |
|
243
|
region_id: region.id, |
|
244
|
amount: 0, |
|
245
|
rules: [ |
|
246
|
{ |
|
247
|
operator: "gt", |
|
248
|
attribute: "item_total", |
|
249
|
value: 2000, |
|
250
|
}, |
|
251
|
], |
|
252
|
}, |
|
253
|
{ |
|
254
|
region_id: regionTwo.id, |
|
255
|
amount: 500, |
|
256
|
}, |
|
257
|
], |
|
258
|
rules: [], |
|
259
|
}, |
|
260
|
adminHeaders |
|
261
|
) |
|
262
|
).data.shipping_option |
|
263
|
|
|
264
|
await dbUtils.snapshot() |
|
265
|
}) |
|
266
|
|
|
267
|
describe("GET /store/shipping-options?cart_id=", () => { |
|
268
|
it("should get calculated and flat rate shipping options for a cart successfully", async () => { |
|
269
|
cart = ( |
|
270
|
await api.post( |
|
271
|
`/store/carts`, |
|
272
|
{ |
|
273
|
region_id: region.id, |
|
274
|
sales_channel_id: salesChannel.id, |
|
275
|
currency_code: "usd", |
|
276
|
email: "test@admin.com", |
|
277
|
items: [ |
|
278
|
{ |
|
279
|
variant_id: product.variants[0].id, |
|
280
|
quantity: 2, |
|
281
|
}, |
|
282
|
], |
|
283
|
}, |
|
284
|
storeHeaders |
|
285
|
) |
|
286
|
).data.cart |
|
287
|
|
|
288
|
const resp = await api.get( |
|
289
|
`/store/shipping-options?cart_id=${cart.id}`, |
|
290
|
storeHeaders |
|
291
|
) |
|
292
|
|
|
293
|
const shippingOptions = resp.data.shipping_options |
|
294
|
|
|
295
|
expect(shippingOptions).toHaveLength(2) |
|
296
|
expect(shippingOptions).toEqual( |
|
297
|
expect.arrayContaining([ |
|
298
|
expect.objectContaining({ |
|
299
|
id: shippingOptionFlat.id, |
|
300
|
name: "Flat rate shipping option", |
|
301
|
price_type: "flat", |
|
302
|
amount: 1100, |
|
303
|
is_tax_inclusive: false, |
|
304
|
provider_id: "manual_test-provider", |
|
305
|
calculated_price: expect.objectContaining({ |
|
306
|
calculated_amount: 1100, |
|
307
|
is_calculated_price_tax_inclusive: false, |
|
308
|
}), |
|
309
|
}), |
|
310
|
expect.objectContaining({ |
|
311
|
id: shippingOptionCalculated.id, |
|
312
|
name: "Calculated shipping option", |
|
313
|
price_type: "calculated", |
|
314
|
provider_id: "manual-calculated_test-provider-calculated", |
|
315
|
calculated_price: null, |
|
316
|
prices: [], |
|
317
|
// amount doesn't exist for calculated shipping options -> /calculate needs to be called |
|
318
|
}), |
|
319
|
]) |
|
320
|
) |
|
321
|
}) |
|
322
|
|
|
323
|
it("should get fetch pricing for calculated shipping options", async () => { |
|
324
|
cart = ( |
|
325
|
await api.post( |
|
326
|
`/store/carts`, |
|
327
|
{ |
|
328
|
region_id: region.id, |
|
329
|
sales_channel_id: salesChannel.id, |
|
330
|
currency_code: "usd", |
|
331
|
email: "test@admin.com", |
|
332
|
items: [ |
|
333
|
{ |
|
334
|
variant_id: product.variants[0].id, |
|
335
|
quantity: 2, |
|
336
|
}, |
|
337
|
], |
|
338
|
}, |
|
339
|
storeHeaders |
|
340
|
) |
|
341
|
).data.cart |
|
342
|
|
|
343
|
const resp = await api.post( |
|
344
|
`/store/shipping-options/${shippingOptionCalculated.id}/calculate?fields=+provider_id`, |
|
345
|
{ cart_id: cart.id, data: { pin_id: "test" } }, |
|
346
|
storeHeaders |
|
347
|
) |
|
348
|
|
|
349
|
const shippingOption = resp.data.shipping_option |
|
350
|
|
|
351
|
expect(shippingOption).toEqual( |
|
352
|
expect.objectContaining({ |
|
353
|
id: shippingOptionCalculated.id, |
|
354
|
name: "Calculated shipping option", |
|
355
|
price_type: "calculated", |
|
356
|
provider_id: "manual-calculated_test-provider-calculated", |
|
357
|
calculated_price: expect.objectContaining({ |
|
358
|
calculated_amount: 3, |
|
359
|
is_calculated_price_tax_inclusive: false, |
|
360
|
}), |
|
361
|
amount: 3, |
|
362
|
}) |
|
363
|
) |
|
364
|
}) |
|
365
|
|
|
366
|
it("should pass the cart's currency to the provider in both the calculate and the add path", async () => { |
|
367
|
cart = ( |
|
368
|
await api.post( |
|
369
|
`/store/carts`, |
|
370
|
{ |
|
371
|
region_id: regionTwo.id, |
|
372
|
sales_channel_id: salesChannel.id, |
|
373
|
currency_code: "dkk", |
|
374
|
email: "test@admin.com", |
|
375
|
items: [ |
|
376
|
{ |
|
377
|
variant_id: product.variants[0].id, |
|
378
|
quantity: 2, |
|
379
|
}, |
|
380
|
], |
|
381
|
}, |
|
382
|
storeHeaders |
|
383
|
) |
|
384
|
).data.cart |
|
385
|
|
|
386
|
// Display path: POST /store/shipping-options/:id/calculate |
|
387
|
const calculated = ( |
|
388
|
await api.post( |
|
389
|
`/store/shipping-options/${shippingOptionCalculated.id}/calculate`, |
|
390
|
{ cart_id: cart.id, data: { pin_id: "test" } }, |
|
391
|
storeHeaders |
|
392
|
) |
|
393
|
).data.shipping_option |
|
394
|
|
|
395
|
// 2 items * 1.5 * dkk rate of 2 |
|
396
|
expect(calculated.amount).toEqual(6) |
|
397
|
|
|
398
|
// Add path: the price charged must match the price displayed |
|
399
|
const cartWithMethod = ( |
|
400
|
await api.post( |
|
401
|
`/store/carts/${cart.id}/shipping-methods?fields=*shipping_methods`, |
|
402
|
{ |
|
403
|
option_id: shippingOptionCalculated.id, |
|
404
|
data: { pin_id: "test" }, |
|
405
|
}, |
|
406
|
storeHeaders |
|
407
|
) |
|
408
|
).data.cart |
|
409
|
|
|
410
|
expect(cartWithMethod.shipping_methods).toEqual([ |
|
411
|
expect.objectContaining({ |
|
412
|
shipping_option_id: shippingOptionCalculated.id, |
|
413
|
amount: 6, |
|
414
|
}), |
|
415
|
]) |
|
416
|
}) |
|
417
|
|
|
418
|
it("should add shipping method with calculated price to cart", async () => { |
|
419
|
cart = ( |
|
420
|
await api.post( |
|
421
|
`/store/carts`, |
|
422
|
{ |
|
423
|
region_id: region.id, |
|
424
|
sales_channel_id: salesChannel.id, |
|
425
|
currency_code: "usd", |
|
426
|
email: "test@admin.com", |
|
427
|
items: [ |
|
428
|
{ |
|
429
|
variant_id: product.variants[0].id, |
|
430
|
quantity: 2, |
|
431
|
}, |
|
432
|
], |
|
433
|
}, |
|
434
|
storeHeaders |
|
435
|
) |
|
436
|
).data.cart |
|
437
|
|
|
438
|
// Select shipping option and create shipping method |
|
439
|
|
|
440
|
let response = await api.post( |
|
441
|
`/store/carts/${cart.id}/shipping-methods?fields=*shipping_methods`, |
|
442
|
{ |
|
443
|
option_id: shippingOptionCalculated.id, |
|
444
|
data: { pin_id: "test" }, |
|
445
|
}, |
|
446
|
storeHeaders |
|
447
|
) |
|
448
|
|
|
449
|
expect(response.status).toEqual(200) |
|
450
|
expect(response.data.cart).toEqual( |
|
451
|
expect.objectContaining({ |
|
452
|
id: cart.id, |
|
453
|
shipping_methods: expect.arrayContaining([ |
|
454
|
expect.objectContaining({ |
|
455
|
shipping_option_id: shippingOptionCalculated.id, |
|
456
|
amount: 3, |
|
457
|
is_tax_inclusive: false, |
|
458
|
data: { pin_id: "test" }, |
|
459
|
}), |
|
460
|
]), |
|
461
|
shipping_total: 3, |
|
462
|
}) |
|
463
|
) |
|
464
|
|
|
465
|
// Update cart and refresh shipping methods |
|
466
|
|
|
467
|
response = await api.post( |
|
468
|
`/store/carts/${cart.id}/line-items?fields=*shipping_methods`, |
|
469
|
{ |
|
470
|
variant_id: product.variants[0].id, |
|
471
|
quantity: 1, |
|
472
|
}, |
|
473
|
storeHeaders |
|
474
|
) |
|
475
|
|
|
476
|
expect(response.status).toEqual(200) |
|
477
|
expect(response.data.cart).toEqual( |
|
478
|
expect.objectContaining({ |
|
479
|
id: cart.id, |
|
480
|
shipping_methods: expect.arrayContaining([ |
|
481
|
expect.objectContaining({ |
|
482
|
shipping_option_id: shippingOptionCalculated.id, |
|
483
|
amount: 4.5, |
|
484
|
is_tax_inclusive: false, |
|
485
|
data: { pin_id: "test" }, |
|
486
|
}), |
|
487
|
]), |
|
488
|
shipping_subtotal: 4.5, |
|
489
|
}) |
|
490
|
) |
|
491
|
}) |
|
492
|
|
|
493
|
it("should recompute shipping promotion adjustments after shipping price changes due to item update", async () => { |
|
494
|
// Create an automatic promotion targeting shipping_methods |
|
495
|
// 100% off shipping (percentage) when applied |
|
496
|
await api.post( |
|
497
|
`/admin/promotions`, |
|
498
|
{ |
|
499
|
code: "FREE_SHIPPING", |
|
500
|
type: "standard", |
|
501
|
status: "active", |
|
502
|
is_automatic: true, |
|
503
|
application_method: { |
|
504
|
type: "percentage", |
|
505
|
target_type: "shipping_methods", |
|
506
|
allocation: "each", |
|
507
|
value: 100, |
|
508
|
max_quantity: 1, |
|
509
|
currency_code: "usd", |
|
510
|
}, |
|
511
|
}, |
|
512
|
adminHeaders |
|
513
|
) |
|
514
|
|
|
515
|
cart = ( |
|
516
|
await api.post( |
|
517
|
`/store/carts`, |
|
518
|
{ |
|
519
|
region_id: region.id, |
|
520
|
sales_channel_id: salesChannel.id, |
|
521
|
currency_code: "usd", |
|
522
|
email: "test@admin.com", |
|
523
|
items: [ |
|
524
|
{ |
|
525
|
variant_id: product.variants[0].id, |
|
526
|
quantity: 2, |
|
527
|
}, |
|
528
|
], |
|
529
|
}, |
|
530
|
storeHeaders |
|
531
|
) |
|
532
|
).data.cart |
|
533
|
|
|
534
|
// Add calculated shipping method to cart |
|
535
|
// With 2 items (quantity 2), price = 2 * 1.5 = 3 |
|
536
|
let response = await api.post( |
|
537
|
`/store/carts/${cart.id}/shipping-methods`, |
|
538
|
{ |
|
539
|
option_id: shippingOptionCalculated.id, |
|
540
|
data: { pin_id: "test" }, |
|
541
|
}, |
|
542
|
storeHeaders |
|
543
|
) |
|
544
|
|
|
545
|
expect(response.data.cart).toEqual( |
|
546
|
expect.objectContaining({ |
|
547
|
id: cart.id, |
|
548
|
shipping_methods: expect.arrayContaining([ |
|
549
|
expect.objectContaining({ |
|
550
|
shipping_option_id: shippingOptionCalculated.id, |
|
551
|
amount: 3, |
|
552
|
adjustments: expect.arrayContaining([ |
|
553
|
expect.objectContaining({ |
|
554
|
code: "FREE_SHIPPING", |
|
555
|
amount: 3, |
|
556
|
}), |
|
557
|
]), |
|
558
|
}), |
|
559
|
]), |
|
560
|
shipping_total: 0, |
|
561
|
}) |
|
562
|
) |
|
563
|
|
|
564
|
// Now add more items to the cart, which changes item quantity to 3 |
|
565
|
// New shipping price should be 3 * 1.5 = 4.5 |
|
566
|
// The promotion should adjust to 4.5 (100% off the NEW shipping amount) |
|
567
|
response = await api.post( |
|
568
|
`/store/carts/${cart.id}/line-items`, |
|
569
|
{ |
|
570
|
variant_id: product.variants[0].id, |
|
571
|
quantity: 1, |
|
572
|
}, |
|
573
|
storeHeaders |
|
574
|
) |
|
575
|
|
|
576
|
expect(response.data.cart).toEqual( |
|
577
|
expect.objectContaining({ |
|
578
|
id: cart.id, |
|
579
|
shipping_methods: expect.arrayContaining([ |
|
580
|
expect.objectContaining({ |
|
581
|
shipping_option_id: shippingOptionCalculated.id, |
|
582
|
amount: 4.5, |
|
583
|
adjustments: expect.arrayContaining([ |
|
584
|
expect.objectContaining({ |
|
585
|
code: "FREE_SHIPPING", |
|
586
|
amount: 4.5, // Must match the NEW shipping amount, not the old one (3) |
|
587
|
}), |
|
588
|
]), |
|
589
|
}), |
|
590
|
]), |
|
591
|
shipping_total: 0, // Still fully discounted |
|
592
|
}) |
|
593
|
) |
|
594
|
}) |
|
595
|
}) |
|
596
|
|
|
597
|
describe("with cart spanning multiple shipping profiles", () => { |
|
598
|
let shippingProfileB |
|
599
|
let productB |
|
600
|
let stockLocationB |
|
601
|
let fulfillmentSetB |
|
602
|
let shippingOptionCalculatedB |
|
603
|
|
|
604
|
beforeEach(async () => { |
|
605
|
shippingProfileB = ( |
|
606
|
await api.post( |
|
607
|
`/admin/shipping-profiles`, |
|
608
|
{ name: "Profile B", type: "default" }, |
|
609
|
adminHeaders |
|
610
|
) |
|
611
|
).data.shipping_profile |
|
612
|
|
|
613
|
productB = ( |
|
614
|
await api.post( |
|
615
|
"/admin/products", |
|
616
|
{ |
|
617
|
title: "Test fixture B", |
|
618
|
shipping_profile_id: shippingProfileB.id, |
|
619
|
status: ProductStatus.PUBLISHED, |
|
620
|
options: [{ title: "size", values: ["large"] }], |
|
621
|
variants: [ |
|
622
|
{ |
|
623
|
title: "Variant B", |
|
624
|
manage_inventory: false, |
|
625
|
prices: [{ currency_code: "usd", amount: 100 }], |
|
626
|
options: { size: "large" }, |
|
627
|
}, |
|
628
|
], |
|
629
|
}, |
|
630
|
adminHeaders |
|
631
|
) |
|
632
|
).data.product |
|
633
|
|
|
634
|
stockLocationB = ( |
|
635
|
await api.post( |
|
636
|
`/admin/stock-locations`, |
|
637
|
{ name: "test location B" }, |
|
638
|
adminHeaders |
|
639
|
) |
|
640
|
).data.stock_location |
|
641
|
|
|
642
|
await api.post( |
|
643
|
`/admin/stock-locations/${stockLocationB.id}/sales-channels`, |
|
644
|
{ add: [salesChannel.id] }, |
|
645
|
adminHeaders |
|
646
|
) |
|
647
|
|
|
648
|
const fulfillmentSetsB = ( |
|
649
|
await api.post( |
|
650
|
`/admin/stock-locations/${stockLocationB.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
651
|
{ name: "Test B", type: "test-type" }, |
|
652
|
adminHeaders |
|
653
|
) |
|
654
|
).data.stock_location.fulfillment_sets |
|
655
|
|
|
656
|
fulfillmentSetB = ( |
|
657
|
await api.post( |
|
658
|
`/admin/fulfillment-sets/${fulfillmentSetsB[0].id}/service-zones`, |
|
659
|
{ |
|
660
|
name: "Test B", |
|
661
|
geo_zones: [{ type: "country", country_code: "us" }], |
|
662
|
}, |
|
663
|
adminHeaders |
|
664
|
) |
|
665
|
).data.fulfillment_set |
|
666
|
|
|
667
|
await api.post( |
|
668
|
`/admin/stock-locations/${stockLocationB.id}/fulfillment-providers`, |
|
669
|
{ add: ["manual-calculated_test-provider-calculated"] }, |
|
670
|
adminHeaders |
|
671
|
) |
|
672
|
|
|
673
|
shippingOptionCalculatedB = ( |
|
674
|
await api.post( |
|
675
|
`/admin/shipping-options`, |
|
676
|
{ |
|
677
|
name: "Calculated shipping option B", |
|
678
|
service_zone_id: fulfillmentSetB.service_zones[0].id, |
|
679
|
shipping_profile_id: shippingProfileB.id, |
|
680
|
provider_id: "manual-calculated_test-provider-calculated", |
|
681
|
price_type: "calculated", |
|
682
|
type: { |
|
683
|
label: "Test type", |
|
684
|
description: "Test description", |
|
685
|
code: "test-code", |
|
686
|
}, |
|
687
|
prices: [], |
|
688
|
rules: [], |
|
689
|
}, |
|
690
|
adminHeaders |
|
691
|
) |
|
692
|
).data.shipping_option |
|
693
|
}) |
|
694
|
|
|
695
|
it("POST /store/carts/:id/shipping-methods prices each calculated option from only its own location's items", async () => { |
|
696
|
cart = ( |
|
697
|
await api.post( |
|
698
|
`/store/carts`, |
|
699
|
{ |
|
700
|
region_id: region.id, |
|
701
|
sales_channel_id: salesChannel.id, |
|
702
|
currency_code: "usd", |
|
703
|
email: "test@admin.com", |
|
704
|
shipping_address: { country_code: "us" }, |
|
705
|
items: [ |
|
706
|
{ variant_id: product.variants[0].id, quantity: 2 }, |
|
707
|
{ variant_id: productB.variants[0].id, quantity: 1 }, |
|
708
|
], |
|
709
|
}, |
|
710
|
storeHeaders |
|
711
|
) |
|
712
|
).data.cart |
|
713
|
|
|
714
|
// Methods for different shipping profiles stack on the cart. |
|
715
|
// Each calculated provider call sees only the |
|
716
|
// items for that option's profile/location: |
|
717
|
// optionA: 2 * 1.5 = 3 optionB: 1 * 1.5 = 1.5 |
|
718
|
const respA = await api.post( |
|
719
|
`/store/carts/${cart.id}/shipping-methods?fields=*shipping_methods`, |
|
720
|
{ option_id: shippingOptionCalculated.id, data: {} }, |
|
721
|
storeHeaders |
|
722
|
) |
|
723
|
expect(respA.data.cart.shipping_methods).toHaveLength(1) |
|
724
|
const methodA = respA.data.cart.shipping_methods.find( |
|
725
|
(m) => m.shipping_option_id === shippingOptionCalculated.id |
|
726
|
) |
|
727
|
expect(methodA).toBeDefined() |
|
728
|
expect(methodA.amount).toBe(3) |
|
729
|
|
|
730
|
const respB = await api.post( |
|
731
|
`/store/carts/${cart.id}/shipping-methods?fields=*shipping_methods`, |
|
732
|
{ option_id: shippingOptionCalculatedB.id, data: {} }, |
|
733
|
storeHeaders |
|
734
|
) |
|
735
|
expect(respB.data.cart.shipping_methods).toHaveLength(2) |
|
736
|
const methodB = respB.data.cart.shipping_methods.find( |
|
737
|
(m) => m.shipping_option_id === shippingOptionCalculatedB.id |
|
738
|
) |
|
739
|
expect(methodB).toBeDefined() |
|
740
|
expect(methodB.amount).toBe(1.5) |
|
741
|
|
|
742
|
// Method A stacks alongside method B and keeps its profile-scoped |
|
743
|
// price after the refresh that follows adding method B |
|
744
|
const methodAAfterB = respB.data.cart.shipping_methods.find( |
|
745
|
(m) => m.shipping_option_id === shippingOptionCalculated.id |
|
746
|
) |
|
747
|
expect(methodAAfterB).toBeDefined() |
|
748
|
expect(methodAAfterB.amount).toBe(3) |
|
749
|
expect(respB.data.cart.shipping_total).toBe(4.5) |
|
750
|
}) |
|
751
|
|
|
752
|
it("POST /store/carts/:id/shipping-methods prices each option from its own profile's items when added in one call", async () => { |
|
753
|
cart = ( |
|
754
|
await api.post( |
|
755
|
`/store/carts`, |
|
756
|
{ |
|
757
|
region_id: region.id, |
|
758
|
sales_channel_id: salesChannel.id, |
|
759
|
currency_code: "usd", |
|
760
|
email: "test@admin.com", |
|
761
|
shipping_address: { country_code: "us" }, |
|
762
|
items: [ |
|
763
|
{ variant_id: product.variants[0].id, quantity: 2 }, |
|
764
|
{ variant_id: productB.variants[0].id, quantity: 1 }, |
|
765
|
], |
|
766
|
}, |
|
767
|
storeHeaders |
|
768
|
) |
|
769
|
).data.cart |
|
770
|
|
|
771
|
const response = await api.post( |
|
772
|
`/store/carts/${cart.id}/shipping-methods?fields=*shipping_methods`, |
|
773
|
[ |
|
774
|
{ option_id: shippingOptionCalculated.id, data: {} }, |
|
775
|
{ option_id: shippingOptionCalculatedB.id, data: {} }, |
|
776
|
], |
|
777
|
storeHeaders |
|
778
|
) |
|
779
|
|
|
780
|
expect(response.data.cart.shipping_methods).toHaveLength(2) |
|
781
|
expect(response.data.cart.shipping_methods).toEqual( |
|
782
|
expect.arrayContaining([ |
|
783
|
expect.objectContaining({ |
|
784
|
shipping_option_id: shippingOptionCalculated.id, |
|
785
|
amount: 3, |
|
786
|
}), |
|
787
|
expect.objectContaining({ |
|
788
|
shipping_option_id: shippingOptionCalculatedB.id, |
|
789
|
amount: 1.5, |
|
790
|
}), |
|
791
|
]) |
|
792
|
) |
|
793
|
expect(response.data.cart.shipping_total).toBe(4.5) |
|
794
|
}) |
|
795
|
|
|
796
|
it("POST /store/shipping-options/:id/calculate prices each option from only its own location's items", async () => { |
|
797
|
cart = ( |
|
798
|
await api.post( |
|
799
|
`/store/carts`, |
|
800
|
{ |
|
801
|
region_id: region.id, |
|
802
|
sales_channel_id: salesChannel.id, |
|
803
|
currency_code: "usd", |
|
804
|
email: "test@admin.com", |
|
805
|
shipping_address: { country_code: "us" }, |
|
806
|
items: [ |
|
807
|
{ variant_id: product.variants[0].id, quantity: 2 }, |
|
808
|
{ variant_id: productB.variants[0].id, quantity: 1 }, |
|
809
|
], |
|
810
|
}, |
|
811
|
storeHeaders |
|
812
|
) |
|
813
|
).data.cart |
|
814
|
|
|
815
|
const respA = await api.post( |
|
816
|
`/store/shipping-options/${shippingOptionCalculated.id}/calculate`, |
|
817
|
{ cart_id: cart.id, data: {} }, |
|
818
|
storeHeaders |
|
819
|
) |
|
820
|
const respB = await api.post( |
|
821
|
`/store/shipping-options/${shippingOptionCalculatedB.id}/calculate`, |
|
822
|
{ cart_id: cart.id, data: {} }, |
|
823
|
storeHeaders |
|
824
|
) |
|
825
|
|
|
826
|
// Same arithmetic as the GET case, via the single-option endpoint. |
|
827
|
expect(respA.data.shipping_option.amount).toBe(3) |
|
828
|
expect(respB.data.shipping_option.amount).toBe(1.5) |
|
829
|
}) |
|
830
|
}) |
|
831
|
describe("setCalculatedShippingPricingContext hook", () => { |
|
832
|
const createCart = async () => |
|
833
|
( |
|
834
|
await api.post( |
|
835
|
`/store/carts`, |
|
836
|
{ |
|
837
|
region_id: region.id, |
|
838
|
sales_channel_id: salesChannel.id, |
|
839
|
currency_code: "usd", |
|
840
|
email: "test@admin.com", |
|
841
|
items: [{ variant_id: product.variants[0].id, quantity: 2 }], |
|
842
|
}, |
|
843
|
storeHeaders |
|
844
|
) |
|
845
|
).data.cart |
|
846
|
|
|
847
|
it("forwards the hook result to the provider on the calculate route", async () => { |
|
848
|
calculatedShippingPricingContext = { price_multiplier: 3 } |
|
849
|
|
|
850
|
const cartForHook = await createCart() |
|
851
|
|
|
852
|
const shippingOption = ( |
|
853
|
await api.post( |
|
854
|
`/store/shipping-options/${shippingOptionCalculated.id}/calculate`, |
|
855
|
{ cart_id: cartForHook.id, data: { pin_id: "test" } }, |
|
856
|
storeHeaders |
|
857
|
) |
|
858
|
).data.shipping_option |
|
859
|
|
|
860
|
// 2 items * 1.5, tripled by the additional context |
|
861
|
expect(shippingOption.amount).toEqual(9) |
|
862
|
expect(calculatedShippingPricingContextHookInput).toEqual( |
|
863
|
expect.objectContaining({ |
|
864
|
input: expect.objectContaining({ cart_id: cartForHook.id }), |
|
865
|
}) |
|
866
|
) |
|
867
|
}) |
|
868
|
|
|
869
|
it("forwards the hook result to the provider when adding the method to a cart", async () => { |
|
870
|
calculatedShippingPricingContext = { price_multiplier: 4 } |
|
871
|
|
|
872
|
const cartForHook = await createCart() |
|
873
|
|
|
874
|
const cartWithMethod = ( |
|
875
|
await api.post( |
|
876
|
`/store/carts/${cartForHook.id}/shipping-methods?fields=*shipping_methods`, |
|
877
|
{ |
|
878
|
option_id: shippingOptionCalculated.id, |
|
879
|
data: { pin_id: "test" }, |
|
880
|
}, |
|
881
|
storeHeaders |
|
882
|
) |
|
883
|
).data.cart |
|
884
|
|
|
885
|
// 2 items * 1.5, quadrupled by the additional context |
|
886
|
expect(cartWithMethod.shipping_methods).toEqual([ |
|
887
|
expect.objectContaining({ |
|
888
|
shipping_option_id: shippingOptionCalculated.id, |
|
889
|
amount: 12, |
|
890
|
}), |
|
891
|
]) |
|
892
|
expect(calculatedShippingPricingContextHookInput).toEqual( |
|
893
|
expect.objectContaining({ |
|
894
|
input: expect.objectContaining({ cart_id: cartForHook.id }), |
|
895
|
}) |
|
896
|
) |
|
897
|
}) |
|
898
|
|
|
899
|
it("leaves the price untouched when the hook returns nothing", async () => { |
|
900
|
const cartForHook = await createCart() |
|
901
|
|
|
902
|
const shippingOption = ( |
|
903
|
await api.post( |
|
904
|
`/store/shipping-options/${shippingOptionCalculated.id}/calculate`, |
|
905
|
{ cart_id: cartForHook.id, data: { pin_id: "test" } }, |
|
906
|
storeHeaders |
|
907
|
) |
|
908
|
).data.shipping_option |
|
909
|
|
|
910
|
expect(shippingOption.amount).toEqual(3) |
|
911
|
}) |
|
912
|
}) |
|
913
|
}) |
|
914
|
}, |
|
915
|
}) |