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",
|
||||
}),
|
||||
|
||||
@@ -0,0 +1,360 @@
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
IFulfillmentModuleService,
|
||||
IRegionModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
|
||||
import {
|
||||
createAdminUser,
|
||||
generatePublishableKey,
|
||||
generateStoreHeaders,
|
||||
} from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("Store: Shipping Option API", () => {
|
||||
let appContainer
|
||||
let fulfillmentModule: IFulfillmentModuleService
|
||||
let regionService: IRegionModuleService
|
||||
|
||||
let salesChannel
|
||||
let region
|
||||
let regionTwo
|
||||
let product
|
||||
let stockLocation
|
||||
let shippingProfile
|
||||
let fulfillmentSet
|
||||
let cart
|
||||
let shippingOption
|
||||
let storeHeaders
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT)
|
||||
regionService = appContainer.resolve(Modules.REGION)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const publishableKey = await generatePublishableKey(appContainer)
|
||||
storeHeaders = generateStoreHeaders({ publishableKey })
|
||||
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
const remoteLinkService = appContainer.resolve(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
|
||||
region = await regionService.createRegions({
|
||||
name: "Test region",
|
||||
countries: ["US"],
|
||||
currency_code: "usd",
|
||||
})
|
||||
|
||||
regionTwo = await regionService.createRegions({
|
||||
name: "Test region two",
|
||||
countries: ["DK"],
|
||||
currency_code: "dkk",
|
||||
})
|
||||
|
||||
await api.post(
|
||||
"/admin/price-preferences",
|
||||
{
|
||||
attribute: "region_id",
|
||||
value: regionTwo.id,
|
||||
is_tax_inclusive: true,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
salesChannel = (
|
||||
await api.post(
|
||||
"/admin/sales-channels",
|
||||
{ name: "first channel", description: "channel" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.sales_channel
|
||||
|
||||
product = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: "Test fixture",
|
||||
options: [
|
||||
{ title: "size", values: ["large", "small"] },
|
||||
{ title: "color", values: ["green"] },
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Test variant",
|
||||
manage_inventory: false,
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
},
|
||||
{
|
||||
currency_code: "dkk",
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
size: "large",
|
||||
color: "green",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
stockLocation = (
|
||||
await api.post(
|
||||
`/admin/stock-locations`,
|
||||
{
|
||||
name: "test location",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
shippingProfile = await fulfillmentModule.createShippingProfiles({
|
||||
name: "Test",
|
||||
type: "default",
|
||||
})
|
||||
|
||||
fulfillmentSet = await fulfillmentModule.createFulfillmentSets({
|
||||
name: "Test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
{
|
||||
name: "Test",
|
||||
geo_zones: [
|
||||
{ type: "country", country_code: "us" },
|
||||
{ type: "country", country_code: "dk" },
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await remoteLinkService.create([
|
||||
{
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: salesChannel.id,
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: stockLocation.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: stockLocation.id,
|
||||
},
|
||||
[Modules.FULFILLMENT]: {
|
||||
fulfillment_set_id: fulfillmentSet.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
|
||||
{ add: ["manual_test-provider"] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
shippingOption = (
|
||||
await api.post(
|
||||
`/admin/shipping-options`,
|
||||
{
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
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,
|
||||
},
|
||||
],
|
||||
rules: [],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option
|
||||
})
|
||||
|
||||
describe("GET /store/shipping-options?cart_id=", () => {
|
||||
it("should get shipping options for a cart successfully", 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,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
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",
|
||||
amount: 1100,
|
||||
price_type: "flat",
|
||||
})
|
||||
)
|
||||
|
||||
cart = (
|
||||
await api.post(
|
||||
`/store/carts/${cart.id}`,
|
||||
{
|
||||
region_id: regionTwo.id,
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
const secondResp = await api.get(
|
||||
`/store/shipping-options?cart_id=${cart.id}`,
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
expect(secondResp.data.shipping_options).toHaveLength(1)
|
||||
expect(secondResp.data.shipping_options[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: shippingOption.id,
|
||||
name: "Test shipping option",
|
||||
amount: 500,
|
||||
price_type: "flat",
|
||||
is_tax_inclusive: true,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
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",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user