Skip to content

Commit efcd894

Browse files
committed
refactor: validate if calculcated amount matches the external price
1 parent 0a81541 commit efcd894

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

src/services/cart.test.ts

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
import supertest from "supertest";
1616
import { afterEach, beforeEach, describe, expect, test } from "vitest";
1717
import { customerDraftFactory } from "#src/testing/customer.ts";
18+
import { calculateMoneyTotalCentAmount } from "#src/repositories/helpers.ts";
1819
import { CommercetoolsMock } from "../index.ts";
1920

2021
describe("Carts Query", () => {
@@ -824,6 +825,94 @@ describe("Cart Update Actions", () => {
824825
fractionDigits: 3,
825826
preciseAmount: 1015,
826827
};
828+
const expectedUnitCentAmount = calculateMoneyTotalCentAmount(
829+
externalPrice,
830+
1,
831+
);
832+
const expectedTotalCentAmount = calculateMoneyTotalCentAmount(
833+
externalPrice,
834+
2,
835+
);
836+
837+
const response = await supertest(ctMock.app)
838+
.post(`/dummy/carts/${cartWithLineItem.id}`)
839+
.send({
840+
version: cartWithLineItem.version,
841+
actions: [
842+
{
843+
action: "setLineItemPrice",
844+
lineItemId: lineItem.id,
845+
externalPrice,
846+
},
847+
],
848+
});
849+
850+
expect(response.status).toBe(200);
851+
expect(response.body.version).toBe(cartWithLineItem.version + 1);
852+
expect(response.body.lineItems).toHaveLength(1);
853+
854+
const updatedLineItem = response.body.lineItems[0];
855+
expect(updatedLineItem.priceMode).toBe("ExternalPrice");
856+
expect(updatedLineItem.price.value.type).toBe("highPrecision");
857+
expect(updatedLineItem.price.value.currencyCode).toBe(
858+
externalPrice.currencyCode,
859+
);
860+
expect(updatedLineItem.price.value.fractionDigits).toBe(
861+
externalPrice.fractionDigits,
862+
);
863+
expect(updatedLineItem.price.value.preciseAmount).toBe(
864+
externalPrice.preciseAmount,
865+
);
866+
expect(updatedLineItem.price.value.centAmount).toBe(expectedUnitCentAmount);
867+
expect(updatedLineItem.totalPrice.centAmount).toBe(expectedTotalCentAmount);
868+
expect(response.body.totalPrice.centAmount).toBe(expectedTotalCentAmount);
869+
});
870+
871+
test("setLineItemPrice supports high precision external price with fractionDigits 5", async () => {
872+
const product = await supertest(ctMock.app)
873+
.post("/dummy/products")
874+
.send(productDraft)
875+
.then((x) => x.body);
876+
877+
assert(product, "product not created");
878+
879+
const baseCartResponse = await supertest(ctMock.app)
880+
.post("/dummy/carts")
881+
.send({ currency: "EUR" });
882+
expect(baseCartResponse.status).toBe(201);
883+
const baseCart = baseCartResponse.body as Cart;
884+
885+
const addLineItemResponse = await supertest(ctMock.app)
886+
.post(`/dummy/carts/${baseCart.id}`)
887+
.send({
888+
version: baseCart.version,
889+
actions: [
890+
{
891+
action: "addLineItem",
892+
sku: product.masterData.current.masterVariant.sku,
893+
quantity: 2,
894+
},
895+
],
896+
});
897+
expect(addLineItemResponse.status).toBe(200);
898+
const cartWithLineItem = addLineItemResponse.body as Cart;
899+
const lineItem = cartWithLineItem.lineItems[0];
900+
assert(lineItem, "lineItem not created");
901+
902+
const externalPrice: HighPrecisionMoneyDraft = {
903+
type: "highPrecision",
904+
currencyCode: "EUR",
905+
fractionDigits: 5,
906+
preciseAmount: 101499,
907+
};
908+
const expectedUnitCentAmount = calculateMoneyTotalCentAmount(
909+
externalPrice,
910+
1,
911+
);
912+
const expectedTotalCentAmount = calculateMoneyTotalCentAmount(
913+
externalPrice,
914+
2,
915+
);
827916

828917
const response = await supertest(ctMock.app)
829918
.post(`/dummy/carts/${cartWithLineItem.id}`)
@@ -854,9 +943,9 @@ describe("Cart Update Actions", () => {
854943
expect(updatedLineItem.price.value.preciseAmount).toBe(
855944
externalPrice.preciseAmount,
856945
);
857-
expect(updatedLineItem.price.value.centAmount).toBe(102);
858-
expect(updatedLineItem.totalPrice.centAmount).toBe(203);
859-
expect(response.body.totalPrice.centAmount).toBe(203);
946+
expect(updatedLineItem.price.value.centAmount).toBe(expectedUnitCentAmount);
947+
expect(updatedLineItem.totalPrice.centAmount).toBe(expectedTotalCentAmount);
948+
expect(response.body.totalPrice.centAmount).toBe(expectedTotalCentAmount);
860949
});
861950

862951
test("setLineItemPrice fails when the money uses another currency", async () => {

0 commit comments

Comments
 (0)