Fix/polish discount (#569)
* formatting and optional "is_dynamic" on updates * formatting * uppercasing discount code on update * multiple regions fail with fixed discounts * discount testing * testing * spelling * removed comment Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
1 parent
9f1ae87605
commit
68c2a4fe3a
7 files changed
+408
-29
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import DiscountService from "../discount"
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { exportAllDeclaration } from "@babel/types"
|
||||
|
||||
describe("DiscountService", () => {
|
||||
describe("create", () => {
|
||||
@@ -13,7 +15,7 @@ describe("DiscountService", () => {
|
||||
id: IdMap.getId("france"),
|
||||
}
|
||||
},
|
||||
withTransaction: function() {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
}
|
||||
@@ -29,6 +31,25 @@ describe("DiscountService", () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("fails to create a fixed discount with multiple regions", async () => {
|
||||
expect.assertions(3)
|
||||
try {
|
||||
await discountService.create({
|
||||
code: "test",
|
||||
rule: {
|
||||
type: "fixed",
|
||||
allocation: "total",
|
||||
value: 20,
|
||||
},
|
||||
regions: [IdMap.getId("france"), IdMap.getId("Italy")],
|
||||
})
|
||||
} catch (err) {
|
||||
expect(err.type).toEqual("invalid_data")
|
||||
expect(err.message).toEqual("Fixed discounts can have one region")
|
||||
expect(discountRepository.create).toHaveBeenCalledTimes(0)
|
||||
}
|
||||
})
|
||||
|
||||
it("successfully creates discount", async () => {
|
||||
await discountService.create({
|
||||
code: "test",
|
||||
@@ -130,7 +151,7 @@ describe("DiscountService", () => {
|
||||
|
||||
describe("retrieve", () => {
|
||||
const discountRepository = MockRepository({
|
||||
findOne: query => {
|
||||
findOne: (query) => {
|
||||
if (query.where.id) {
|
||||
return Promise.resolve({ id: IdMap.getId("total10") })
|
||||
}
|
||||
@@ -170,7 +191,7 @@ describe("DiscountService", () => {
|
||||
|
||||
describe("retrieveByCode", () => {
|
||||
const discountRepository = MockRepository({
|
||||
findOne: query => {
|
||||
findOne: (query) => {
|
||||
if (query.where.code === "10%OFF") {
|
||||
return Promise.resolve({ id: IdMap.getId("total10"), code: "10%OFF" })
|
||||
}
|
||||
@@ -206,7 +227,11 @@ describe("DiscountService", () => {
|
||||
describe("update", () => {
|
||||
const discountRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({ id: IdMap.getId("total10"), code: "10%OFF" }),
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("total10"),
|
||||
code: "10%OFF",
|
||||
rule: { type: "fixed" },
|
||||
}),
|
||||
})
|
||||
|
||||
const discountRuleRepository = MockRepository({})
|
||||
@@ -230,6 +255,20 @@ describe("DiscountService", () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("fails to update a fixed discount with multiple regions", async () => {
|
||||
expect.assertions(3)
|
||||
try {
|
||||
await discountService.update(IdMap.getId("total10"), {
|
||||
code: "test",
|
||||
regions: [IdMap.getId("france"), IdMap.getId("Italy")],
|
||||
})
|
||||
} catch (err) {
|
||||
expect(err.type).toEqual("invalid_data")
|
||||
expect(err.message).toEqual("Fixed discounts can have one region")
|
||||
expect(discountRepository.create).toHaveBeenCalledTimes(0)
|
||||
}
|
||||
})
|
||||
|
||||
it("successfully updates discount", async () => {
|
||||
await discountService.update(IdMap.getId("total10"), {
|
||||
code: "test",
|
||||
@@ -238,7 +277,8 @@ describe("DiscountService", () => {
|
||||
expect(discountRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(discountRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("total10"),
|
||||
code: "test",
|
||||
code: "TEST",
|
||||
rule: { type: "fixed" },
|
||||
regions: [{ id: IdMap.getId("france") }],
|
||||
})
|
||||
})
|
||||
@@ -262,6 +302,7 @@ describe("DiscountService", () => {
|
||||
expect(discountRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(discountRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("total10"),
|
||||
rule: { type: "fixed" },
|
||||
code: "10%OFF",
|
||||
metadata: { testKey: "testValue" },
|
||||
})
|
||||
@@ -385,11 +426,24 @@ describe("DiscountService", () => {
|
||||
|
||||
describe("addRegion", () => {
|
||||
const discountRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({
|
||||
findOne: (q) => {
|
||||
if (q.where.id === "fixed") {
|
||||
return Promise.resolve({
|
||||
id: IdMap.getId("total10"),
|
||||
regions: [{ id: IdMap.getId("test-region") }],
|
||||
rule: {
|
||||
type: "fixed",
|
||||
},
|
||||
})
|
||||
}
|
||||
return Promise.resolve({
|
||||
id: IdMap.getId("total10"),
|
||||
regions: [{ id: IdMap.getId("test-region") }],
|
||||
}),
|
||||
rule: {
|
||||
type: "percentage",
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const discountRuleRepository = MockRepository({})
|
||||
@@ -413,6 +467,17 @@ describe("DiscountService", () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("fails to add a region to a fixed discount with an existing region", async () => {
|
||||
expect.assertions(3)
|
||||
try {
|
||||
await discountService.addRegion("fixed", IdMap.getId("test-region-2"))
|
||||
} catch (err) {
|
||||
expect(err.type).toEqual("invalid_data")
|
||||
expect(err.message).toEqual("Fixed discounts can have one region")
|
||||
expect(discountRepository.save).toHaveBeenCalledTimes(0)
|
||||
}
|
||||
})
|
||||
|
||||
it("successfully adds a region", async () => {
|
||||
await discountService.addRegion(
|
||||
IdMap.getId("total10"),
|
||||
@@ -426,6 +491,9 @@ describe("DiscountService", () => {
|
||||
{ id: IdMap.getId("test-region") },
|
||||
{ id: IdMap.getId("test-region-2") },
|
||||
],
|
||||
rule: {
|
||||
type: "percentage",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -441,7 +509,7 @@ describe("DiscountService", () => {
|
||||
|
||||
describe("createDynamicDiscount", () => {
|
||||
const discountRepository = MockRepository({
|
||||
create: d => d,
|
||||
create: (d) => d,
|
||||
findOne: () =>
|
||||
Promise.resolve({
|
||||
id: "parent",
|
||||
|
||||
Reference in new issue
Block a user