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

chore: additional tests and check undefined tax_rate #2157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions packages/medusa/src/services/__tests__/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,6 @@ describe("TotalsService", () => {
})

describe("getShippingTotal", () => {
let res

const getTaxLinesMock = jest.fn(() =>
Promise.resolve([
{ shipping_method_id: IdMap.getId("expensiveShipping") },
Expand Down Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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,
Expand All @@ -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)
})
})
})
6 changes: 3 additions & 3 deletions packages/medusa/src/services/totals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down Expand Up @@ -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 =
Expand Down