feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context (#10374)
* feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context * chore: add test for shipping options returning free shipping
This commit is contained in:
1 parent
eacd691951
commit
11bd556133
12 files changed
+284
-156
No files matched your search
@@ -152,12 +152,12 @@ medusaIntegrationTestRunner({
|
||||
amount: 500,
|
||||
rules: [
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gte",
|
||||
value: 100,
|
||||
},
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "lte",
|
||||
value: 200,
|
||||
},
|
||||
@@ -218,12 +218,12 @@ medusaIntegrationTestRunner({
|
||||
rules_count: 2,
|
||||
price_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gte",
|
||||
value: "100",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "lte",
|
||||
value: "200",
|
||||
}),
|
||||
@@ -327,7 +327,7 @@ medusaIntegrationTestRunner({
|
||||
amount: 500,
|
||||
rules: [
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gt",
|
||||
value: 200,
|
||||
},
|
||||
@@ -378,7 +378,7 @@ medusaIntegrationTestRunner({
|
||||
rules_count: 2,
|
||||
price_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gt",
|
||||
value: "200",
|
||||
}),
|
||||
@@ -458,7 +458,7 @@ medusaIntegrationTestRunner({
|
||||
amount: 500,
|
||||
rules: [
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "not_whitelisted",
|
||||
value: 100,
|
||||
},
|
||||
@@ -496,7 +496,7 @@ medusaIntegrationTestRunner({
|
||||
amount: 500,
|
||||
rules: [
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gt",
|
||||
value: "string",
|
||||
},
|
||||
@@ -626,7 +626,7 @@ medusaIntegrationTestRunner({
|
||||
amount: 5,
|
||||
rules: [
|
||||
{
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gt",
|
||||
value: 200,
|
||||
},
|
||||
@@ -702,7 +702,7 @@ medusaIntegrationTestRunner({
|
||||
amount: 5,
|
||||
price_rules: [
|
||||
expect.objectContaining({
|
||||
attribute: "cart_total",
|
||||
attribute: "total",
|
||||
operator: "gt",
|
||||
value: "200",
|
||||
}),
|
||||
|
||||
+88
@@ -190,6 +190,17 @@ medusaIntegrationTestRunner({
|
||||
region_id: region.id,
|
||||
amount: 1100,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 0,
|
||||
rules: [
|
||||
{
|
||||
operator: "gt",
|
||||
attribute: "total",
|
||||
value: 2000,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
region_id: regionTwo.id,
|
||||
amount: 500,
|
||||
@@ -266,6 +277,83 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return prices based on cart total", async () => {
|
||||
cart = (
|
||||
await api.post(
|
||||
`/store/carts`,
|
||||
{
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
email: "test@admin.com",
|
||||
items: [
|
||||
{
|
||||
variant_id: product.variants[0].id,
|
||||
// Adding a quantity of 100 to emulate total being greater than 2000
|
||||
quantity: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
const resp = await api.get(
|
||||
`/store/shipping-options?cart_id=${cart.id}`,
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
const shippingOptions = resp.data.shipping_options
|
||||
|
||||
expect(shippingOptions).toHaveLength(1)
|
||||
expect(shippingOptions[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: shippingOption.id,
|
||||
name: "Test shipping option",
|
||||
// Free shipping due to cart total being greater than 2000
|
||||
amount: 0,
|
||||
price_type: "flat",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw when required fields of a cart are not present", async () => {
|
||||
cart = (
|
||||
await api.post(
|
||||
`/store/carts`,
|
||||
{
|
||||
region_id: region.id,
|
||||
currency_code: "usd",
|
||||
sales_channel_id: null,
|
||||
email: "test@admin.com",
|
||||
items: [],
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
const { response } = await api
|
||||
.get(`/store/shipping-options?cart_id=${cart.id}`, storeHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message:
|
||||
"Field(s) are required to have value to continue - sales_channel_id",
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw error when cart_id is not passed as a parameter", async () => {
|
||||
const { response } = await api
|
||||
.get(`/store/shipping-options`, storeHeaders)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: "Invalid request: Field 'cart_id' is required",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
updatePaymentCollectionStepId,
|
||||
updateTaxLinesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
ICartModuleService,
|
||||
ICustomerModuleService,
|
||||
@@ -30,7 +31,6 @@ import {
|
||||
Modules,
|
||||
RuleOperator,
|
||||
} from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
@@ -1835,6 +1835,15 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
|
||||
describe("listShippingOptionsForCartWorkflow", () => {
|
||||
let region
|
||||
|
||||
beforeEach(async () => {
|
||||
region = await regionModuleService.createRegions({
|
||||
name: "US",
|
||||
currency_code: "usd",
|
||||
})
|
||||
})
|
||||
|
||||
it("should list shipping options for cart", async () => {
|
||||
const salesChannel = await scModuleService.createSalesChannels({
|
||||
name: "Webshop",
|
||||
@@ -1846,6 +1855,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -1935,13 +1945,6 @@ medusaIntegrationTestRunner({
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1965,6 +1968,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -2044,16 +2048,7 @@ medusaIntegrationTestRunner({
|
||||
const { result } = await listShippingOptionsForCartWorkflow(
|
||||
appContainer
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
input: { cart_id: cart.id },
|
||||
})
|
||||
|
||||
expect(result).toEqual([])
|
||||
@@ -2070,6 +2065,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -2140,16 +2136,7 @@ medusaIntegrationTestRunner({
|
||||
const { errors } = await listShippingOptionsForCartWorkflow(
|
||||
appContainer
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
input: { cart_id: cart.id },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user