From bcc1edbf2bbcd5294823d18a1d1e750ea90c047f Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Tue, 6 Dec 2022 16:19:07 +0100 Subject: [PATCH] chore: addressing review - move repeated strings and objects to constants --- .../api/__tests__/admin/discount.js | 87 ++++++++++--------- .../discounts/__tests__/create-discount.js | 77 ++++++---------- 2 files changed, 72 insertions(+), 92 deletions(-) diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index 86732554bd..aa7236715e 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -24,6 +24,9 @@ const adminReqConfig = { }, } +const validRegionId = "test-region" +const invalidRegionId = "not-a-valid-region" + describe("/admin/discounts", () => { let medusaProcess let dbConnection @@ -408,7 +411,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -472,7 +475,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -540,7 +543,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -579,7 +582,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], }, adminReqConfig ) @@ -634,7 +637,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -659,7 +662,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], }, { headers: { @@ -696,7 +699,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -736,7 +739,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -763,7 +766,7 @@ describe("/admin/discounts", () => { }, ], }, - regions: ["test-region"], + regions: [validRegionId], }, { headers: { @@ -788,7 +791,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -832,7 +835,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -855,7 +858,7 @@ describe("/admin/discounts", () => { id: response.data.discount.rule.id, type: "free_shipping", }, - regions: ["test-region"], + regions: [validRegionId], }, adminReqConfig ) @@ -881,7 +884,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -926,7 +929,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -972,7 +975,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -1022,7 +1025,7 @@ describe("/admin/discounts", () => { allocation: "total", }, usage_limit: 10, - regions: ["test-region", "test-region-2"], + regions: [validRegionId, "test-region-2"], }, adminReqConfig ) @@ -1048,7 +1051,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -1058,7 +1061,7 @@ describe("/admin/discounts", () => { .post( `/admin/discounts/${response.data.discount.id}`, { - regions: ["test-region", "test-region-2"], + regions: [validRegionId, "test-region-2"], }, adminReqConfig ) @@ -1086,7 +1089,7 @@ describe("/admin/discounts", () => { allocation: "total", }, usage_limit: 10, - regions: ["test-region"], + regions: [validRegionId], }, adminReqConfig ) @@ -1119,7 +1122,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, starts_at: new Date("09/15/2021 11:50"), ends_at: new Date("09/15/2021 17:50"), @@ -1189,7 +1192,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, starts_at: new Date("09/15/2021 11:50"), ends_at: new Date("09/15/2021 17:50"), @@ -1247,7 +1250,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, starts_at: new Date("09/15/2021 11: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) const api = useApi() - const response = await api.post( + const err = await api.post( "/admin/discounts", { code: "HELLOWORLD", @@ -1278,22 +1281,22 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region", "not-a-valid-region"], + regions: [validRegionId, invalidRegionId], }, adminReqConfig - ).catch((err) => { - expect(err.response.status).toEqual(404) - expect(err.response.data.message).toEqual( - `Region with not-a-valid-region was not found` - ) - }) + ).catch(e => e) + + expect(err.response.status).toEqual(404) + 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) const api = useApi() - const response = await api.post( + const err = await api.post( "/admin/discounts", { code: "HELLOWORLD", @@ -1305,12 +1308,12 @@ describe("/admin/discounts", () => { }, }, adminReqConfig - ).catch((err) => { - expect(err.response.status).toEqual(400) - expect(err.response.data.message).toEqual( - `each value in regions must be a string, regions must be an array` - ) - }) + ).catch((e) => e) + + expect(err.response.status).toEqual(400) + 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, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -1446,7 +1449,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig @@ -2214,7 +2217,7 @@ describe("/admin/discounts", () => { value: 10, allocation: "total", }, - regions: ["test-region"], + regions: [validRegionId], usage_limit: 10, }, adminReqConfig diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js index fa315a1e44..ea83bc239e 100644 --- a/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js @@ -2,7 +2,15 @@ import { IdMap } from "medusa-test-utils" import { request } from "../../../../../helpers/test-request" import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" +const validRegionId = IdMap.getId("region-france") + describe("POST /admin/discounts", () => { + const generateAdminJwtSession = () => ({ + jwt: { + userId: IdMap.getId("admin_user") + } + }) + describe("successful creation", () => { let subject @@ -16,15 +24,11 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: "02/02/2021 13:45", ends_at: "03/14/2021 04:30", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -42,7 +46,7 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: new Date("02/02/2021 13:45"), ends_at: new Date("03/14/2021 04:30"), is_disabled: false, @@ -56,6 +60,7 @@ describe("POST /admin/discounts", () => { beforeAll(async () => { jest.clearAllMocks() + subject = await request("POST", "/admin/discounts", { payload: { code: "TEST", @@ -65,16 +70,12 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: "02/02/2021 13:45", is_dynamic: true, valid_duration: "PaMT2D", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -103,16 +104,12 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: "02/02/2021 13:45", is_dynamic: true, valid_duration: "P1Y2M03DT04H05M", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -130,7 +127,7 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: new Date("02/02/2021 13:45"), is_disabled: false, is_dynamic: true, @@ -151,13 +148,9 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], - }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, + regions: [validRegionId], }, + adminSession: generateAdminJwtSession(), }) }) @@ -192,16 +185,12 @@ describe("POST /admin/discounts", () => { }, ], }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: "02/02/2021 13:45", is_dynamic: true, valid_duration: "P1Y2M03DT04H05M", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -229,15 +218,11 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], ends_at: "02/02/2021", starts_at: "03/14/2021", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -267,14 +252,10 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: "03/14/2021 14:30", }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) }) @@ -293,7 +274,7 @@ describe("POST /admin/discounts", () => { value: 10, allocation: "total", }, - regions: [IdMap.getId("region-france")], + regions: [validRegionId], starts_at: new Date("03/14/2021 14:30"), }) }) @@ -315,11 +296,7 @@ describe("POST /admin/discounts", () => { allocation: "total", }, }, - adminSession: { - jwt: { - userId: IdMap.getId("admin_user"), - }, - }, + adminSession: generateAdminJwtSession(), }) })