chore: addressing review - move repeated strings and objects to constants

This commit is contained in:
Riqwan Thamir
2022-12-07 16:42:32 +01:00
committed by Adrien de Peretti
parent cb2a32a82b
commit bcc1edbf2b
2 changed files with 72 additions and 92 deletions
@@ -24,6 +24,9 @@ const adminReqConfig = {
}, },
} }
const validRegionId = "test-region"
const invalidRegionId = "not-a-valid-region"
describe("/admin/discounts", () => { describe("/admin/discounts", () => {
let medusaProcess let medusaProcess
let dbConnection let dbConnection
@@ -408,7 +411,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -472,7 +475,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -540,7 +543,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -579,7 +582,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
}, },
adminReqConfig adminReqConfig
) )
@@ -634,7 +637,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -659,7 +662,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
}, },
{ {
headers: { headers: {
@@ -696,7 +699,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -736,7 +739,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -763,7 +766,7 @@ describe("/admin/discounts", () => {
}, },
], ],
}, },
regions: ["test-region"], regions: [validRegionId],
}, },
{ {
headers: { headers: {
@@ -788,7 +791,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -832,7 +835,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -855,7 +858,7 @@ describe("/admin/discounts", () => {
id: response.data.discount.rule.id, id: response.data.discount.rule.id,
type: "free_shipping", type: "free_shipping",
}, },
regions: ["test-region"], regions: [validRegionId],
}, },
adminReqConfig adminReqConfig
) )
@@ -881,7 +884,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -926,7 +929,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -972,7 +975,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -1022,7 +1025,7 @@ describe("/admin/discounts", () => {
allocation: "total", allocation: "total",
}, },
usage_limit: 10, usage_limit: 10,
regions: ["test-region", "test-region-2"], regions: [validRegionId, "test-region-2"],
}, },
adminReqConfig adminReqConfig
) )
@@ -1048,7 +1051,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -1058,7 +1061,7 @@ describe("/admin/discounts", () => {
.post( .post(
`/admin/discounts/${response.data.discount.id}`, `/admin/discounts/${response.data.discount.id}`,
{ {
regions: ["test-region", "test-region-2"], regions: [validRegionId, "test-region-2"],
}, },
adminReqConfig adminReqConfig
) )
@@ -1086,7 +1089,7 @@ describe("/admin/discounts", () => {
allocation: "total", allocation: "total",
}, },
usage_limit: 10, usage_limit: 10,
regions: ["test-region"], regions: [validRegionId],
}, },
adminReqConfig adminReqConfig
) )
@@ -1119,7 +1122,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
starts_at: new Date("09/15/2021 11:50"), starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/15/2021 17:50"), ends_at: new Date("09/15/2021 17:50"),
@@ -1189,7 +1192,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
starts_at: new Date("09/15/2021 11:50"), starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/15/2021 17:50"), ends_at: new Date("09/15/2021 17:50"),
@@ -1247,7 +1250,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
starts_at: new Date("09/15/2021 11:50"), starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/14/2021 17:50"), ends_at: new Date("09/14/2021 17:50"),
@@ -1264,11 +1267,11 @@ describe("/admin/discounts", () => {
}) })
}) })
it("fails to create a discount if regions array contains an invalid regionID ", async () => { it("fails to create a discount if the regions contains an invalid regionId ", async () => {
expect.assertions(2) expect.assertions(2)
const api = useApi() const api = useApi()
const response = await api.post( const err = await api.post(
"/admin/discounts", "/admin/discounts",
{ {
code: "HELLOWORLD", code: "HELLOWORLD",
@@ -1278,22 +1281,22 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region", "not-a-valid-region"], regions: [validRegionId, invalidRegionId],
}, },
adminReqConfig adminReqConfig
).catch((err) => { ).catch(e => e)
expect(err.response.status).toEqual(404)
expect(err.response.data.message).toEqual( expect(err.response.status).toEqual(404)
`Region with not-a-valid-region was not found` expect(err.response.data.message).toEqual(
) `Region with not-a-valid-region was not found`
}) )
}) })
it("fails to create a discount if regions array is not present ", async () => { it("fails to create a discount if regions are not present ", async () => {
expect.assertions(2) expect.assertions(2)
const api = useApi() const api = useApi()
const response = await api.post( const err = await api.post(
"/admin/discounts", "/admin/discounts",
{ {
code: "HELLOWORLD", code: "HELLOWORLD",
@@ -1305,12 +1308,12 @@ describe("/admin/discounts", () => {
}, },
}, },
adminReqConfig adminReqConfig
).catch((err) => { ).catch((e) => e)
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual( expect(err.response.status).toEqual(400)
`each value in regions must be a string, regions must be an array` expect(err.response.data.message).toEqual(
) `each value in regions must be a string, regions must be an array`
}) )
}) })
}) })
@@ -1416,7 +1419,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -1446,7 +1449,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -2214,7 +2217,7 @@ describe("/admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: ["test-region"], regions: [validRegionId],
usage_limit: 10, usage_limit: 10,
}, },
adminReqConfig adminReqConfig
@@ -2,7 +2,15 @@ import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request" import { request } from "../../../../../helpers/test-request"
import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" import { DiscountServiceMock } from "../../../../../services/__mocks__/discount"
const validRegionId = IdMap.getId("region-france")
describe("POST /admin/discounts", () => { describe("POST /admin/discounts", () => {
const generateAdminJwtSession = () => ({
jwt: {
userId: IdMap.getId("admin_user")
}
})
describe("successful creation", () => { describe("successful creation", () => {
let subject let subject
@@ -16,15 +24,11 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: "02/02/2021 13:45", starts_at: "02/02/2021 13:45",
ends_at: "03/14/2021 04:30", ends_at: "03/14/2021 04:30",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -42,7 +46,7 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: new Date("02/02/2021 13:45"), starts_at: new Date("02/02/2021 13:45"),
ends_at: new Date("03/14/2021 04:30"), ends_at: new Date("03/14/2021 04:30"),
is_disabled: false, is_disabled: false,
@@ -56,6 +60,7 @@ describe("POST /admin/discounts", () => {
beforeAll(async () => { beforeAll(async () => {
jest.clearAllMocks() jest.clearAllMocks()
subject = await request("POST", "/admin/discounts", { subject = await request("POST", "/admin/discounts", {
payload: { payload: {
code: "TEST", code: "TEST",
@@ -65,16 +70,12 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: "02/02/2021 13:45", starts_at: "02/02/2021 13:45",
is_dynamic: true, is_dynamic: true,
valid_duration: "PaMT2D", valid_duration: "PaMT2D",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -103,16 +104,12 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: "02/02/2021 13:45", starts_at: "02/02/2021 13:45",
is_dynamic: true, is_dynamic: true,
valid_duration: "P1Y2M03DT04H05M", valid_duration: "P1Y2M03DT04H05M",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -130,7 +127,7 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: new Date("02/02/2021 13:45"), starts_at: new Date("02/02/2021 13:45"),
is_disabled: false, is_disabled: false,
is_dynamic: true, is_dynamic: true,
@@ -151,13 +148,9 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
}, },
adminSession: generateAdminJwtSession(),
}) })
}) })
@@ -192,16 +185,12 @@ describe("POST /admin/discounts", () => {
}, },
], ],
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: "02/02/2021 13:45", starts_at: "02/02/2021 13:45",
is_dynamic: true, is_dynamic: true,
valid_duration: "P1Y2M03DT04H05M", valid_duration: "P1Y2M03DT04H05M",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -229,15 +218,11 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
ends_at: "02/02/2021", ends_at: "02/02/2021",
starts_at: "03/14/2021", starts_at: "03/14/2021",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -267,14 +252,10 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: "03/14/2021 14:30", starts_at: "03/14/2021 14:30",
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })
@@ -293,7 +274,7 @@ describe("POST /admin/discounts", () => {
value: 10, value: 10,
allocation: "total", allocation: "total",
}, },
regions: [IdMap.getId("region-france")], regions: [validRegionId],
starts_at: new Date("03/14/2021 14:30"), starts_at: new Date("03/14/2021 14:30"),
}) })
}) })
@@ -315,11 +296,7 @@ describe("POST /admin/discounts", () => {
allocation: "total", allocation: "total",
}, },
}, },
adminSession: { adminSession: generateAdminJwtSession(),
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}) })
}) })