Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Nov 23, 2022
1 parent 785c7c4 commit c4a65ff
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
100 changes: 82 additions & 18 deletions packages/medusa/src/services/__tests__/line-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ describe("LineItemService", () => {
}
},
getRegionPrice: () => 100,
list: jest.fn().mockImplementation(async (selector) => {
return (selector.id || []).map((id) => ({
id,
title: "Test variant",
product: {
title: "Test product",
thumbnail: "",
},
}))
}),
}

const pricingService = {
Expand Down Expand Up @@ -435,6 +445,37 @@ describe("LineItemService", () => {
}),
})
})

it("successfully create a line item with tax inclusive set to true by passing an object", async () => {
await lineItemService.generate({
variantId: IdMap.getId("test-variant"),
regionId: IdMap.getId("test-region"),
quantity: 1,
})

expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
expect(lineItemRepository.create).toHaveBeenCalledWith({
unit_price: 100,
title: "Test product",
description: "Test variant",
thumbnail: "",
variant_id: IdMap.getId("test-variant"),
quantity: 1,
allow_discounts: undefined,
is_giftcard: undefined,
metadata: {},
should_merge: true,
includes_tax: true,
variant: expect.objectContaining({
id: expect.any(String),
product: expect.objectContaining({
thumbnail: "",
title: "Test product",
}),
title: "Test variant",
}),
})
})
})
describe("generate", () => {
const lineItemRepository = MockRepository({
Expand Down Expand Up @@ -474,27 +515,19 @@ describe("LineItemService", () => {
},
}
}
return {
id: IdMap.getId("test-variant"),
title: "Test variant",
product: {
title: "Test product",
thumbnail: "",
},
}
},
getRegionPrice: () => 100,
list: jest.fn().mockImplementation(async (selector) => {
return (selector.id || []).map((id) => ({
id,
title: "Test variant",
product: {
title: "Test product",
thumbnail: "",
discountable: false,
is_giftcard: true,
},
}))
return (selector.id || []).map((id) => {
return {
id,
title: "Test variant",
product: {
title: "Test product",
thumbnail: "",
},
}
})
}),
}

Expand Down Expand Up @@ -564,6 +597,37 @@ describe("LineItemService", () => {
}),
})
})

it("successfully create a line item with tax inclusive set to false by passing an object", async () => {
await lineItemService.generate({
variantId: IdMap.getId("test-variant"),
regionId: IdMap.getId("test-region"),
quantity: 1,
})

expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
expect(lineItemRepository.create).toHaveBeenCalledWith({
unit_price: 100,
title: "Test product",
description: "Test variant",
thumbnail: "",
variant_id: IdMap.getId("test-variant"),
quantity: 1,
allow_discounts: undefined,
is_giftcard: undefined,
metadata: {},
should_merge: true,
includes_tax: false,
variant: expect.objectContaining({
id: expect.any(String),
product: expect.objectContaining({
thumbnail: "",
title: "Test product",
}),
title: "Test variant",
}),
})
})
})

describe("clone", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/services/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class LineItemService extends TransactionBaseService {
return await this.atomicPhase_(
async (transactionManager: EntityManager) => {
let data = variantIdOrData as unknown as GenerateInputData
let resolvedContext = regionIdOrContext as GenerateContext
let resolvedContext = (regionIdOrContext ?? {}) as GenerateContext

if (isString(variantIdOrData)) {
if (!quantity || !regionIdOrContext || !isString(regionIdOrContext)) {
Expand Down

0 comments on commit c4a65ff

Please sign in to comment.