fix: allow changing regions safely

This commit is contained in:
Sebastian Rindom
2021-10-19 18:56:52 +02:00
278 changed files with 14657 additions and 9233 deletions
+128 -14
View File
@@ -1,5 +1,12 @@
const path = require("path")
const { Region, LineItem, GiftCard } = require("@medusajs/medusa")
const {
Region,
LineItem,
GiftCard,
Cart,
CustomShippingOption,
ShippingOption,
} = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
@@ -23,7 +30,7 @@ describe("/store/carts", () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
try {
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
medusaProcess = await setupServer({ cwd, verbose: true })
} catch (error) {
console.log(error)
}
@@ -63,6 +70,24 @@ describe("/store/carts", () => {
expect(getRes.status).toEqual(200)
})
it("fails to create a cart when no region exist", async () => {
const api = useApi()
await dbConnection.manager.query(
`UPDATE "country" SET region_id=null WHERE iso_2 = 'us'`
)
await dbConnection.manager.query(`DELETE from region`)
try {
await api.post("/store/carts")
} catch (error) {
expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual(
"A region is required to create a cart"
)
}
})
it("creates a cart with country", async () => {
const api = useApi()
@@ -134,16 +159,16 @@ describe("/store/carts", () => {
expect.assertions(2)
const api = useApi()
try {
const { data } = await api.post("/store/carts/test-cart", {
discounts: [{ code: "LIMIT_REACHED" }],
await api
.post("/store/carts/test-cart", {
discounts: [{ code: "SPENT" }],
})
.catch((error) => {
expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual(
"Discount has been used maximum allowed times"
)
})
} catch (error) {
expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual(
"Discount has been used maximum allowed times"
)
}
})
it("fails to apply expired discount", async () => {
@@ -434,14 +459,42 @@ describe("/store/carts", () => {
describe("POST /store/carts/:id/shipping-methods", () => {
beforeEach(async () => {
await cartSeeder(dbConnection)
try {
await cartSeeder(dbConnection)
const manager = dbConnection.manager
const _cart = await manager.create(Cart, {
id: "test-cart-with-cso",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
type: "swap",
})
let cartWithCustomSo = await manager.save(_cart)
await manager.insert(CustomShippingOption, {
id: "another-cso-test",
cart_id: "test-cart-with-cso",
shipping_option_id: "test-option",
price: 5,
})
} catch (err) {
console.log(err)
}
})
afterEach(async () => {
await doAfterEach()
})
it("adds a shipping method to cart", async () => {
it("adds a normal shipping method to cart", async () => {
const api = useApi()
const cartWithShippingMethod = await api.post(
@@ -458,6 +511,49 @@ describe("/store/carts", () => {
expect(cartWithShippingMethod.status).toEqual(200)
})
it("given a cart with custom options and a shipping option already belonging to said cart, then it should add a shipping method based on the given custom shipping option", async () => {
const shippingOptionId = "test-option"
const api = useApi()
const cartWithCustomShippingMethod = await api
.post(
"/store/carts/test-cart-with-cso/shipping-methods",
{
option_id: shippingOptionId,
},
{ withCredentials: true }
)
.catch((err) => err.response)
expect(
cartWithCustomShippingMethod.data.cart.shipping_methods
).toContainEqual(
expect.objectContaining({
shipping_option_id: shippingOptionId,
price: 5,
})
)
expect(cartWithCustomShippingMethod.status).toEqual(200)
})
it("given a cart with custom options and an option id not corresponding to any custom shipping option, then it should throw an invalid error", async () => {
const api = useApi()
try {
await api.post(
"/store/carts/test-cart-with-cso/shipping-methods",
{
option_id: "orphan-so",
},
{ withCredentials: true }
)
} catch (err) {
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual("Wrong shipping option")
}
})
it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => {
const api = useApi()
@@ -649,7 +745,7 @@ describe("/store/carts", () => {
})
})
describe("shipping address + region updates", () => {
describe.only("shipping address + region updates", () => {
beforeEach(async () => {
try {
await cartSeeder(dbConnection)
@@ -680,9 +776,27 @@ describe("/store/carts", () => {
expect(data.cart.shipping_address).toMatchSnapshot({
id: expect.any(String),
country_code: null,
created_at: expect.any(String),
updated_at: expect.any(String),
})
})
it("updates region only - single to multipe countries", async () => {
const api = useApi()
const { data, status } = await api
.post(`/store/carts/test-cart`, {
shipping_address: null,
})
.catch((err) => {
console.log(err)
throw err
})
expect(status).toEqual(200)
expect(data.cart.shipping_address).toEqual(null)
})
// it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => {})
})
})