tests: integration tests

This commit is contained in:
zakariaelas committed 2021-09-30 14:52:43 +01:00
1 parent 77ee0bf186
commit 79c601279e
5 files changed
+837 -650

No files matched your search

File diff suppressed because it is too large. Load diff
+233 -188
View File
@@ -1,155 +1,162 @@
const path = require("path"); const path = require("path")
const { Region, LineItem, GiftCard } = require("@medusajs/medusa"); const {
Region,
LineItem,
GiftCard,
RMAShippingOption,
Cart,
} = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server"); const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api"); const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db"); const { initDb, useDb } = require("../../../helpers/use-db")
const cartSeeder = require("../../helpers/cart-seeder"); const cartSeeder = require("../../helpers/cart-seeder")
const swapSeeder = require("../../helpers/swap-seeder")
jest.setTimeout(30000); jest.setTimeout(30000)
describe("/store/carts", () => { describe("/store/carts", () => {
let medusaProcess; let medusaProcess
let dbConnection; let dbConnection
const doAfterEach = async () => { const doAfterEach = async () => {
const db = useDb(); const db = useDb()
return await db.teardown(); return await db.teardown()
}; }
beforeAll(async () => { beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..")); const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd }); dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd }); medusaProcess = await setupServer({ cwd })
}); })
afterAll(async () => { afterAll(async () => {
const db = useDb(); const db = useDb()
await db.shutdown(); await db.shutdown()
medusaProcess.kill(); medusaProcess.kill()
}); })
describe("POST /store/carts", () => { describe("POST /store/carts", () => {
beforeEach(async () => { beforeEach(async () => {
const manager = dbConnection.manager; const manager = dbConnection.manager
await manager.insert(Region, { await manager.insert(Region, {
id: "region", id: "region",
name: "Test Region", name: "Test Region",
currency_code: "usd", currency_code: "usd",
tax_rate: 0, tax_rate: 0,
}); })
await manager.query( await manager.query(
`UPDATE "country" SET region_id='region' WHERE iso_2 = 'us'` `UPDATE "country" SET region_id='region' WHERE iso_2 = 'us'`
); )
}); })
afterEach(async () => { afterEach(async () => {
await doAfterEach(); await doAfterEach()
}); })
it("creates a cart", async () => { it("creates a cart", async () => {
const api = useApi(); 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)
const getRes = await api.post(`/store/carts/${response.data.cart.id}`); const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
expect(getRes.status).toEqual(200); expect(getRes.status).toEqual(200)
}); })
it("creates a cart with country", async () => { it("creates a cart with country", async () => {
const api = useApi(); const api = useApi()
const response = await api.post("/store/carts", { const response = await api.post("/store/carts", {
country_code: "us", country_code: "us",
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
expect(response.data.cart.shipping_address.country_code).toEqual("us"); expect(response.data.cart.shipping_address.country_code).toEqual("us")
const getRes = await api.post(`/store/carts/${response.data.cart.id}`); const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
expect(getRes.status).toEqual(200); expect(getRes.status).toEqual(200)
}); })
it("creates a cart with context", async () => { it("creates a cart with context", async () => {
const api = useApi(); 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",
}, },
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
const getRes = await api.post(`/store/carts/${response.data.cart.id}`); const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
expect(getRes.status).toEqual(200); expect(getRes.status).toEqual(200)
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: "::ffff:127.0.0.1",
user_agent: "axios/0.21.1", user_agent: "axios/0.21.1",
test_id: "test", test_id: "test",
}); })
}); })
}); })
describe("POST /store/carts/:id", () => { describe("POST /store/carts/:id", () => {
beforeEach(async () => { beforeEach(async () => {
try { try {
await cartSeeder(dbConnection); await cartSeeder(dbConnection)
} catch (err) { } catch (err) {
console.log(err); console.log(err)
throw err; throw err
} }
}); })
afterEach(async () => { afterEach(async () => {
await doAfterEach(); await doAfterEach()
}); })
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 api = useApi()
try { try {
await api.post("/store/carts/test-cart", { await api.post("/store/carts/test-cart", {
discounts: [{ code: "CREATED" }], discounts: [{ code: "CREATED" }],
}); })
} catch (error) { } catch (error) {
expect(error.response.status).toEqual(400); expect(error.response.status).toEqual(400)
expect(error.response.data.message).toEqual( expect(error.response.data.message).toEqual(
"Discount has been used maximum allowed times" "Discount has been used maximum allowed times"
); )
} }
}); })
it("updates cart customer id", async () => { it("updates cart customer id", async () => {
const api = useApi(); 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",
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
}); })
it("updates address using string id", async () => { it("updates address using string id", async () => {
const api = useApi(); 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",
}); })
expect(response.data.cart.shipping_address_id).toEqual( expect(response.data.cart.shipping_address_id).toEqual(
"test-general-address" "test-general-address"
); )
expect(response.data.cart.billing_address_id).toEqual( expect(response.data.cart.billing_address_id).toEqual(
"test-general-address" "test-general-address"
); )
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
}); })
it("updates address", async () => { it("updates address", async () => {
const api = useApi(); 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: {
@@ -160,14 +167,14 @@ describe("/store/carts", () => {
country_code: "us", country_code: "us",
postal_code: "something", postal_code: "something",
}, },
}); })
expect(response.data.cart.shipping_address.first_name).toEqual("clark"); expect(response.data.cart.shipping_address.first_name).toEqual("clark")
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
}); })
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(); const api = useApi()
let cart = await api.post( let cart = await api.post(
"/store/carts/test-cart", "/store/carts/test-cart",
@@ -175,10 +182,10 @@ describe("/store/carts", () => {
discounts: [{ code: "FREE_SHIPPING" }, { code: "CREATED" }], discounts: [{ code: "FREE_SHIPPING" }, { code: "CREATED" }],
}, },
{ withCredentials: true } { withCredentials: true }
); )
expect(cart.data.cart.shipping_total).toBe(0); expect(cart.data.cart.shipping_total).toBe(0)
expect(cart.status).toEqual(200); expect(cart.status).toEqual(200)
cart = await api.post( cart = await api.post(
"/store/carts/test-cart", "/store/carts/test-cart",
@@ -186,68 +193,68 @@ describe("/store/carts", () => {
discounts: [{ code: "CREATED" }], discounts: [{ code: "CREATED" }],
}, },
{ withCredentials: true } { withCredentials: true }
); )
expect(cart.data.cart.shipping_total).toBe(1000); expect(cart.data.cart.shipping_total).toBe(1000)
expect(cart.status).toEqual(200); expect(cart.status).toEqual(200)
}); })
it("complete cart with giftcard total 0", async () => { it("complete cart with giftcard total 0", async () => {
const manager = dbConnection.manager; const manager = dbConnection.manager
await manager.insert(GiftCard, { await manager.insert(GiftCard, {
id: "gift_test", id: "gift_test",
code: "GC_TEST", code: "GC_TEST",
value: 20000, value: 20000,
balance: 20000, balance: 20000,
region_id: "test-region", region_id: "test-region",
}); })
const api = useApi(); 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" }],
}); })
const getRes = await api const getRes = await api
.post(`/store/carts/test-cart-3/complete`) .post(`/store/carts/test-cart-3/complete`)
.catch((err) => { .catch((err) => {
console.log(err.response.data); console.log(err.response.data)
}); })
expect(getRes.status).toEqual(200); expect(getRes.status).toEqual(200)
expect(getRes.data.type).toEqual("order"); expect(getRes.data.type).toEqual("order")
}); })
it("complete cart with items inventory covered", async () => { it("complete cart with items inventory covered", async () => {
const api = useApi(); 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)
const variantRes = await api.get("/store/variants/test-variant"); const variantRes = await api.get("/store/variants/test-variant")
expect(variantRes.data.variant.inventory_quantity).toEqual(0); expect(variantRes.data.variant.inventory_quantity).toEqual(0)
}); })
it("returns early, if cart is already completed", async () => { it("returns early, if cart is already completed", async () => {
const manager = dbConnection.manager; const manager = dbConnection.manager
const api = useApi(); const api = useApi()
await manager.query( await manager.query(
`UPDATE "cart" SET completed_at=current_timestamp WHERE id = 'test-cart-2'` `UPDATE "cart" SET completed_at=current_timestamp WHERE id = 'test-cart-2'`
); )
try { try {
await api.post(`/store/carts/test-cart-2/complete-cart`); await api.post(`/store/carts/test-cart-2/complete-cart`)
} catch (error) { } catch (error) {
expect(error.response.data).toMatchSnapshot({ expect(error.response.data).toMatchSnapshot({
code: "not_allowed", code: "not_allowed",
message: "Cart has already been completed", message: "Cart has already been completed",
code: "cart_incompatible_state", code: "cart_incompatible_state",
}); })
expect(error.response.status).toEqual(409); expect(error.response.status).toEqual(409)
} }
}); })
it("fails to complete cart with items inventory not/partially covered", async () => { it("fails to complete cart with items inventory not/partially covered", async () => {
const manager = dbConnection.manager; const manager = dbConnection.manager
const li = manager.create(LineItem, { const li = manager.create(LineItem, {
id: "test-item", id: "test-item",
@@ -258,37 +265,58 @@ describe("/store/carts", () => {
quantity: 99, quantity: 99,
variant_id: "test-variant-2", variant_id: "test-variant-2",
cart_id: "test-cart-2", cart_id: "test-cart-2",
}); })
await manager.save(li); await manager.save(li)
const api = useApi(); 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) {
expect(e.response.data).toMatchSnapshot({ expect(e.response.data).toMatchSnapshot({
code: "insufficient_inventory", code: "insufficient_inventory",
}); })
expect(e.response.status).toBe(409); expect(e.response.status).toBe(409)
} }
//check to see if payment has been cancelled //check to see if payment has been cancelled
const res = await api.get(`/store/carts/test-cart-2`); const res = await api.get(`/store/carts/test-cart-2`)
expect(res.data.cart.payment.canceled_at).not.toBe(null); expect(res.data.cart.payment.canceled_at).not.toBe(null)
}); })
}); })
describe("POST /store/carts/:id/shipping-methods", () => { describe("POST /store/carts/:id/shipping-methods", () => {
beforeEach(async () => { beforeEach(async () => {
await cartSeeder(dbConnection); await cartSeeder(dbConnection)
}); const manager = dbConnection.manager
await manager.insert(Cart, {
id: "test-cart-rma",
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",
})
await manager.insert(RMAShippingOption, {
id: "test-rmaso",
shipping_option_id: "test-option",
price: 5,
})
})
afterEach(async () => { afterEach(async () => {
await doAfterEach(); await doAfterEach()
}); })
it("adds a shipping method to cart", async () => { it("adds a normal shipping method to cart", async () => {
const api = useApi(); 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",
@@ -296,16 +324,37 @@ describe("/store/carts", () => {
option_id: "test-option", option_id: "test-option",
}, },
{ 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)
}); })
it("adds a rma shipping method to cart", async () => {
const api = useApi()
const cartWithRMAShippingMethod = await api
.post(
"/store/carts/test-cart-rma/shipping-methods",
{
option_id: "test-rmaso",
},
{ withCredentials: true }
)
.catch((err) => err.response)
expect(
cartWithRMAShippingMethod.data.cart.shipping_methods
).toContainEqual(
expect.objectContaining({ shipping_option_id: "test-option", price: 5 })
)
expect(cartWithRMAShippingMethod.status).toEqual(200)
})
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(); const api = useApi()
// Add standard line item to cart // Add standard line item to cart
await api.post( await api.post(
@@ -315,7 +364,7 @@ describe("/store/carts", () => {
quantity: 1, quantity: 1,
}, },
{ withCredentials: true } { withCredentials: true }
); )
// Add gift card to cart // Add gift card to cart
await api.post( await api.post(
@@ -325,7 +374,7 @@ describe("/store/carts", () => {
quantity: 1, quantity: 1,
}, },
{ withCredentials: true } { withCredentials: true }
); )
// Add a 10% discount to the cart // Add a 10% discount to the cart
const cartWithGiftcard = await api.post( const cartWithGiftcard = await api.post(
@@ -334,16 +383,16 @@ describe("/store/carts", () => {
discounts: [{ code: "10PERCENT" }], discounts: [{ code: "10PERCENT" }],
}, },
{ withCredentials: true } { withCredentials: true }
); )
// Ensure that the discount is only applied to the standard item // Ensure that the discount is only applied to the standard item
expect(cartWithGiftcard.data.cart.total).toBe(1900); // 1000 (giftcard) + 900 (standard item with 10% discount) expect(cartWithGiftcard.data.cart.total).toBe(1900) // 1000 (giftcard) + 900 (standard item with 10% discount)
expect(cartWithGiftcard.data.cart.discount_total).toBe(100); expect(cartWithGiftcard.data.cart.discount_total).toBe(100)
expect(cartWithGiftcard.status).toEqual(200); expect(cartWithGiftcard.status).toEqual(200)
}); })
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 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",
@@ -351,17 +400,17 @@ describe("/store/carts", () => {
option_id, option_id,
}, },
{ withCredentials: true } { withCredentials: true }
); )
}; }
await addShippingMethod("test-option"); await addShippingMethod("test-option")
const cartWithAnotherShippingMethod = await addShippingMethod( const cartWithAnotherShippingMethod = await addShippingMethod(
"test-option-2" "test-option-2"
); )
expect( expect(
cartWithAnotherShippingMethod.data.cart.shipping_methods.length cartWithAnotherShippingMethod.data.cart.shipping_methods.length
).toEqual(1); ).toEqual(1)
expect( expect(
cartWithAnotherShippingMethod.data.cart.shipping_methods cartWithAnotherShippingMethod.data.cart.shipping_methods
).toContainEqual( ).toContainEqual(
@@ -369,30 +418,30 @@ describe("/store/carts", () => {
shipping_option_id: "test-option-2", shipping_option_id: "test-option-2",
price: 500, price: 500,
}) })
); )
expect(cartWithAnotherShippingMethod.status).toEqual(200); expect(cartWithAnotherShippingMethod.status).toEqual(200)
}); })
}); })
describe("DELETE /store/carts/:id/discounts/:code", () => { describe("DELETE /store/carts/:id/discounts/:code", () => {
beforeEach(async () => { beforeEach(async () => {
try { try {
await cartSeeder(dbConnection); await cartSeeder(dbConnection)
await dbConnection.manager.query( await dbConnection.manager.query(
`INSERT INTO "cart_discounts" (cart_id, discount_id) VALUES ('test-cart', 'free-shipping')` `INSERT INTO "cart_discounts" (cart_id, discount_id) VALUES ('test-cart', 'free-shipping')`
); )
} catch (err) { } catch (err) {
console.log(err); console.log(err)
throw err; throw err
} }
}); })
afterEach(async () => { afterEach(async () => {
await doAfterEach(); await doAfterEach()
}); })
it("removes free shipping and updates shipping total", async () => { it("removes free shipping and updates shipping total", async () => {
const api = useApi(); const api = useApi()
const cartWithFreeShipping = await api.post( const cartWithFreeShipping = await api.post(
"/store/carts/test-cart", "/store/carts/test-cart",
@@ -400,36 +449,36 @@ describe("/store/carts", () => {
discounts: [{ code: "FREE_SHIPPING" }], discounts: [{ code: "FREE_SHIPPING" }],
}, },
{ withCredentials: true } { withCredentials: true }
); )
expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0); expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0)
expect(cartWithFreeShipping.status).toEqual(200); expect(cartWithFreeShipping.status).toEqual(200)
const response = await api.delete( const response = await api.delete(
"/store/carts/test-cart/discounts/FREE_SHIPPING" "/store/carts/test-cart/discounts/FREE_SHIPPING"
); )
expect(response.data.cart.shipping_total).toBe(1000); expect(response.data.cart.shipping_total).toBe(1000)
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
}); })
}); })
describe("get-cart with session customer", () => { describe("get-cart with session customer", () => {
beforeEach(async () => { beforeEach(async () => {
try { try {
await cartSeeder(dbConnection); await cartSeeder(dbConnection)
} catch (err) { } catch (err) {
console.log(err); console.log(err)
throw err; throw err
} }
}); })
afterEach(async () => { afterEach(async () => {
await doAfterEach(); 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 api = useApi()
let customer = await api.post( let customer = await api.post(
"/store/customers", "/store/customers",
@@ -440,29 +489,25 @@ describe("/store/carts", () => {
last_name: "oli", last_name: "oli",
}, },
{ withCredentials: true } { withCredentials: true }
); )
const cookie = customer.headers["set-cookie"][0]; const cookie = customer.headers["set-cookie"][0]
const cart = await api.post( const cart = await api.post("/store/carts", {}, { withCredentials: true })
"/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: {
cookie, cookie,
}, },
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 api = useApi()
let customer = await api.post( let customer = await api.post(
"/store/customers", "/store/customers",
@@ -473,15 +518,15 @@ describe("/store/carts", () => {
last_name: "oli", last_name: "oli",
}, },
{ withCredentials: true } { withCredentials: true }
); )
const cookie = customer.headers["set-cookie"][0]; const cookie = customer.headers["set-cookie"][0]
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}`,
@@ -490,10 +535,10 @@ describe("/store/carts", () => {
cookie, cookie,
}, },
} }
); )
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)
}); })
}); })
}); })
@@ -1,51 +1,53 @@
const path = require("path"); const path = require("path")
const { Region, ShippingProfile, ShippingOption } = require("@medusajs/medusa"); const { Region, ShippingProfile, ShippingOption } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server"); const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api"); const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db"); const { initDb, useDb } = require("../../../helpers/use-db")
const cartSeeder = require("../../helpers/cart-seeder")
const swapSeeder = require("../../helpers/swap-seeder")
jest.setTimeout(30000); jest.setTimeout(30000)
describe("/store/shipping-options", () => { describe("/store/shipping-options", () => {
let medusaProcess; let medusaProcess
let dbConnection; let dbConnection
beforeAll(async () => { beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..")); const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd }); dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd }); medusaProcess = await setupServer({ cwd })
}); })
afterAll(async () => { afterAll(async () => {
const db = useDb(); const db = useDb()
await db.shutdown(); await db.shutdown()
medusaProcess.kill(); medusaProcess.kill()
}); })
describe("POST /store/shipping-options", () => { describe("POST /store/shipping-options", () => {
beforeEach(async () => { beforeEach(async () => {
const manager = dbConnection.manager; const manager = dbConnection.manager
await manager.query( await manager.query(
`ALTER SEQUENCE order_display_id_seq RESTART WITH 111` `ALTER SEQUENCE order_display_id_seq RESTART WITH 111`
); )
await manager.insert(Region, { await manager.insert(Region, {
id: "region", id: "region",
name: "Test Region", name: "Test Region",
currency_code: "usd", currency_code: "usd",
tax_rate: 0, tax_rate: 0,
}); })
await manager.insert(Region, { await manager.insert(Region, {
id: "region2", id: "region2",
name: "Test Region 2", name: "Test Region 2",
currency_code: "usd", currency_code: "usd",
tax_rate: 0, tax_rate: 0,
}); })
const defaultProfile = await manager.findOne(ShippingProfile, { const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default", type: "default",
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "test-out", id: "test-out",
@@ -57,7 +59,7 @@ describe("/store/shipping-options", () => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 2000, amount: 2000,
is_return: false, is_return: false,
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "test-return", id: "test-return",
@@ -69,7 +71,7 @@ describe("/store/shipping-options", () => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 1000, amount: 1000,
is_return: true, is_return: true,
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "test-region2", id: "test-region2",
@@ -81,51 +83,101 @@ describe("/store/shipping-options", () => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 1000, amount: 1000,
is_return: false, is_return: false,
}); })
}); })
afterEach(async () => { afterEach(async () => {
const db = useDb(); const db = useDb()
await db.teardown(); await db.teardown()
}); })
it("retrieves all shipping options", async () => { it("retrieves all shipping options", async () => {
const api = useApi(); const api = useApi()
const response = await api.get("/store/shipping-options").catch((err) => { const response = await api.get("/store/shipping-options").catch((err) => {
return err.response; return err.response
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
expect(response.data.shipping_options.length).toEqual(3); expect(response.data.shipping_options.length).toEqual(3)
}); })
it("creates a return with shipping method", async () => { it("creates a return with shipping method", async () => {
const api = useApi(); const api = useApi()
const response = await api const response = await api
.get("/store/shipping-options?is_return=true") .get("/store/shipping-options?is_return=true")
.catch((err) => { .catch((err) => {
return err.response; return err.response
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
expect(response.data.shipping_options.length).toEqual(1); expect(response.data.shipping_options.length).toEqual(1)
expect(response.data.shipping_options[0].id).toEqual("test-return"); expect(response.data.shipping_options[0].id).toEqual("test-return")
}); })
it("creates a return with shipping method", async () => { it("creates a return with shipping method", async () => {
const api = useApi(); const api = useApi()
const response = await api const response = await api
.get("/store/shipping-options?region_id=region2") .get("/store/shipping-options?region_id=region2")
.catch((err) => { .catch((err) => {
return err.response; return err.response
}); })
expect(response.status).toEqual(200); expect(response.status).toEqual(200)
expect(response.data.shipping_options.length).toEqual(1); expect(response.data.shipping_options.length).toEqual(1)
expect(response.data.shipping_options[0].id).toEqual("test-region2"); expect(response.data.shipping_options[0].id).toEqual("test-region2")
}); })
}); })
});
describe("GET /store/shipping-options/:cart_id", () => {
beforeEach(async () => {
await cartSeeder(dbConnection)
await swapSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("given a default cart, when user retrieves its shipping options, then should return a list of shipping options", async () => {
const api = useApi()
const response = await api
.get("/store/shipping-options/test-cart-2")
.catch((err) => {
return err.response
})
expect(response.status).toEqual(200)
expect(response.data.shipping_options).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: "test-option", amount: 1000 }),
expect.objectContaining({ id: "test-option-2", amount: 500 }),
])
)
})
it("given a swap cart, when user retrieves its shipping options, then should return a list of RMA shipping options", async () => {
const api = useApi()
const response = await api
.get("/store/shipping-options/test-cart-rma")
.catch((err) => {
return err.response
})
expect(response.status).toEqual(200)
expect(response.data.shipping_options).toEqual(
expect.arrayContaining([
expect.objectContaining({
shipping_option_id: "test-option",
price: 0,
}),
])
)
})
})
})
+56 -56
View File
@@ -14,31 +14,31 @@ const {
LineItem, LineItem,
Payment, Payment,
PaymentSession, PaymentSession,
} = require("@medusajs/medusa"); } = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => { module.exports = async (connection, data = {}) => {
const manager = connection.manager; const manager = connection.manager
const defaultProfile = await manager.findOne(ShippingProfile, { const defaultProfile = await manager.findOne(ShippingProfile, {
type: "default", type: "default",
}); })
const gcProfile = await manager.findOne(ShippingProfile, { const gcProfile = await manager.findOne(ShippingProfile, {
type: "gift_card", type: "gift_card",
}); })
await manager.insert(Address, { await manager.insert(Address, {
id: "test-general-address", id: "test-general-address",
first_name: "superman", first_name: "superman",
country_code: "us", country_code: "us",
}); })
const r = manager.create(Region, { const r = manager.create(Region, {
id: "test-region", id: "test-region",
name: "Test Region", name: "Test Region",
currency_code: "usd", currency_code: "usd",
tax_rate: 0, tax_rate: 0,
}); })
const freeRule = manager.create(DiscountRule, { const freeRule = manager.create(DiscountRule, {
id: "free-shipping-rule", id: "free-shipping-rule",
@@ -46,18 +46,18 @@ module.exports = async (connection, data = {}) => {
type: "free_shipping", type: "free_shipping",
value: 100, value: 100,
allocation: "total", allocation: "total",
}); })
const freeDisc = manager.create(Discount, { const freeDisc = manager.create(Discount, {
id: "free-shipping", id: "free-shipping",
code: "FREE_SHIPPING", code: "FREE_SHIPPING",
is_dynamic: false, is_dynamic: false,
is_disabled: false, is_disabled: false,
}); })
freeDisc.regions = [r]; freeDisc.regions = [r]
freeDisc.rule = freeRule; freeDisc.rule = freeRule
await manager.save(freeDisc); await manager.save(freeDisc)
const tenPercentRule = manager.create(DiscountRule, { const tenPercentRule = manager.create(DiscountRule, {
id: "tenpercent-rule", id: "tenpercent-rule",
@@ -65,25 +65,25 @@ module.exports = async (connection, data = {}) => {
type: "percentage", type: "percentage",
value: 10, value: 10,
allocation: "total", allocation: "total",
}); })
const tenPercent = manager.create(Discount, { const tenPercent = manager.create(Discount, {
id: "10Percent", id: "10Percent",
code: "10PERCENT", code: "10PERCENT",
is_dynamic: false, is_dynamic: false,
is_disabled: false, is_disabled: false,
}); })
tenPercent.regions = [r]; tenPercent.regions = [r]
tenPercent.rule = tenPercentRule; tenPercent.rule = tenPercentRule
await manager.save(tenPercent); await manager.save(tenPercent)
const d = await manager.create(Discount, { const d = await manager.create(Discount, {
id: "test-discount", id: "test-discount",
code: "CREATED", code: "CREATED",
is_dynamic: false, is_dynamic: false,
is_disabled: false, is_disabled: false,
}); })
const dr = await manager.create(DiscountRule, { const dr = await manager.create(DiscountRule, {
id: "test-discount-rule", id: "test-discount-rule",
@@ -91,31 +91,31 @@ module.exports = async (connection, data = {}) => {
type: "fixed", type: "fixed",
value: 10000, value: 10000,
allocation: "total", allocation: "total",
}); })
d.rule = dr; d.rule = dr
d.regions = [r]; d.regions = [r]
await manager.save(d); await manager.save(d)
await manager.query( await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'` `UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
); )
await manager.insert(Customer, { await manager.insert(Customer, {
id: "test-customer", id: "test-customer",
email: "test@email.com", email: "test@email.com",
}); })
await manager.insert(Customer, { await manager.insert(Customer, {
id: "test-customer-2", id: "test-customer-2",
email: "test-2@email.com", email: "test-2@email.com",
}); })
await manager.insert(Customer, { await manager.insert(Customer, {
id: "some-customer", id: "some-customer",
email: "some-customer@email.com", email: "some-customer@email.com",
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "test-option", id: "test-option",
@@ -126,7 +126,7 @@ module.exports = async (connection, data = {}) => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 1000, amount: 1000,
data: {}, data: {},
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "gc-option", id: "gc-option",
@@ -137,7 +137,7 @@ module.exports = async (connection, data = {}) => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 0, amount: 0,
data: {}, data: {},
}); })
await manager.insert(ShippingOption, { await manager.insert(ShippingOption, {
id: "test-option-2", id: "test-option-2",
@@ -148,7 +148,7 @@ module.exports = async (connection, data = {}) => {
price_type: "flat_rate", price_type: "flat_rate",
amount: 500, amount: 500,
data: {}, data: {},
}); })
await manager.insert(Product, { await manager.insert(Product, {
id: "giftcard-product", id: "giftcard-product",
@@ -157,7 +157,7 @@ module.exports = async (connection, data = {}) => {
discountable: false, discountable: false,
profile_id: gcProfile.id, profile_id: gcProfile.id,
options: [{ id: "denom", title: "Denomination" }], options: [{ id: "denom", title: "Denomination" }],
}); })
await manager.insert(ProductVariant, { await manager.insert(ProductVariant, {
id: "giftcard-denom", id: "giftcard-denom",
@@ -170,14 +170,14 @@ module.exports = async (connection, data = {}) => {
value: "1000", value: "1000",
}, },
], ],
}); })
await manager.insert(Product, { await manager.insert(Product, {
id: "test-product", id: "test-product",
title: "test product", title: "test product",
profile_id: defaultProfile.id, profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }], options: [{ id: "test-option", title: "Size" }],
}); })
await manager.insert(ProductVariant, { await manager.insert(ProductVariant, {
id: "test-variant", id: "test-variant",
@@ -190,7 +190,7 @@ module.exports = async (connection, data = {}) => {
value: "Size", value: "Size",
}, },
], ],
}); })
await manager.insert(ProductVariant, { await manager.insert(ProductVariant, {
id: "test-variant-2", id: "test-variant-2",
@@ -203,31 +203,31 @@ module.exports = async (connection, data = {}) => {
value: "Size", value: "Size",
}, },
], ],
}); })
const ma = manager.create(MoneyAmount, { const ma = manager.create(MoneyAmount, {
variant_id: "test-variant", variant_id: "test-variant",
currency_code: "usd", currency_code: "usd",
amount: 1000, amount: 1000,
}); })
await manager.save(ma); await manager.save(ma)
const ma2 = manager.create(MoneyAmount, { const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2", variant_id: "test-variant-2",
currency_code: "usd", currency_code: "usd",
amount: 8000, amount: 8000,
}); })
await manager.save(ma2); await manager.save(ma2)
const ma3 = manager.create(MoneyAmount, { const ma3 = manager.create(MoneyAmount, {
variant_id: "giftcard-denom", variant_id: "giftcard-denom",
currency_code: "usd", currency_code: "usd",
amount: 1000, amount: 1000,
}); })
await manager.save(ma3); await manager.save(ma3)
const cart = manager.create(Cart, { const cart = manager.create(Cart, {
id: "test-cart", id: "test-cart",
@@ -241,9 +241,9 @@ module.exports = async (connection, data = {}) => {
region_id: "test-region", region_id: "test-region",
currency_code: "usd", currency_code: "usd",
items: [], items: [],
}); })
await manager.save(cart); await manager.save(cart)
const cart2 = manager.create(Cart, { const cart2 = manager.create(Cart, {
id: "test-cart-2", id: "test-cart-2",
@@ -258,7 +258,7 @@ module.exports = async (connection, data = {}) => {
currency_code: "usd", currency_code: "usd",
completed_at: null, completed_at: null,
items: [], items: [],
}); })
const pay = manager.create(Payment, { const pay = manager.create(Payment, {
id: "test-payment", id: "test-payment",
@@ -267,13 +267,13 @@ module.exports = async (connection, data = {}) => {
amount_refunded: 0, amount_refunded: 0,
provider_id: "test-pay", provider_id: "test-pay",
data: {}, data: {},
}); })
await manager.save(pay); await manager.save(pay)
cart2.payment = pay; cart2.payment = pay
await manager.save(cart2); await manager.save(cart2)
await manager.insert(PaymentSession, { await manager.insert(PaymentSession, {
id: "test-session", id: "test-session",
@@ -282,7 +282,7 @@ module.exports = async (connection, data = {}) => {
is_selected: true, is_selected: true,
data: {}, data: {},
status: "authorized", status: "authorized",
}); })
await manager.insert(ShippingMethod, { await manager.insert(ShippingMethod, {
id: "test-method", id: "test-method",
@@ -290,7 +290,7 @@ module.exports = async (connection, data = {}) => {
cart_id: "test-cart", cart_id: "test-cart",
price: 1000, price: 1000,
data: {}, data: {},
}); })
const li = manager.create(LineItem, { const li = manager.create(LineItem, {
id: "test-item", id: "test-item",
@@ -301,8 +301,8 @@ module.exports = async (connection, data = {}) => {
quantity: 1, quantity: 1,
variant_id: "test-variant", variant_id: "test-variant",
cart_id: "test-cart-2", cart_id: "test-cart-2",
}); })
await manager.save(li); await manager.save(li)
const cart3 = manager.create(Cart, { const cart3 = manager.create(Cart, {
id: "test-cart-3", id: "test-cart-3",
@@ -317,8 +317,8 @@ module.exports = async (connection, data = {}) => {
currency_code: "usd", currency_code: "usd",
completed_at: null, completed_at: null,
items: [], items: [],
}); })
await manager.save(cart3); await manager.save(cart3)
await manager.insert(ShippingMethod, { await manager.insert(ShippingMethod, {
id: "test-method-2", id: "test-method-2",
@@ -326,7 +326,7 @@ module.exports = async (connection, data = {}) => {
cart_id: "test-cart-3", cart_id: "test-cart-3",
price: 0, price: 0,
data: {}, data: {},
}); })
const li2 = manager.create(LineItem, { const li2 = manager.create(LineItem, {
id: "test-item-2", id: "test-item-2",
@@ -337,6 +337,6 @@ module.exports = async (connection, data = {}) => {
quantity: 1, quantity: 1,
variant_id: "test-variant", variant_id: "test-variant",
cart_id: "test-cart-3", cart_id: "test-cart-3",
}); })
await manager.save(li2); await manager.save(li2)
}; }
+85 -31
View File
@@ -14,10 +14,11 @@ const {
Swap, Swap,
Cart, Cart,
Return, Return,
} = require("@medusajs/medusa"); RMAShippingOption,
} = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => { module.exports = async (connection, data = {}) => {
const manager = connection.manager; const manager = connection.manager
let orderWithSwap = manager.create(Order, { let orderWithSwap = manager.create(Order, {
id: "order-with-swap", id: "order-with-swap",
@@ -50,9 +51,9 @@ module.exports = async (connection, data = {}) => {
], ],
items: [], items: [],
...data, ...data,
}); })
orderWithSwap = await manager.save(orderWithSwap); orderWithSwap = await manager.save(orderWithSwap)
const cart = manager.create(Cart, { const cart = manager.create(Cart, {
id: "test-cart", id: "test-cart",
@@ -66,9 +67,9 @@ module.exports = async (connection, data = {}) => {
swap_id: "test-swap", swap_id: "test-swap",
parent_order_id: orderWithSwap.id, parent_order_id: orderWithSwap.id,
}, },
}); })
await manager.save(cart); await manager.save(cart)
const swap = manager.create(Swap, { const swap = manager.create(Swap, {
id: "test-swap", id: "test-swap",
@@ -97,9 +98,62 @@ module.exports = async (connection, data = {}) => {
cart_id: "test-cart", cart_id: "test-cart",
}, },
], ],
}); })
await manager.save(swap); await manager.save(swap)
const rmaCart = manager.create(Cart, {
id: "test-cart-rma",
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: "test-region",
type: "swap",
metadata: {
swap_id: "test-swap",
parent_order_id: orderWithSwap.id,
},
})
await manager.save(rmaCart)
const swapWithRMAMethod = manager.create(Swap, {
id: "test-swap-rma",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
cart_id: "test-cart-rma",
payment: {
id: "test-payment-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swapped",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 9000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart",
},
],
rma_shipping_options: [
{
shipping_option_id: "test-option",
price: 0,
},
],
})
await manager.save(swapWithRMAMethod)
const cartTemplate = async (cartId) => { const cartTemplate = async (cartId) => {
const cart = manager.create(Cart, { const cart = manager.create(Cart, {
@@ -112,13 +166,13 @@ module.exports = async (connection, data = {}) => {
type: "swap", type: "swap",
metadata: {}, metadata: {},
...data, ...data,
}); })
await manager.save(cart); await manager.save(cart)
}; }
const swapTemplate = async (cartId) => { const swapTemplate = async (cartId) => {
await cartTemplate(cartId); await cartTemplate(cartId)
return { return {
order_id: orderWithSwap.id, order_id: orderWithSwap.id,
fulfillment_status: "fulfilled", fulfillment_status: "fulfilled",
@@ -132,8 +186,8 @@ module.exports = async (connection, data = {}) => {
data: {}, data: {},
}, },
...data, ...data,
}; }
}; }
const swapWithFulfillments = manager.create(Swap, { const swapWithFulfillments = manager.create(Swap, {
id: "swap-w-f", id: "swap-w-f",
@@ -150,9 +204,9 @@ module.exports = async (connection, data = {}) => {
}, },
], ],
...(await swapTemplate("sc-w-f")), ...(await swapTemplate("sc-w-f")),
}); })
await manager.save(swapWithFulfillments); await manager.save(swapWithFulfillments)
const swapWithReturn = manager.create(Swap, { const swapWithReturn = manager.create(Swap, {
id: "swap-w-r", id: "swap-w-r",
@@ -162,9 +216,9 @@ module.exports = async (connection, data = {}) => {
refund_amount: 0, refund_amount: 0,
}, },
...(await swapTemplate("sc-w-r")), ...(await swapTemplate("sc-w-r")),
}); })
await manager.save(swapWithReturn); await manager.save(swapWithReturn)
const li = manager.create(LineItem, { const li = manager.create(LineItem, {
id: "return-item-1", id: "return-item-1",
fulfilled_quantity: 1, fulfilled_quantity: 1,
@@ -176,9 +230,9 @@ module.exports = async (connection, data = {}) => {
variant_id: "test-variant", variant_id: "test-variant",
order_id: orderWithSwap.id, order_id: orderWithSwap.id,
cart_id: cart.id, cart_id: cart.id,
}); })
await manager.save(li); await manager.save(li)
const li2 = manager.create(LineItem, { const li2 = manager.create(LineItem, {
id: "test-item-many", id: "test-item-many",
@@ -190,9 +244,9 @@ module.exports = async (connection, data = {}) => {
quantity: 4, quantity: 4,
variant_id: "test-variant", variant_id: "test-variant",
order_id: orderWithSwap.id, order_id: orderWithSwap.id,
}); })
await manager.save(li2); await manager.save(li2)
const swapReturn = await manager.create(Return, { const swapReturn = await manager.create(Return, {
swap_id: swap.id, swap_id: swap.id,
@@ -200,16 +254,16 @@ module.exports = async (connection, data = {}) => {
item_id: li.id, item_id: li.id,
refund_amount: li.quantity * li.unit_price, refund_amount: li.quantity * li.unit_price,
// shipping_method_id: , // shipping_method_id: ,
}); })
await manager.save(swapReturn); await manager.save(swapReturn)
const return_item1 = manager.create(LineItem, { const return_item1 = manager.create(LineItem, {
...li, ...li,
unit_price: -1 * li.unit_price, unit_price: -1 * li.unit_price,
}); })
await manager.save(return_item1); await manager.save(return_item1)
await manager.insert(ShippingMethod, { await manager.insert(ShippingMethod, {
id: "another-test-method", id: "another-test-method",
@@ -217,7 +271,7 @@ module.exports = async (connection, data = {}) => {
cart_id: "test-cart", cart_id: "test-cart",
price: 1000, price: 1000,
data: {}, data: {},
}); })
const swapOnSwap = manager.create(Swap, { const swapOnSwap = manager.create(Swap, {
id: "swap-on-swap", id: "swap-on-swap",
@@ -255,9 +309,9 @@ module.exports = async (connection, data = {}) => {
variant_id: "test-variant", variant_id: "test-variant",
}, },
], ],
}); })
await manager.save(swapOnSwap); await manager.save(swapOnSwap)
await manager.insert(ShippingMethod, { await manager.insert(ShippingMethod, {
id: "test-method-swap-order", id: "test-method-swap-order",
@@ -265,5 +319,5 @@ module.exports = async (connection, data = {}) => {
order_id: "order-with-swap", order_id: "order-with-swap",
price: 1000, price: 1000,
data: {}, data: {},
}); })
}; }