Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cart): Line items operations #6066

Merged
merged 15 commits into from
Jan 16, 2024
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { ICartModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { initialize } from "../../../../src/initialize"
import { DB_URL, MikroOrmWrapper } from "../../../utils"

jest.setTimeout(30000)

describe("Cart Module Service", () => {
let service: ICartModuleService
let repositoryManager: SqlEntityManager

beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()

service = await initialize({
database: {
Expand Down Expand Up @@ -242,4 +239,366 @@ describe("Cart Module Service", () => {
expect(address).toBe(undefined)
})
})

describe("addLineItems", () => {
it("should add a line item to cart succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const items = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
])

const cart = await service.retrieve(createdCart.id, {
relations: ["items"],
})

expect(items[0].id).toBe(cart.items![0].id)
})

it("should add multiple line items to cart succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const items = await service.addLineItems({
cart_id: createdCart.id,
items: [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
],
})

const cart = await service.retrieve(createdCart.id, {
relations: ["items"],
})

expect(items[0].id).toBe(cart.items![0].id)
expect(cart.items?.length).toBe(1)
})

it("should add multiple line items to multiple carts succesfully", async () => {
let [eurCart] = await service.create([
{
currency_code: "eur",
},
])

let [usdCart] = await service.create([
{
currency_code: "usd",
},
])

const items = await service.addLineItems([
{
cart_id: eurCart.id,
items: [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
],
},
{
cart_id: usdCart.id,
items: [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
],
},
])

const carts = await service.list(
{ id: [eurCart.id, usdCart.id] },
{ relations: ["items"] }
)

eurCart = carts.find((c) => c.currency_code === "eur")!
usdCart = carts.find((c) => c.currency_code === "usd")!

const eurItems = items.filter((i) => i.cart_id === eurCart.id)
const usdItems = items.filter((i) => i.cart_id === usdCart.id)

expect(eurCart.items![0].id).toBe(eurItems[0].id)
expect(usdCart.items![0].id).toBe(usdItems[0].id)

expect(eurCart.items?.length).toBe(1)
expect(usdCart.items?.length).toBe(1)
})

it("should throw if cart does not exist", async () => {
const error = await service
.addLineItems("foo", [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
])
.catch((e) => e)

expect(error.message).toContain("Cart with id: foo was not found")
})

it("should throw an error when required params are not passed", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const error = await service
.addLineItems(createdCart.id, [
{
quantity: 1,
title: "test",
},
] as any)
.catch((e) => e)

expect(error.message).toContain(
"Value for LineItem.unit_price is required, 'undefined' found"
)
})
})

describe("updateLineItems", () => {
it("should update a line item in cart succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [item] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
])

expect(item.title).toBe("test")

const [updatedItem] = await service.updateLineItems(createdCart.id, [
{
id: item.id,
title: "test2",
},
])

expect(updatedItem.title).toBe("test2")
})

it("should update multiples line items in cart succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const items = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
{
quantity: 2,
unit_price: 200,
title: "other-test",
},
])

expect(items).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "test",
quantity: 1,
unit_price: 100,
}),
expect.objectContaining({
title: "other-test",
quantity: 2,
unit_price: 200,
}),
])
)

const itemOne = items.find((i) => i.title === "test")
const itemTwo = items.find((i) => i.title === "other-test")

const updatedItems = await service.updateLineItems(createdCart.id, [
{
id: itemOne!.id,
title: "changed-test",
},
{
id: itemTwo!.id,
title: "changed-other-test",
},
])

expect(updatedItems).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "changed-test",
quantity: 1,
unit_price: 100,
}),
expect.objectContaining({
title: "changed-other-test",
quantity: 2,
unit_price: 200,
}),
])
)
})
})

describe("removeLineItems", () => {
it("should remove a line item succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [item] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
])

expect(item.title).toBe("test")

await service.removeLineItems([item.id])

const cart = await service.retrieve(createdCart.id, {
relations: ["items"],
})

expect(cart.items?.length).toBe(0)
})

it("should remove multiple line items succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [item, item2] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
{
quantity: 1,
unit_price: 100,
title: "test-2",
},
])

await service.removeLineItems([item.id, item2.id])

const cart = await service.retrieve(createdCart.id, {
relations: ["items"],
})

expect(cart.items?.length).toBe(0)
})

it("should update multiples line items in cart succesfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const items = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
{
quantity: 2,
unit_price: 200,
title: "other-test",
},
])

expect(items).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "test",
quantity: 1,
unit_price: 100,
}),
expect.objectContaining({
title: "other-test",
quantity: 2,
unit_price: 200,
}),
])
)

const itemOne = items.find((i) => i.title === "test")
const itemTwo = items.find((i) => i.title === "other-test")

const updatedItems = await service.updateLineItems(createdCart.id, [
{
id: itemOne!.id,
title: "changed-test",
},
{
id: itemTwo!.id,
title: "changed-other-test",
},
])

expect(updatedItems).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "changed-test",
quantity: 1,
unit_price: 100,
}),
expect.objectContaining({
title: "changed-other-test",
quantity: 2,
unit_price: 200,
}),
])
)
})
})
})
2 changes: 2 additions & 0 deletions packages/cart/src/loaders/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default async ({
container.register({
cartService: asClass(defaultServices.CartService).singleton(),
addressService: asClass(defaultServices.AddressService).singleton(),
lineItemService: asClass(defaultServices.LineItemService).singleton(),
})

if (customRepositories) {
Expand All @@ -38,5 +39,6 @@ function loadDefaultRepositories({ container }) {
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
cartRepository: asClass(defaultRepositories.CartRepository).singleton(),
addressRepository: asClass(defaultRepositories.AddressRepository).singleton(),
lineItemRepository: asClass(defaultRepositories.LineItemRepository).singleton(),
})
}
Loading