diff --git a/packages/medusa/src/services/__tests__/totals.js b/packages/medusa/src/services/__tests__/totals.js index 1260081d50258..0b524efc54710 100644 --- a/packages/medusa/src/services/__tests__/totals.js +++ b/packages/medusa/src/services/__tests__/totals.js @@ -708,8 +708,6 @@ describe("TotalsService", () => { }) describe("getShippingTotal", () => { - let res - const getTaxLinesMock = jest.fn(() => Promise.resolve([ { shipping_method_id: IdMap.getId("expensiveShipping") }, @@ -749,9 +747,9 @@ describe("TotalsService", () => { }, ], } - res = await totalsService.getShippingTotal(order) + const total = await totalsService.getShippingTotal(order) - expect(res).toEqual(100) + expect(total).toEqual(100) }) }) @@ -1011,6 +1009,17 @@ describe("TotalsService", () => { }) describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] getShippingTotal ", () => { + const shippingMethodData = { + id: IdMap.getId("expensiveShipping"), + name: "Expensive Shipping", + price: 120, + tax_lines: [{ shipping_method_id: IdMap.getId("expensiveShipping") }], + provider_id: "default_provider", + profile_id: IdMap.getId("default"), + data: { + extra: "hi", + }, + } const calculateMock = jest.fn(() => Promise.resolve(20)) const totalsService = new TotalsService({ ...container, @@ -1026,41 +1035,56 @@ describe("TotalsService", () => { jest.clearAllMocks() }) - it("calculates total", async () => { + it("calculates total with tax lines and being tax inclusive", async () => { const order = { - tax_rate: null, - items: [ + object: "order", + shipping_methods: [ { - unit_price: 20, - quantity: 2, + ...shippingMethodData, + includes_tax: true, }, + ], + } + + const total = await totalsService.getShippingTotal(order) + + expect(total).toEqual(100) + }) + + it("calculates total with tax lines and not being tax inclusive", async () => { + const order = { + object: "order", + shipping_methods: [ { - unit_price: 25, - quantity: 2, - includes_tax: true, + ...shippingMethodData, + price: 100, + includes_tax: false, }, ], + } + + const total = await totalsService.getShippingTotal(order) + + expect(total).toEqual(100) + }) + + it("calculates total with the old system and not being tax inclusive", async () => { + const order = { + object: "order", + tax_rate: 20, shipping_methods: [ { - id: IdMap.getId("expensiveShipping"), - name: "Expensive Shipping", - price: 120, - includes_tax: true, - tax_lines: [ - { shipping_method_id: IdMap.getId("expensiveShipping") }, - ], - provider_id: "default_provider", - profile_id: IdMap.getId("default"), - data: { - extra: "hi", - }, + ...shippingMethodData, + price: 100, + includes_tax: false, + tax_lines: [], }, ], } - const res = await totalsService.getShippingTotal(order) + const total = await totalsService.getShippingTotal(order) - expect(res).toEqual(100) + expect(total).toEqual(100) }) }) }) diff --git a/packages/medusa/src/services/totals.ts b/packages/medusa/src/services/totals.ts index a271e10a6d990..59dc48722d5c4 100644 --- a/packages/medusa/src/services/totals.ts +++ b/packages/medusa/src/services/totals.ts @@ -210,7 +210,7 @@ class TotalsService extends TransactionBaseService { } if (opts.include_tax) { - if (isOrder(cartOrOrder) && cartOrOrder.tax_rate !== null) { + if (isOrder(cartOrOrder) && cartOrOrder.tax_rate != null) { totals.original_tax_total = Math.round( totals.price * (cartOrOrder.tax_rate / 100) ) @@ -825,10 +825,10 @@ class TotalsService extends TransactionBaseService { // Tax Information if (options.include_tax) { - // When we have an order with a null'ed tax rate we know that it is an + // When we have an order with a nulled or undefined tax rate we know that it is an // order from the old tax system. The following is a backward compat // calculation. - if (isOrder(cartOrOrder) && cartOrOrder.tax_rate !== null) { + if (isOrder(cartOrOrder) && cartOrOrder.tax_rate != null) { const taxRate = cartOrOrder.tax_rate / 100 const includesTax =