fix(medusa): Refresh adjustments when region on cart is changed (#1827)
* fix(medusa): Refresh adjustments when region on cart is changed * fix test * Fix unit test * fix: integration tests * fix: comment
This commit is contained in:
committed by
GitHub
parent
dffb86bb58
commit
02967f95b1
@@ -1376,7 +1376,7 @@ describe("/store/carts", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("updates prices when cart region id is updated", async () => {
|
||||
it("on region update: updates line item prices", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const beforeUpdate = await api
|
||||
@@ -1394,7 +1394,108 @@ describe("/store/carts", () => {
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.region_id).toEqual("test-region-multiple")
|
||||
expect(response.data.cart.items[0].unit_price).toEqual(700)
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
unit_price: 2000,
|
||||
variant_id: "test-variant",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
unit_price: 700,
|
||||
variant_id: "test-variant-sale-cg",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("on region update: refreshes line item adjustments", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const usdRegion = await api
|
||||
.post(`/store/carts/test-cart-3/`, {
|
||||
discounts: [{ code: "10PERCENT" }],
|
||||
})
|
||||
.catch((error) => console.log(error))
|
||||
|
||||
// current state of the cart:
|
||||
// - currency -> USD
|
||||
// - items -> [{ unit_price: 8000, ... }, { unit_price: 8000, ... }]
|
||||
// - discount -> 10%
|
||||
// - discount total -> 1600
|
||||
|
||||
expect(usdRegion.data.cart.region_id).toEqual("test-region")
|
||||
expect(usdRegion.data.cart.discount_total).toEqual(1600)
|
||||
|
||||
// now, we change the region to one operating in EUR
|
||||
// this should trigger a refresh of the line item adjustments
|
||||
const eurRegion = await api
|
||||
.post("/store/carts/test-cart-3", {
|
||||
region_id: "eur-region",
|
||||
})
|
||||
.catch((error) => console.log(error))
|
||||
|
||||
// state of the cart after update:
|
||||
// - currency -> EUR
|
||||
// - items -> [{ unit_price: 2000, ... }, { unit_price: 700, ... }]
|
||||
// - discount -> 10%
|
||||
// - discount total -> 270
|
||||
|
||||
expect(eurRegion.data.cart.region_id).toEqual("eur-region")
|
||||
expect(eurRegion.data.cart.discount_total).toEqual(270)
|
||||
})
|
||||
|
||||
it("on region update: removes line item adjustment if discount is not applicable in region", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const withDiscount = await api
|
||||
.post(`/store/carts/test-cart-3/`, {
|
||||
discounts: [{ code: "15PERCENT" }],
|
||||
})
|
||||
.catch((error) => console.log(error))
|
||||
|
||||
expect(withDiscount.data.cart.region_id).toEqual("test-region")
|
||||
expect(withDiscount.data.cart.discount_total).toEqual(2400)
|
||||
expect(withDiscount.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant-sale-cg",
|
||||
adjustments: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 1200,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant",
|
||||
adjustments: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 1200,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const response = await api
|
||||
.post("/store/carts/test-cart-3", {
|
||||
region_id: "eur-region",
|
||||
})
|
||||
.catch((error) => console.log(error))
|
||||
|
||||
expect(response.data.cart.region_id).toEqual("eur-region")
|
||||
expect(response.data.cart.discount_total).toEqual(0)
|
||||
expect(response.data.cart.items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant-sale-cg",
|
||||
adjustments: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
variant_id: "test-variant",
|
||||
adjustments: [],
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("updates address using string id", async () => {
|
||||
|
||||
@@ -57,6 +57,16 @@ module.exports = async (connection, data = {}) => {
|
||||
|
||||
await manager.save(r)
|
||||
|
||||
const europeRegion = manager.create(Region, {
|
||||
id: "eur-region",
|
||||
name: "Europe Region",
|
||||
payment_providers: [{ id: "test-pay" }],
|
||||
currency_code: "eur",
|
||||
tax_rate: 0,
|
||||
})
|
||||
|
||||
await manager.save(europeRegion)
|
||||
|
||||
// Region with multiple countries
|
||||
const regionWithMultipleCoutries = manager.create(Region, {
|
||||
id: "test-region-multiple",
|
||||
@@ -141,7 +151,7 @@ module.exports = async (connection, data = {}) => {
|
||||
ends_at: tenDaysFromToday,
|
||||
})
|
||||
|
||||
tenPercent.regions = [r]
|
||||
tenPercent.regions = [r, europeRegion]
|
||||
tenPercent.rule = tenPercentRule
|
||||
await manager.save(tenPercent)
|
||||
|
||||
@@ -533,6 +543,14 @@ module.exports = async (connection, data = {}) => {
|
||||
})
|
||||
await manager.save(ma)
|
||||
|
||||
const maEur = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant",
|
||||
currency_code: "eur",
|
||||
type: "default",
|
||||
amount: 2000,
|
||||
})
|
||||
await manager.save(maEur)
|
||||
|
||||
const ma_sale = manager.create(MoneyAmount, {
|
||||
variant_id: "test-variant-sale",
|
||||
currency_code: "usd",
|
||||
|
||||
@@ -8,16 +8,16 @@
|
||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/medusa": "1.3.3-dev-1657050014476",
|
||||
"@medusajs/medusa": "1.3.3-dev-1657484310311",
|
||||
"faker": "^5.5.3",
|
||||
"medusa-interfaces": "1.3.1-dev-1657050014476",
|
||||
"medusa-interfaces": "1.3.1-dev-1657484310311",
|
||||
"typeorm": "^0.2.31"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.12.10",
|
||||
"@babel/core": "^7.12.10",
|
||||
"@babel/node": "^7.12.10",
|
||||
"babel-preset-medusa-package": "1.1.19-dev-1657050014476",
|
||||
"babel-preset-medusa-package": "1.1.19-dev-1657484310311",
|
||||
"jest": "^26.6.3"
|
||||
}
|
||||
}
|
||||
|
||||
+7798
-10475
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user