fix: Invoke canCalculate with an entire shipping option (#1397)
This commit is contained in:
1 parent
b8f1ae9ff5
commit
40b63d092a
2 files changed
+27
-28
No files matched your search
@@ -26,7 +26,7 @@ describe("ShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("update", () => {
|
describe("update", () => {
|
||||||
const shippingOptionRepository = MockRepository({
|
const shippingOptionRepository = MockRepository({
|
||||||
findOne: q => {
|
findOne: (q) => {
|
||||||
switch (q.where.id) {
|
switch (q.where.id) {
|
||||||
case IdMap.getId("noCalc"):
|
case IdMap.getId("noCalc"):
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
@@ -47,11 +47,13 @@ describe("ShippingOptionService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const fulfillmentProviderService = {
|
const fulfillmentProviderService = {
|
||||||
canCalculate: jest.fn().mockImplementation(id => id !== "no_calc"),
|
canCalculate: jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((option) => option.provider_id !== "no_calc"),
|
||||||
}
|
}
|
||||||
|
|
||||||
const shippingOptionRequirementRepository = MockRepository({
|
const shippingOptionRequirementRepository = MockRepository({
|
||||||
create: r => r,
|
create: (r) => r,
|
||||||
})
|
})
|
||||||
const optionService = new ShippingOptionService({
|
const optionService = new ShippingOptionService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
@@ -151,12 +153,12 @@ describe("ShippingOptionService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(fulfillmentProviderService.canCalculate).toHaveBeenCalledTimes(1)
|
expect(fulfillmentProviderService.canCalculate).toHaveBeenCalledTimes(1)
|
||||||
expect(fulfillmentProviderService.canCalculate).toHaveBeenCalledWith(
|
expect(fulfillmentProviderService.canCalculate).toHaveBeenCalledWith({
|
||||||
"provider",
|
amount: null,
|
||||||
{
|
data: { provider_data: "true" },
|
||||||
provider_data: "true",
|
price_type: "calculated",
|
||||||
}
|
provider_id: "provider",
|
||||||
)
|
})
|
||||||
|
|
||||||
expect(shippingOptionRepository.save).toHaveBeenCalledTimes(1)
|
expect(shippingOptionRepository.save).toHaveBeenCalledTimes(1)
|
||||||
expect(shippingOptionRepository.save).toHaveBeenCalledWith({
|
expect(shippingOptionRepository.save).toHaveBeenCalledWith({
|
||||||
@@ -204,7 +206,7 @@ describe("ShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("delete", () => {
|
describe("delete", () => {
|
||||||
const shippingOptionRepository = MockRepository({
|
const shippingOptionRepository = MockRepository({
|
||||||
findOne: i =>
|
findOne: (i) =>
|
||||||
i.where.id === IdMap.getId("validId")
|
i.where.id === IdMap.getId("validId")
|
||||||
? { id: IdMap.getId("validId") }
|
? { id: IdMap.getId("validId") }
|
||||||
: null,
|
: null,
|
||||||
@@ -236,7 +238,7 @@ describe("ShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("addRequirement", () => {
|
describe("addRequirement", () => {
|
||||||
const shippingOptionRepository = MockRepository({
|
const shippingOptionRepository = MockRepository({
|
||||||
findOne: q => {
|
findOne: (q) => {
|
||||||
switch (q.where.id) {
|
switch (q.where.id) {
|
||||||
case IdMap.getId("has-min"):
|
case IdMap.getId("has-min"):
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
@@ -254,7 +256,7 @@ describe("ShippingOptionService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const shippingOptionRequirementRepository = MockRepository({
|
const shippingOptionRequirementRepository = MockRepository({
|
||||||
create: r => r,
|
create: (r) => r,
|
||||||
})
|
})
|
||||||
const optionService = new ShippingOptionService({
|
const optionService = new ShippingOptionService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
@@ -297,10 +299,10 @@ describe("ShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("removeRequirement", () => {
|
describe("removeRequirement", () => {
|
||||||
const shippingOptionRequirementRepository = MockRepository({
|
const shippingOptionRequirementRepository = MockRepository({
|
||||||
softRemove: q => {
|
softRemove: (q) => {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
},
|
},
|
||||||
findOne: i =>
|
findOne: (i) =>
|
||||||
i.where.id === IdMap.getId("requirement_id")
|
i.where.id === IdMap.getId("requirement_id")
|
||||||
? { id: IdMap.getId("requirement_id") }
|
? { id: IdMap.getId("requirement_id") }
|
||||||
: null,
|
: null,
|
||||||
@@ -334,17 +336,17 @@ describe("ShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const shippingOptionRepository = MockRepository({
|
const shippingOptionRepository = MockRepository({
|
||||||
create: r => r,
|
create: (r) => r,
|
||||||
})
|
})
|
||||||
|
|
||||||
const fulfillmentProviderService = {
|
const fulfillmentProviderService = {
|
||||||
validateOption: jest.fn().mockImplementation(o => {
|
validateOption: jest.fn().mockImplementation((o) => {
|
||||||
return Promise.resolve(o.data.res)
|
return Promise.resolve(o.data.res)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
const regionService = {
|
const regionService = {
|
||||||
withTransaction: function() {
|
withTransaction: function () {
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
retrieve: () => {
|
retrieve: () => {
|
||||||
@@ -353,7 +355,7 @@ describe("ShippingOptionService", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const shippingOptionRequirementRepository = MockRepository({
|
const shippingOptionRequirementRepository = MockRepository({
|
||||||
create: r => r,
|
create: (r) => r,
|
||||||
})
|
})
|
||||||
const optionService = new ShippingOptionService({
|
const optionService = new ShippingOptionService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
@@ -481,7 +483,7 @@ describe("ShippingOptionService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("createShippingMethod", () => {
|
describe("createShippingMethod", () => {
|
||||||
const option = id => ({
|
const option = (id) => ({
|
||||||
id,
|
id,
|
||||||
region_id: IdMap.getId("region"),
|
region_id: IdMap.getId("region"),
|
||||||
price_type: "flat_rate",
|
price_type: "flat_rate",
|
||||||
@@ -497,16 +499,16 @@ describe("ShippingOptionService", () => {
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
const shippingOptionRepository = MockRepository({
|
const shippingOptionRepository = MockRepository({
|
||||||
findOne: q => {
|
findOne: (q) => {
|
||||||
switch (q.where.id) {
|
switch (q.where.id) {
|
||||||
default:
|
default:
|
||||||
return Promise.resolve(option(q.where.id))
|
return Promise.resolve(option(q.where.id))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const shippingMethodRepository = MockRepository({ create: r => r })
|
const shippingMethodRepository = MockRepository({ create: (r) => r })
|
||||||
const totalsService = {
|
const totalsService = {
|
||||||
getSubtotal: c => {
|
getSubtotal: (c) => {
|
||||||
return c.subtotal
|
return c.subtotal
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -514,8 +516,8 @@ describe("ShippingOptionService", () => {
|
|||||||
const providerService = {
|
const providerService = {
|
||||||
validateFulfillmentData: jest
|
validateFulfillmentData: jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockImplementation(r => Promise.resolve(r.data)),
|
.mockImplementation((r) => Promise.resolve(r.data)),
|
||||||
getPrice: d => d.price,
|
getPrice: (d) => d.price,
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionService = new ShippingOptionService({
|
const optionService = new ShippingOptionService({
|
||||||
|
|||||||
@@ -440,10 +440,7 @@ class ShippingOptionService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (priceType === "calculated") {
|
if (priceType === "calculated") {
|
||||||
const canCalculate = await this.providerService_.canCalculate(
|
const canCalculate = await this.providerService_.canCalculate(option)
|
||||||
option.provider_id,
|
|
||||||
option.data
|
|
||||||
)
|
|
||||||
if (!canCalculate) {
|
if (!canCalculate) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.INVALID_DATA,
|
MedusaError.Types.INVALID_DATA,
|
||||||
|
|||||||
Reference in new issue
Block a user