chore: Use module test runner in Carts API test suite (#6679)

Nothing but migrating to our new module test runner. Hopefully, that'll make the actual test PRs easier to digest.
This commit is contained in:
Oli Juhl
2024-03-13 07:32:58 +00:00
committed by GitHub
parent d4b921f3db
commit db26dafa85
3 changed files with 2632 additions and 2796 deletions
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/store/carts POST /store/carts/:id fails to complete cart with items inventory not/partially covered 1`] = ` exports[` POST /store/carts/:id fails to complete cart with items inventory not/partially covered 1`] = `
Object { Object {
"errors": Array [ "errors": Array [
Object { Object {
@@ -12,7 +12,7 @@ Object {
} }
`; `;
exports[`/store/carts POST /store/carts/:id fails to complete swap cart with items inventory not/partially covered 1`] = ` exports[` POST /store/carts/:id fails to complete swap cart with items inventory not/partially covered 1`] = `
Object { Object {
"errors": Array [ "errors": Array [
Object { Object {
@@ -24,7 +24,7 @@ Object {
} }
`; `;
exports[`/store/carts shipping address + region updates updates region only - single to multiple countries 1`] = ` exports[` shipping address + region updates updates region only - single to multiple countries 1`] = `
Object { Object {
"address_1": null, "address_1": null,
"address_2": null, "address_2": null,
@@ -1,5 +1,11 @@
const path = require("path") const { medusaIntegrationTestRunner } = require("medusa-test-utils")
const { const { useApi } = require("../../../../environment-helpers/use-api")
jest.setTimeout(30000)
let cartSeeder = undefined
let swapSeeder = undefined
let {
Region, Region,
LineItem, LineItem,
GiftCard, GiftCard,
@@ -7,15 +13,40 @@ const {
CustomShippingOption, CustomShippingOption,
PriceList, PriceList,
MoneyAmount, MoneyAmount,
} = require("@medusajs/medusa") ProductVariantMoneyAmount,
} = {}
let {
simpleCustomerFactory,
simpleCartFactory,
simpleLineItemFactory,
simpleRegionFactory,
simpleProductFactory,
simpleSalesChannelFactory,
simpleShippingOptionFactory,
simplePriceListFactory,
simpleDiscountFactory,
simpleCustomerGroupFactory,
} = {}
const setupServer = require("../../../../environment-helpers/setup-server") medusaIntegrationTestRunner({
const { useApi } = require("../../../../environment-helpers/use-api") env: { MEDUSA_FF_MEDUSA_V2: false },
const { initDb, useDb } = require("../../../../environment-helpers/use-db") testSuite: ({ dbConnection, getContainer, api }) => {
let container
const cartSeeder = require("../../../../helpers/cart-seeder") beforeAll(() => {
const swapSeeder = require("../../../../helpers/swap-seeder") cartSeeder = require("../../../../helpers/cart-seeder")
const { swapSeeder = require("../../../../helpers/swap-seeder")
;({
Region,
LineItem,
GiftCard,
Cart,
CustomShippingOption,
PriceList,
MoneyAmount,
ProductVariantMoneyAmount,
} = require("@medusajs/medusa"))
;({
simpleCartFactory, simpleCartFactory,
simpleRegionFactory, simpleRegionFactory,
simpleProductFactory, simpleProductFactory,
@@ -23,39 +54,14 @@ const {
simpleLineItemFactory, simpleLineItemFactory,
simpleSalesChannelFactory, simpleSalesChannelFactory,
simplePriceListFactory, simplePriceListFactory,
} = require("../../../../factories")
const {
simpleDiscountFactory, simpleDiscountFactory,
} = require("../../../../factories/simple-discount-factory")
const {
simpleCustomerFactory, simpleCustomerFactory,
} = require("../../../../factories/simple-customer-factory")
const {
simpleCustomerGroupFactory, simpleCustomerGroupFactory,
} = require("../../../../factories/simple-customer-group-factory") } = require("../../../../factories"))
const { ProductVariantMoneyAmount } = require("@medusajs/medusa")
jest.setTimeout(30000)
describe("/store/carts", () => {
let medusaProcess
let dbConnection
const doAfterEach = async () => {
const db = useDb()
return await db.teardown()
}
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
}) })
afterAll(async () => { beforeEach(async () => {
const db = useDb() container = getContainer()
await db.shutdown()
medusaProcess.kill()
}) })
describe("POST /store/carts", () => { describe("POST /store/carts", () => {
@@ -92,13 +98,7 @@ describe("/store/carts", () => {
}) })
}) })
afterEach(async () => {
await doAfterEach()
})
it("creates a cart", async () => { it("creates a cart", async () => {
const api = useApi()
const response = await api.post("/store/carts") const response = await api.post("/store/carts")
expect(response.status).toEqual(200) expect(response.status).toEqual(200)
@@ -107,8 +107,6 @@ describe("/store/carts", () => {
}) })
it("fails to create a cart when no region exist", async () => { it("fails to create a cart when no region exist", async () => {
const api = useApi()
await dbConnection.manager.query( await dbConnection.manager.query(
`UPDATE "country" `UPDATE "country"
SET region_id=null SET region_id=null
@@ -159,8 +157,6 @@ describe("/store/carts", () => {
money_amount_id: ma_sale_1.id, money_amount_id: ma_sale_1.id,
}) })
const api = useApi()
const response = await api const response = await api
.post("/store/carts", { .post("/store/carts", {
items: [ items: [
@@ -199,8 +195,6 @@ describe("/store/carts", () => {
}) })
it("creates a cart with country", async () => { it("creates a cart with country", async () => {
const api = useApi()
const response = await api.post("/store/carts", { const response = await api.post("/store/carts", {
country_code: "us", country_code: "us",
}) })
@@ -212,7 +206,6 @@ describe("/store/carts", () => {
}) })
it("creates a cart with context", async () => { it("creates a cart with context", async () => {
const api = useApi()
const response = await api.post("/store/carts", { const response = await api.post("/store/carts", {
context: { context: {
test_id: "test", test_id: "test",
@@ -225,7 +218,7 @@ describe("/store/carts", () => {
const cart = getRes.data.cart const cart = getRes.data.cart
expect(cart.context).toEqual({ expect(cart.context).toEqual({
ip: "::ffff:127.0.0.1", ip: expect.any(String),
user_agent: expect.stringContaining("axios/0.21."), user_agent: expect.stringContaining("axios/0.21."),
test_id: "test", test_id: "test",
}) })
@@ -238,13 +231,7 @@ describe("/store/carts", () => {
await swapSeeder(dbConnection) await swapSeeder(dbConnection)
}) })
afterEach(async () => {
await doAfterEach()
})
it("adds line item to cart", async () => { it("adds line item to cart", async () => {
const api = useApi()
// Add standard line item to cart // Add standard line item to cart
const response = await api const response = await api
.post( .post(
@@ -272,8 +259,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart containing a total fixed discount", async () => { it("adds line item to cart containing a total fixed discount", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart-w-total-fixed-discount/line-items", "/store/carts/test-cart-w-total-fixed-discount/line-items",
@@ -306,8 +291,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart containing a total percentage discount", async () => { it("adds line item to cart containing a total percentage discount", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart-w-total-percentage-discount/line-items", "/store/carts/test-cart-w-total-percentage-discount/line-items",
@@ -340,8 +323,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart containing an item fixed discount", async () => { it("adds line item to cart containing an item fixed discount", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart-w-item-fixed-discount/line-items", "/store/carts/test-cart-w-item-fixed-discount/line-items",
@@ -374,8 +355,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart containing an item percentage discount", async () => { it("adds line item to cart containing an item percentage discount", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart-w-item-percentage-discount/line-items", "/store/carts/test-cart-w-item-percentage-discount/line-items",
@@ -408,8 +387,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart time limited sale", async () => { it("adds line item to cart time limited sale", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart/line-items", "/store/carts/test-cart/line-items",
@@ -435,8 +412,6 @@ describe("/store/carts", () => {
}) })
it("adds line item to cart time customer pricing", async () => { it("adds line item to cart time customer pricing", async () => {
const api = useApi()
// customer with customer-group 5 // customer with customer-group 5
const authResponse = await api.post("/store/auth", { const authResponse = await api.post("/store/auth", {
email: "test5@email.com", email: "test5@email.com",
@@ -476,8 +451,6 @@ describe("/store/carts", () => {
}) })
it("adds line item with quantity to cart with quantity discount", async () => { it("adds line item with quantity to cart with quantity discount", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart/line-items", "/store/carts/test-cart/line-items",
@@ -503,8 +476,6 @@ describe("/store/carts", () => {
}) })
it("adds line item with quantity to cart with quantity discount no ceiling", async () => { it("adds line item with quantity to cart with quantity discount no ceiling", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart/line-items", "/store/carts/test-cart/line-items",
@@ -545,7 +516,11 @@ describe("/store/carts", () => {
let discount let discount
beforeEach(async () => { beforeEach(async () => {
discount = await simpleDiscountFactory(dbConnection, discountData, 100) discount = await simpleDiscountFactory(
dbConnection,
discountData,
100
)
discountCart = await simpleCartFactory( discountCart = await simpleCartFactory(
dbConnection, dbConnection,
{ {
@@ -591,13 +566,7 @@ describe("/store/carts", () => {
.add(discount) .add(discount)
}) })
afterEach(async () => {
await doAfterEach()
})
it("updates an old line item adjustment when a new line item is added to a discount cart", async () => { it("updates an old line item adjustment when a new line item is added to a discount cart", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/discount-cart/line-items", "/store/carts/discount-cart/line-items",
@@ -644,8 +613,6 @@ describe("/store/carts", () => {
}) })
it("updates an existing item adjustment when a line item is updated", async () => { it("updates an existing item adjustment when a line item is updated", async () => {
const api = useApi()
await simpleLineItemFactory( await simpleLineItemFactory(
dbConnection, dbConnection,
{ {
@@ -714,8 +681,6 @@ describe("/store/carts", () => {
}) })
it("updates an existing item adjustment when a line item is deleted from a discount cart", async () => { it("updates an existing item adjustment when a line item is deleted from a discount cart", async () => {
const api = useApi()
await simpleLineItemFactory( await simpleLineItemFactory(
dbConnection, dbConnection,
{ {
@@ -767,13 +732,7 @@ describe("/store/carts", () => {
await swapSeeder(dbConnection) await swapSeeder(dbConnection)
}) })
afterEach(async () => {
await doAfterEach()
})
it("updates line item of cart", async () => { it("updates line item of cart", async () => {
const api = useApi()
const response = await api const response = await api
.post( .post(
"/store/carts/test-cart-3/line-items/test-item3/", "/store/carts/test-cart-3/line-items/test-item3/",
@@ -805,7 +764,6 @@ describe("/store/carts", () => {
}) })
it("updates line item of a cart containing a total fixed discount", async () => { it("updates line item of a cart containing a total fixed discount", async () => {
const api = useApi()
await simpleLineItemFactory(dbConnection, { await simpleLineItemFactory(dbConnection, {
id: "test-li-disc", id: "test-li-disc",
allow_discounts: true, allow_discounts: true,
@@ -849,7 +807,6 @@ describe("/store/carts", () => {
}) })
it("updates line item of a cart containing a total percentage discount", async () => { it("updates line item of a cart containing a total percentage discount", async () => {
const api = useApi()
await simpleLineItemFactory(dbConnection, { await simpleLineItemFactory(dbConnection, {
id: "test-li-disc", id: "test-li-disc",
allow_discounts: true, allow_discounts: true,
@@ -893,7 +850,6 @@ describe("/store/carts", () => {
}) })
it("updates line item of a cart containing an item fixed discount", async () => { it("updates line item of a cart containing an item fixed discount", async () => {
const api = useApi()
await simpleLineItemFactory(dbConnection, { await simpleLineItemFactory(dbConnection, {
id: "test-li-disc", id: "test-li-disc",
allow_discounts: true, allow_discounts: true,
@@ -937,7 +893,6 @@ describe("/store/carts", () => {
}) })
it("updates line item of a cart containing an item percentage discount", async () => { it("updates line item of a cart containing an item percentage discount", async () => {
const api = useApi()
await simpleLineItemFactory(dbConnection, { await simpleLineItemFactory(dbConnection, {
id: "test-li-disc", id: "test-li-disc",
allow_discounts: true, allow_discounts: true,
@@ -981,8 +936,6 @@ describe("/store/carts", () => {
}) })
it("updates line item quantity with unit price reflected", async () => { it("updates line item quantity with unit price reflected", async () => {
const api = useApi()
await simplePriceListFactory(dbConnection, { await simplePriceListFactory(dbConnection, {
id: "pl_current", id: "pl_current",
prices: [ prices: [
@@ -1018,8 +971,6 @@ describe("/store/carts", () => {
}) })
it("updates line item quantity with unit price reflected when merging line-items", async () => { it("updates line item quantity with unit price reflected when merging line-items", async () => {
const api = useApi()
await simplePriceListFactory(dbConnection, { await simplePriceListFactory(dbConnection, {
id: "pl_current", id: "pl_current",
prices: [ prices: [
@@ -1056,8 +1007,6 @@ describe("/store/carts", () => {
}) })
it("creates and updates line item quantity with unit price reflected", async () => { it("creates and updates line item quantity with unit price reflected", async () => {
const api = useApi()
await simplePriceListFactory(dbConnection, { await simplePriceListFactory(dbConnection, {
id: "pl_current", id: "pl_current",
prices: [ prices: [
@@ -1135,17 +1084,11 @@ describe("/store/carts", () => {
}) })
}) })
afterEach(async () => {
await doAfterEach()
})
// We were experiencing some issues when having created a cart in a region // We were experiencing some issues when having created a cart in a region
// containing multiple countries. At this point, the cart does not have a shipping // containing multiple countries. At this point, the cart does not have a shipping
// address. Therefore, on subsequent requests to update the cart, the server // address. Therefore, on subsequent requests to update the cart, the server
// would throw a 500 due to missing shipping address id on insertion. // would throw a 500 due to missing shipping address id on insertion.
it("updates a cart, that does not have a shipping address", async () => { it("updates a cart, that does not have a shipping address", async () => {
const api = useApi()
const response = await api.post("/store/carts", { const response = await api.post("/store/carts", {
region_id: "test-region-multiple", region_id: "test-region-multiple",
}) })
@@ -1158,8 +1101,6 @@ describe("/store/carts", () => {
}) })
it("fails on apply discount if limit has been reached", async () => { it("fails on apply discount if limit has been reached", async () => {
const api = useApi()
const code = "SPENT" const code = "SPENT"
const err = await api const err = await api
@@ -1176,8 +1117,6 @@ describe("/store/carts", () => {
}) })
it("successfully passes customer conditions with `in` operator and applies discount", async () => { it("successfully passes customer conditions with `in` operator and applies discount", async () => {
const api = useApi()
await simpleCustomerFactory(dbConnection, { await simpleCustomerFactory(dbConnection, {
id: "cus_1234", id: "cus_1234",
email: "oli@medusajs.com", email: "oli@medusajs.com",
@@ -1249,8 +1188,6 @@ describe("/store/carts", () => {
}) })
it("throws if no customer is associated with the cart while applying a customer groups discount", async () => { it("throws if no customer is associated with the cart while applying a customer groups discount", async () => {
const api = useApi()
await simpleCustomerGroupFactory(dbConnection, { await simpleCustomerGroupFactory(dbConnection, {
id: "customer-group-2", id: "customer-group-2",
name: "Loyal", name: "Loyal",
@@ -1353,8 +1290,6 @@ describe("/store/carts", () => {
.of(discountCart) .of(discountCart)
.add(discount) .add(discount)
const api = useApi()
let response = await api.post(`/store/carts/${cartId}/line-items`, { let response = await api.post(`/store/carts/${cartId}/line-items`, {
quantity: 1, quantity: 1,
variant_id: "test-variant-quantity", variant_id: "test-variant-quantity",
@@ -1383,8 +1318,6 @@ describe("/store/carts", () => {
}) })
it("successfully passes customer conditions with `not_in` operator and applies discount", async () => { it("successfully passes customer conditions with `not_in` operator and applies discount", async () => {
const api = useApi()
await simpleCustomerFactory(dbConnection, { await simpleCustomerFactory(dbConnection, {
id: "cus_1234", id: "cus_1234",
email: "oli@medusajs.com", email: "oli@medusajs.com",
@@ -1461,8 +1394,6 @@ describe("/store/carts", () => {
}) })
it("successfully applies discount in case no conditions is defined for group", async () => { it("successfully applies discount in case no conditions is defined for group", async () => {
const api = useApi()
await simpleCustomerFactory(dbConnection, { await simpleCustomerFactory(dbConnection, {
id: "cus_1234", id: "cus_1234",
email: "oli@medusajs.com", email: "oli@medusajs.com",
@@ -1522,8 +1453,6 @@ describe("/store/carts", () => {
}) })
it("fails to apply discount if customer group is part of `not_in` conditions", async () => { it("fails to apply discount if customer group is part of `not_in` conditions", async () => {
const api = useApi()
await simpleCustomerFactory(dbConnection, { await simpleCustomerFactory(dbConnection, {
id: "cus_1234", id: "cus_1234",
email: "oli@medusajs.com", email: "oli@medusajs.com",
@@ -1588,8 +1517,6 @@ describe("/store/carts", () => {
}) })
it("fails to apply discount if customer group is not part of `in` conditions", async () => { it("fails to apply discount if customer group is not part of `in` conditions", async () => {
const api = useApi()
await simpleCustomerFactory(dbConnection, { await simpleCustomerFactory(dbConnection, {
id: "cus_1234", id: "cus_1234",
email: "oli@medusajs.com", email: "oli@medusajs.com",
@@ -1660,7 +1587,6 @@ describe("/store/carts", () => {
it("fails to apply expired discount", async () => { it("fails to apply expired discount", async () => {
expect.assertions(2) expect.assertions(2)
const api = useApi()
const code = "EXP_DISC" const code = "EXP_DISC"
@@ -1676,7 +1602,6 @@ describe("/store/carts", () => {
it("fails on discount before start day", async () => { it("fails on discount before start day", async () => {
expect.assertions(2) expect.assertions(2)
const api = useApi()
const code = "PREM_DISC" const code = "PREM_DISC"
@@ -1693,8 +1618,6 @@ describe("/store/carts", () => {
}) })
it("fails on apply invalid dynamic discount", async () => { it("fails on apply invalid dynamic discount", async () => {
const api = useApi()
const code = "INV_DYN_DISC" const code = "INV_DYN_DISC"
const err = await api const err = await api
@@ -1708,8 +1631,6 @@ describe("/store/carts", () => {
}) })
it("Applies dynamic discount to cart correctly", async () => { it("Applies dynamic discount to cart correctly", async () => {
const api = useApi()
const cart = await api.post("/store/carts/test-cart", { const cart = await api.post("/store/carts/test-cart", {
discounts: [{ code: "DYN_DISC" }], discounts: [{ code: "DYN_DISC" }],
}) })
@@ -1719,8 +1640,6 @@ describe("/store/carts", () => {
}) })
it("successfully apply a fixed discount with total allocation and return the total with the right precision", async () => { it("successfully apply a fixed discount with total allocation and return the total with the right precision", async () => {
const api = useApi()
const cartId = "test-cart" const cartId = "test-cart"
const quantity = 2 const quantity = 2
@@ -1803,8 +1722,6 @@ describe("/store/carts", () => {
}) })
it("updates cart customer id", async () => { it("updates cart customer id", async () => {
const api = useApi()
const response = await api.post("/store/carts/test-cart", { const response = await api.post("/store/carts/test-cart", {
customer_id: "test-customer-2", customer_id: "test-customer-2",
}) })
@@ -1813,8 +1730,6 @@ describe("/store/carts", () => {
}) })
it("should remove shipping on line item remove", async () => { it("should remove shipping on line item remove", async () => {
const api = useApi()
const cartId = "test-cart-2" const cartId = "test-cart-2"
const lineId = "test-item" const lineId = "test-item"
const optionId = "test-option" const optionId = "test-option"
@@ -1839,8 +1754,6 @@ describe("/store/carts", () => {
}) })
it("should remove shipping on line item update", async () => { it("should remove shipping on line item update", async () => {
const api = useApi()
const cartId = "test-cart-2" const cartId = "test-cart-2"
const lineId = "test-item" const lineId = "test-item"
const optionId = "test-option" const optionId = "test-option"
@@ -1868,8 +1781,6 @@ describe("/store/carts", () => {
}) })
it("should remove shipping on line item add", async () => { it("should remove shipping on line item add", async () => {
const api = useApi()
const cartId = "test-cart-2" const cartId = "test-cart-2"
const optionId = "test-option" const optionId = "test-option"
@@ -1894,8 +1805,6 @@ describe("/store/carts", () => {
}) })
it("updates prices when cart customer id is updated", async () => { it("updates prices when cart customer id is updated", async () => {
const api = useApi()
const beforeUpdate = await api const beforeUpdate = await api
.get(`/store/carts/test-cart-3`) .get(`/store/carts/test-cart-3`)
.catch((error) => console.log(error)) .catch((error) => console.log(error))
@@ -1920,8 +1829,6 @@ describe("/store/carts", () => {
}) })
it("on region update: updates line item prices", async () => { it("on region update: updates line item prices", async () => {
const api = useApi()
const beforeUpdate = await api const beforeUpdate = await api
.get(`/store/carts/test-cart-3`) .get(`/store/carts/test-cart-3`)
.catch((error) => console.log(error)) .catch((error) => console.log(error))
@@ -1952,8 +1859,6 @@ describe("/store/carts", () => {
}) })
it("on region update: refreshes line item adjustments", async () => { it("on region update: refreshes line item adjustments", async () => {
const api = useApi()
const usdRegion = await api const usdRegion = await api
.post(`/store/carts/test-cart-3/`, { .post(`/store/carts/test-cart-3/`, {
discounts: [{ code: "10PERCENT" }], discounts: [{ code: "10PERCENT" }],
@@ -1988,8 +1893,6 @@ describe("/store/carts", () => {
}) })
it("on region update: removes line item adjustment if discount is not applicable in region", async () => { it("on region update: removes line item adjustment if discount is not applicable in region", async () => {
const api = useApi()
const withDiscount = await api const withDiscount = await api
.post(`/store/carts/test-cart-3/`, { .post(`/store/carts/test-cart-3/`, {
discounts: [{ code: "15PERCENT" }], discounts: [{ code: "15PERCENT" }],
@@ -2042,8 +1945,6 @@ describe("/store/carts", () => {
}) })
it("updates address using string id", async () => { it("updates address using string id", async () => {
const api = useApi()
const response = await api.post("/store/carts/test-cart", { const response = await api.post("/store/carts/test-cart", {
billing_address: "test-general-address", billing_address: "test-general-address",
shipping_address: "test-general-address", shipping_address: "test-general-address",
@@ -2059,8 +1960,6 @@ describe("/store/carts", () => {
}) })
it("updates address", async () => { it("updates address", async () => {
const api = useApi()
const response = await api.post("/store/carts/test-cart", { const response = await api.post("/store/carts/test-cart", {
shipping_address: { shipping_address: {
first_name: "clark", first_name: "clark",
@@ -2077,8 +1976,6 @@ describe("/store/carts", () => {
}) })
it("partially updates shipping and billing address while retaining the addresses country codes", async () => { it("partially updates shipping and billing address while retaining the addresses country codes", async () => {
const api = useApi()
// Partially update the shipping address // Partially update the shipping address
await api await api
.post("/store/carts/test-cart", { .post("/store/carts/test-cart", {
@@ -2115,8 +2012,6 @@ describe("/store/carts", () => {
}) })
it("adds free shipping to cart then removes it again", async () => { it("adds free shipping to cart then removes it again", async () => {
const api = useApi()
let cart = await api.post( let cart = await api.post(
"/store/carts/test-cart", "/store/carts/test-cart",
{ {
@@ -2150,8 +2045,6 @@ describe("/store/carts", () => {
region_id: "test-region", region_id: "test-region",
}) })
const api = useApi()
await api.post(`/store/carts/test-cart-3`, { await api.post(`/store/carts/test-cart-3`, {
gift_cards: [{ code: "GC_TEST" }], gift_cards: [{ code: "GC_TEST" }],
}) })
@@ -2176,8 +2069,6 @@ describe("/store/carts", () => {
regions: ["test-region"], regions: ["test-region"],
}) })
const api = useApi()
await api await api
.post(`/store/carts/test-cart-3`, { .post(`/store/carts/test-cart-3`, {
discounts: [{ code: "100PERCENT" }], discounts: [{ code: "100PERCENT" }],
@@ -2197,7 +2088,6 @@ describe("/store/carts", () => {
}) })
it("complete cart with items inventory covered", async () => { it("complete cart with items inventory covered", async () => {
const api = useApi()
const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`) const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`)
expect(getRes.status).toEqual(200) expect(getRes.status).toEqual(200)
@@ -2209,8 +2099,6 @@ describe("/store/carts", () => {
}) })
it("calculates correct payment totals on cart completion taking into account line item adjustments", async () => { it("calculates correct payment totals on cart completion taking into account line item adjustments", async () => {
const api = useApi()
await api.post("/store/carts/test-cart-3", { await api.post("/store/carts/test-cart-3", {
discounts: [{ code: "CREATED" }], discounts: [{ code: "CREATED" }],
}) })
@@ -2235,8 +2123,6 @@ describe("/store/carts", () => {
}) })
it("should return early, if cart is already completed", async () => { it("should return early, if cart is already completed", async () => {
const api = useApi()
const completedCart = await api.post( const completedCart = await api.post(
`/store/carts/test-cart-2/complete-cart` `/store/carts/test-cart-2/complete-cart`
) )
@@ -2272,8 +2158,6 @@ describe("/store/carts", () => {
}) })
await manager.save(li) await manager.save(li)
const api = useApi()
try { try {
await api.post(`/store/carts/test-cart-2/complete-cart`) await api.post(`/store/carts/test-cart-2/complete-cart`)
} catch (e) { } catch (e) {
@@ -2308,8 +2192,6 @@ describe("/store/carts", () => {
"UPDATE swap SET cart_id='swap-cart' where id='test-swap'" "UPDATE swap SET cart_id='swap-cart' where id='test-swap'"
) )
const api = useApi()
try { try {
await api.post(`/store/carts/swap-cart/complete-cart`) await api.post(`/store/carts/swap-cart/complete-cart`)
} catch (e) { } catch (e) {
@@ -2347,8 +2229,6 @@ describe("/store/carts", () => {
) )
await manager.query("DELETE FROM payment where swap_id='test-swap'") await manager.query("DELETE FROM payment where swap_id='test-swap'")
const api = useApi()
await api.post(`/store/carts/swap-cart/complete-cart`) await api.post(`/store/carts/swap-cart/complete-cart`)
// check to see if payment is authorized and cart is completed // check to see if payment is authorized and cart is completed
@@ -2365,8 +2245,6 @@ describe("/store/carts", () => {
await manager.query("DELETE FROM payment where swap_id='test-swap'") await manager.query("DELETE FROM payment where swap_id='test-swap'")
const api = useApi()
await api.post(`/store/carts/swap-cart/complete-cart`) await api.post(`/store/carts/swap-cart/complete-cart`)
const alreadyCompletedCart = await api.post( const alreadyCompletedCart = await api.post(
@@ -2384,7 +2262,6 @@ describe("/store/carts", () => {
}) })
it("completes cart with a non-customer and for a customer with the same email created later the order doesn't show up", async () => { it("completes cart with a non-customer and for a customer with the same email created later the order doesn't show up", async () => {
const api = useApi()
const customerEmail = "test-email-for-non-existent-customer@test.com" const customerEmail = "test-email-for-non-existent-customer@test.com"
const product = await simpleProductFactory(dbConnection) const product = await simpleProductFactory(dbConnection)
@@ -2419,7 +2296,8 @@ describe("/store/carts", () => {
password: "test", password: "test",
}) })
const [authCookie] = customerResponse.headers["set-cookie"][0].split(";") const [authCookie] =
customerResponse.headers["set-cookie"][0].split(";")
const customerOrdersResponse = await api const customerOrdersResponse = await api
.get("/store/customers/me/orders?status[]=completed", { .get("/store/customers/me/orders?status[]=completed", {
@@ -2436,7 +2314,6 @@ describe("/store/carts", () => {
}) })
it("complete cart throws if there is no customer on the cart", async () => { it("complete cart throws if there is no customer on the cart", async () => {
const api = useApi()
const product = await simpleProductFactory(dbConnection) const product = await simpleProductFactory(dbConnection)
const region = await simpleRegionFactory(dbConnection, { tax_rate: 10 }) const region = await simpleRegionFactory(dbConnection, { tax_rate: 10 })
const cart = await simpleCartFactory(dbConnection, { const cart = await simpleCartFactory(dbConnection, {
@@ -2492,13 +2369,7 @@ describe("/store/carts", () => {
}) })
}) })
afterEach(async () => {
await doAfterEach()
})
it("adds a normal shipping method to cart", async () => { it("adds a normal shipping method to cart", async () => {
const api = useApi()
const cartWithShippingMethod = await api.post( const cartWithShippingMethod = await api.post(
"/store/carts/test-cart/shipping-methods", "/store/carts/test-cart/shipping-methods",
{ {
@@ -2507,7 +2378,9 @@ describe("/store/carts", () => {
{ withCredentials: true } { withCredentials: true }
) )
expect(cartWithShippingMethod.data.cart.shipping_methods).toContainEqual( expect(
cartWithShippingMethod.data.cart.shipping_methods
).toContainEqual(
expect.objectContaining({ shipping_option_id: "test-option" }) expect.objectContaining({ shipping_option_id: "test-option" })
) )
expect(cartWithShippingMethod.status).toEqual(200) expect(cartWithShippingMethod.status).toEqual(200)
@@ -2516,8 +2389,6 @@ describe("/store/carts", () => {
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 () => { 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 shippingOptionId = "test-option"
const api = useApi()
const cartWithCustomShippingMethod = await api const cartWithCustomShippingMethod = await api
.post( .post(
"/store/carts/test-cart-with-cso/shipping-methods", "/store/carts/test-cart-with-cso/shipping-methods",
@@ -2540,8 +2411,6 @@ describe("/store/carts", () => {
}) })
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 () => { 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 { try {
await api.post( await api.post(
"/store/carts/test-cart-with-cso/shipping-methods", "/store/carts/test-cart-with-cso/shipping-methods",
@@ -2557,8 +2426,6 @@ describe("/store/carts", () => {
}) })
it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => { it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => {
const api = useApi()
// Add standard line item to cart // Add standard line item to cart
await api.post( await api.post(
"/store/carts/test-cart/line-items", "/store/carts/test-cart/line-items",
@@ -2616,7 +2483,6 @@ describe("/store/carts", () => {
}) })
it("adds no more than 1 shipping method per shipping profile", async () => { it("adds no more than 1 shipping method per shipping profile", async () => {
const api = useApi()
const addShippingMethod = async (option_id) => { const addShippingMethod = async (option_id) => {
return await api.post( return await api.post(
"/store/carts/test-cart/shipping-methods", "/store/carts/test-cart/shipping-methods",
@@ -2667,13 +2533,7 @@ describe("/store/carts", () => {
) )
}) })
afterEach(async () => {
await doAfterEach()
})
it("removes free shipping and updates shipping total", async () => { it("removes free shipping and updates shipping total", async () => {
const api = useApi()
const cartWithFreeShipping = await api.post( const cartWithFreeShipping = await api.post(
"/store/carts/test-cart", "/store/carts/test-cart",
{ {
@@ -2728,8 +2588,6 @@ describe("/store/carts", () => {
.of(discountCart) .of(discountCart)
.add(discount) .add(discount)
const api = useApi()
let response = await api.post(`/store/carts/${cartId}/line-items`, { let response = await api.post(`/store/carts/${cartId}/line-items`, {
quantity: 1, quantity: 1,
variant_id: "test-variant-quantity", variant_id: "test-variant-quantity",
@@ -2767,13 +2625,7 @@ describe("/store/carts", () => {
}) })
}) })
afterEach(async () => {
await doAfterEach()
})
it("updates empty cart.customer_id on cart retrieval", async () => { it("updates empty cart.customer_id on cart retrieval", async () => {
const api = useApi()
const customer = await api.post( const customer = await api.post(
"/store/customers", "/store/customers",
{ {
@@ -2787,7 +2639,11 @@ describe("/store/carts", () => {
const cookie = customer.headers["set-cookie"][0] const cookie = customer.headers["set-cookie"][0]
const cart = await api.post("/store/carts", {}, { withCredentials: true }) const cart = await api.post(
"/store/carts",
{},
{ withCredentials: true }
)
const response = await api.get(`/store/carts/${cart.data.cart.id}`, { const response = await api.get(`/store/carts/${cart.data.cart.id}`, {
headers: { headers: {
@@ -2796,13 +2652,13 @@ describe("/store/carts", () => {
withCredentials: true, withCredentials: true,
}) })
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id) expect(response.data.cart.customer_id).toEqual(
customer.data.customer.id
)
expect(response.status).toEqual(200) expect(response.status).toEqual(200)
}) })
it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => { it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => {
const api = useApi()
const customer = await api.post( const customer = await api.post(
"/store/customers", "/store/customers",
{ {
@@ -2818,9 +2674,12 @@ describe("/store/carts", () => {
const cart = await api.post("/store/carts") const cart = await api.post("/store/carts")
const updatedCart = await api.post(`/store/carts/${cart.data.cart.id}`, { const updatedCart = await api.post(
`/store/carts/${cart.data.cart.id}`,
{
customer_id: "test-customer", customer_id: "test-customer",
}) }
)
const response = await api.get( const response = await api.get(
`/store/carts/${updatedCart.data.cart.id}`, `/store/carts/${updatedCart.data.cart.id}`,
@@ -2831,7 +2690,9 @@ describe("/store/carts", () => {
} }
) )
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id) expect(response.data.cart.customer_id).toEqual(
customer.data.customer.id
)
expect(response.status).toEqual(200) expect(response.status).toEqual(200)
}) })
}) })
@@ -2841,13 +2702,7 @@ describe("/store/carts", () => {
await cartSeeder(dbConnection) await cartSeeder(dbConnection)
}) })
afterEach(async () => {
await doAfterEach()
})
it("updates region only - single to multiple countries", async () => { it("updates region only - single to multiple countries", async () => {
const api = useApi()
const { data, status } = await api const { data, status } = await api
.post(`/store/carts/test-cart`, { .post(`/store/carts/test-cart`, {
region_id: `test-region-multiple`, region_id: `test-region-multiple`,
@@ -2868,8 +2723,6 @@ describe("/store/carts", () => {
}) })
it("should reset the shipping_address on null value", async () => { it("should reset the shipping_address on null value", async () => {
const api = useApi()
const { data, status } = await api const { data, status } = await api
.post(`/store/carts/test-cart`, { .post(`/store/carts/test-cart`, {
shipping_address: null, shipping_address: null,
@@ -2885,10 +2738,6 @@ describe("/store/carts", () => {
}) })
describe("ignore region update if value didn't change", () => { describe("ignore region update if value didn't change", () => {
afterEach(async () => {
await doAfterEach()
})
it("has no side-effects when updating region with no change", async () => { it("has no side-effects when updating region with no change", async () => {
const region = await simpleRegionFactory(dbConnection, { const region = await simpleRegionFactory(dbConnection, {
countries: ["us", "ca"], countries: ["us", "ca"],
@@ -2908,8 +2757,6 @@ describe("/store/carts", () => {
}, },
}) })
const api = useApi()
const { data: preconditionData } = await api.get( const { data: preconditionData } = await api.get(
`/store/carts/${cart.id}` `/store/carts/${cart.id}`
) )
@@ -2928,10 +2775,6 @@ describe("/store/carts", () => {
}) })
describe("calculated prices for shipping option", () => { describe("calculated prices for shipping option", () => {
afterEach(async () => {
await doAfterEach()
})
it("it fetches prices with calculated amount", async () => { it("it fetches prices with calculated amount", async () => {
const region = await simpleRegionFactory(dbConnection) const region = await simpleRegionFactory(dbConnection)
const product = await simpleProductFactory(dbConnection) const product = await simpleProductFactory(dbConnection)
@@ -2951,8 +2794,6 @@ describe("/store/carts", () => {
data: { price: 123 }, data: { price: 123 },
}) })
const api = useApi()
const { data, status } = await api const { data, status } = await api
.get(`/store/shipping-options/${cart.id}`) .get(`/store/shipping-options/${cart.id}`)
.catch((err) => { .catch((err) => {
@@ -2966,10 +2807,6 @@ describe("/store/carts", () => {
}) })
describe("mix of calculated and flat rate prices", () => { describe("mix of calculated and flat rate prices", () => {
afterEach(async () => {
await doAfterEach()
})
it("it fetches prices with calculated amount", async () => { it("it fetches prices with calculated amount", async () => {
const region = await simpleRegionFactory(dbConnection) const region = await simpleRegionFactory(dbConnection)
const product = await simpleProductFactory(dbConnection) const product = await simpleProductFactory(dbConnection)
@@ -2994,8 +2831,6 @@ describe("/store/carts", () => {
data: { price: 123 }, data: { price: 123 },
}) })
const api = useApi()
const { data, status } = await api const { data, status } = await api
.get(`/store/shipping-options/${cart.id}`) .get(`/store/shipping-options/${cart.id}`)
.catch((err) => { .catch((err) => {
@@ -3018,4 +2853,5 @@ describe("/store/carts", () => {
) )
}) })
}) })
},
}) })
@@ -16,9 +16,9 @@ import {
simpleSalesChannelFactory, simpleSalesChannelFactory,
} from "./simple-sales-channel-factory" } from "./simple-sales-channel-factory"
import { DataSource } from "typeorm"
import faker from "faker"
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import faker from "faker"
import { DataSource } from "typeorm"
export type ProductFactoryData = { export type ProductFactoryData = {
id?: string id?: string