add: is_discount filtering + unit tests (#330)

This commit is contained in:
Zakaria El Asri
2021-08-05 15:44:19 +01:00
committed by GitHub
parent b101725396
commit ca0de00d0b
2 changed files with 72 additions and 2 deletions

View File

@@ -44,7 +44,7 @@ describe("GET /admin/discounts", () => {
jest.clearAllMocks()
subject = await request(
"GET",
`/admin/discounts?q=OLI&limit=40&offset=20&is_dynamic=false`,
`/admin/discounts?q=OLI&limit=40&offset=20&is_dynamic=false&is_disabled=false`,
{
adminSession: {
jwt: {
@@ -62,7 +62,7 @@ describe("GET /admin/discounts", () => {
it("calls service retrieve with config", () => {
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledTimes(1)
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledWith(
{ q: "OLI", is_dynamic: false },
{ q: "OLI", is_dynamic: false, is_disabled: false },
{
select: defaultFields,
relations: defaultRelations,
@@ -73,4 +73,70 @@ describe("GET /admin/discounts", () => {
)
})
})
describe("successful retrieval of dynamic discounts", () => {
let subject
beforeAll(async () => {
jest.clearAllMocks()
subject = await request("GET", `/admin/discounts?is_dynamic=true`, {
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("calls service retrieve with corresponding config", () => {
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledTimes(1)
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledWith(
{ is_dynamic: true },
{
select: defaultFields,
relations: defaultRelations,
skip: 0,
take: 20,
order: { created_at: "DESC" },
}
)
})
})
describe("successful retrieval of disabled discounts", () => {
let subject
beforeAll(async () => {
jest.clearAllMocks()
subject = await request("GET", `/admin/discounts?is_disabled=true`, {
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("calls service retrieve with corresponding config", () => {
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledTimes(1)
expect(DiscountServiceMock.listAndCount).toHaveBeenCalledWith(
{ is_disabled: true },
{
select: defaultFields,
relations: defaultRelations,
skip: 0,
take: 20,
order: { created_at: "DESC" },
}
)
})
})
})

View File

@@ -34,6 +34,10 @@ export default async (req, res) => {
selector.is_dynamic = req.query.is_dynamic === "true"
}
if ("is_disabled" in req.query) {
selector.is_disabled = req.query.is_disabled === "true"
}
const listConfig = {
select: defaultFields,
relations: defaultRelations,