feat(cart): Line item adjustments (#6112)

This commit is contained in:
Oli Juhl authored and GitHub committed 2024-01-22 09:55:47 +01:00
1 parent af7af73745
commit 06b33a9b45
21 files changed
+974 -71

No files matched your search

@@ -799,4 +799,515 @@ describe("Cart Module Service", () => {
expect(cart.shipping_methods?.length).toBe(0)
})
})
describe("setLineItemAdjustments", () => {
it("should set line item adjustments for a cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const [itemTwo] = await service.addLineItems(createdCart.id, [
{
quantity: 2,
unit_price: 200,
title: "test-2",
},
])
const adjustments = await service.setLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
{
item_id: itemTwo.id,
amount: 200,
code: "FREE-2",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
expect.objectContaining({
item_id: itemTwo.id,
amount: 200,
code: "FREE-2",
}),
])
)
})
it("should replace line item adjustments for a cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const adjustments = await service.setLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
])
)
await service.setLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 50,
code: "50%",
},
])
const cart = await service.retrieve(createdCart.id, {
relations: ["items.adjustments"],
})
expect(cart.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: itemOne.id,
adjustments: expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 50,
code: "50%",
}),
]),
}),
])
)
expect(cart.items?.length).toBe(1)
expect(cart.items?.[0].adjustments?.length).toBe(1)
})
it("should remove all line item adjustments for a cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const adjustments = await service.setLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
])
)
await service.setLineItemAdjustments(createdCart.id, [])
const cart = await service.retrieve(createdCart.id, {
relations: ["items.adjustments"],
})
expect(cart.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: itemOne.id,
adjustments: [],
}),
])
)
expect(cart.items?.length).toBe(1)
expect(cart.items?.[0].adjustments?.length).toBe(0)
})
it("should update line item adjustments for a cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const adjustments = await service.setLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
])
)
await service.setLineItemAdjustments(createdCart.id, [
{
id: adjustments[0].id,
amount: 50,
code: "50%",
},
])
const cart = await service.retrieve(createdCart.id, {
relations: ["items.adjustments"],
})
expect(cart.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: itemOne.id,
adjustments: [
expect.objectContaining({
id: adjustments[0].id,
item_id: itemOne.id,
amount: 50,
code: "50%",
}),
],
}),
])
)
expect(cart.items?.length).toBe(1)
expect(cart.items?.[0].adjustments?.length).toBe(1)
})
})
describe("addLineItemAdjustments", () => {
it("should add line item adjustments for items in a cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const adjustments = await service.addLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
])
)
})
it("should add multiple line item adjustments for multiple line items", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const [itemTwo] = await service.addLineItems(createdCart.id, [
{
quantity: 2,
unit_price: 200,
title: "test-2",
},
])
const adjustments = await service.addLineItemAdjustments(createdCart.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
{
item_id: itemTwo.id,
amount: 150,
code: "CODE-2",
},
])
expect(adjustments).toEqual(
expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
expect.objectContaining({
item_id: itemTwo.id,
amount: 150,
code: "CODE-2",
}),
])
)
})
it("should add line item adjustments for line items on multiple carts", async () => {
const [cartOne] = await service.create([
{
currency_code: "eur",
},
])
const [cartTwo] = await service.create([
{
currency_code: "usd",
},
])
const [itemOne] = await service.addLineItems(cartOne.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const [itemTwo] = await service.addLineItems(cartTwo.id, [
{
quantity: 2,
unit_price: 200,
title: "test-2",
},
])
await service.addLineItemAdjustments([
// item from cart one
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
// item from cart two
{
item_id: itemTwo.id,
amount: 150,
code: "CODE-2",
},
])
const cartOneItems = await service.listLineItems(
{ cart_id: cartOne.id },
{ relations: ["adjustments"] }
)
const cartTwoItems = await service.listLineItems(
{ cart_id: cartTwo.id },
{ relations: ["adjustments"] }
)
expect(cartOneItems).toEqual(
expect.arrayContaining([
expect.objectContaining({
adjustments: expect.arrayContaining([
expect.objectContaining({
item_id: itemOne.id,
amount: 100,
code: "FREE",
}),
]),
}),
])
)
expect(cartTwoItems).toEqual(
expect.arrayContaining([
expect.objectContaining({
adjustments: expect.arrayContaining([
expect.objectContaining({
item_id: itemTwo.id,
amount: 150,
code: "CODE-2",
}),
]),
}),
])
)
})
it("should throw if line item is not associated with cart", async () => {
const [cartOne] = await service.create([
{
currency_code: "eur",
},
])
const [cartTwo] = await service.create([
{
currency_code: "eur",
},
])
const [itemOne] = await service.addLineItems(cartOne.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const error = await service
.addLineItemAdjustments(cartTwo.id, [
{
item_id: itemOne.id,
amount: 100,
code: "FREE",
},
])
.catch((e) => e)
expect(error.message).toBe(
`Line item with id ${itemOne.id} does not exist on cart with id ${cartTwo.id}`
)
})
})
describe("removeLineItemAdjustments", () => {
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",
},
])
const [adjustment] = await service.addLineItemAdjustments(
createdCart.id,
[
{
item_id: item.id,
amount: 50,
},
]
)
expect(adjustment.item_id).toBe(item.id)
await service.removeLineItemAdjustments(adjustment.id)
const adjustments = await service.listLineItemAdjustments({
item_id: item.id,
})
expect(adjustments?.length).toBe(0)
})
it("should remove a line item succesfully with selector", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [item] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
},
])
const [adjustment] = await service.addLineItemAdjustments(
createdCart.id,
[
{
item_id: item.id,
amount: 50,
},
]
)
expect(adjustment.item_id).toBe(item.id)
await service.removeLineItemAdjustments({ item_id: item.id })
const adjustments = await service.listLineItemAdjustments({
item_id: item.id,
})
expect(adjustments?.length).toBe(0)
})
})
})