From fefce1ff70175ac0be062cdf72775368acb1e0a3 Mon Sep 17 00:00:00 2001 From: Ian Bolton Date: Fri, 15 Mar 2024 21:07:00 -0400 Subject: [PATCH] Finish wiring up msw data to unit tests --- .../application-assessment-status.test.tsx | 228 +++++++++++------- 1 file changed, 143 insertions(+), 85 deletions(-) diff --git a/client/src/app/pages/applications/components/application-assessment-status/tests/application-assessment-status.test.tsx b/client/src/app/pages/applications/components/application-assessment-status/tests/application-assessment-status.test.tsx index bf5cd511a0..a32ff84780 100644 --- a/client/src/app/pages/applications/components/application-assessment-status/tests/application-assessment-status.test.tsx +++ b/client/src/app/pages/applications/components/application-assessment-status/tests/application-assessment-status.test.tsx @@ -13,11 +13,12 @@ describe("useAssessmentStatus", () => { beforeEach(() => { jest.clearAllMocks(); }); + afterEach(() => { + server.resetHandlers(); + }); - // // Dynamically change the handler response for a specific test - beforeEach(() => { + it("Correctly calculates status given one started assessment and one complete assessment for an application", async () => { server.use( - // Override the handler for fetching assessments for this test only rest.get("/hub/assessments", (req, res, ctx) => { return res( ctx.json([ @@ -39,7 +40,6 @@ describe("useAssessmentStatus", () => { ); }), - // Override the handler for fetching archetypes for this test only rest.get("/hub/archetypes", (req, res, ctx) => { return res( ctx.json([ @@ -52,9 +52,6 @@ describe("useAssessmentStatus", () => { ); }) ); - }); - - it("Correctly calculates status given one started assessment and one complete assessment for an application", async () => { const { result, waitForNextUpdate } = renderHook(() => useAssessmentStatus(createMockApplication({ id: 1, name: "app1" })) ); @@ -69,30 +66,28 @@ describe("useAssessmentStatus", () => { }); }); - it.skip("Correctly calculates status given two complete assessments for an application", () => { + it("Correctly calculates status given two complete assessments for an application", async () => { const mockAssessments = [ createMockAssessment({ id: 1, application: { id: 1, name: "app1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "complete", + sections: [], }), - createMockAssessment({ id: 2, application: { id: 1, name: "app1" }, questionnaire: { id: 2, name: "questionnaire2" }, status: "complete", + sections: [], }), ]; - - const mockArchetypes = [ - createMockArchetype({ - id: 1, - name: "archetype1", - applications: [{ id: 1, name: "app1" }], - }), - ]; + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }) + ); const mockApplication = createMockApplication({ id: 1, @@ -101,25 +96,29 @@ describe("useAssessmentStatus", () => { assessments: mockAssessments, }); - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - - // expect(result.current).toEqual({ - // allArchetypesAssessed: false, - // countOfFullyAssessedArchetypes: 0, - // countOfArchetypesWithRequiredAssessments: 0, - // hasApplicationAssessmentInProgress: true, - // isApplicationDirectlyAssessed: true, - // }); + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + + await waitForNextUpdate(); + + expect(result.current).toEqual({ + allArchetypesAssessed: false, + countOfFullyAssessedArchetypes: 0, + countOfArchetypesWithRequiredAssessments: 0, + hasApplicationAssessmentInProgress: true, + isApplicationDirectlyAssessed: true, + }); }); - it.skip("Correctly calculates status given two inherited archetype; One with a complete state and one with started state.", () => { + + it("Correctly calculates status given two inherited archetype; One with a complete state and one with started state.", async () => { const arch1Assessments = [ createMockAssessment({ id: 1, archetype: { id: 1, name: "archetype1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "complete", + sections: [], }), ]; const arch2Assessments = [ @@ -128,10 +127,10 @@ describe("useAssessmentStatus", () => { archetype: { id: 2, name: "archetype2" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "started", + sections: [], }), ]; const mockAssessments = [...arch1Assessments, ...arch2Assessments]; - const mockArchetypes = [ createMockArchetype({ id: 1, @@ -148,7 +147,15 @@ describe("useAssessmentStatus", () => { assessed: false, }), ]; + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }), + rest.get("/hub/archetypes", (req, res, ctx) => { + return res(ctx.json(mockArchetypes)); + }) + ); const mockApplication = createMockApplication({ id: 1, name: "app1", @@ -158,26 +165,27 @@ describe("useAssessmentStatus", () => { ], assessed: false, }); - - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - // expect(result.current).toEqual({ - // allArchetypesAssessed: false, - // countOfFullyAssessedArchetypes: 1, - // countOfArchetypesWithRequiredAssessments: 2, - // hasApplicationAssessmentInProgress: false, - // isApplicationDirectlyAssessed: false, - // }); + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + await waitForNextUpdate(); + expect(result.current).toEqual({ + allArchetypesAssessed: false, + countOfFullyAssessedArchetypes: 1, + countOfArchetypesWithRequiredAssessments: 2, + hasApplicationAssessmentInProgress: false, + isApplicationDirectlyAssessed: false, + }); }); - it.skip("Correctly calculates status given a single inherited archetype with a complete state.", () => { + it("Correctly calculates status given a single inherited archetype with a complete state.", async () => { const mockAssessments = [ createMockAssessment({ id: 1, archetype: { id: 1, name: "archetype1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "complete", + sections: [], }), ]; @@ -198,24 +206,37 @@ describe("useAssessmentStatus", () => { assessed: false, }); - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - // expect(result.current).toEqual({ - // allArchetypesAssessed: true, - // countOfFullyAssessedArchetypes: 1, - // countOfArchetypesWithRequiredAssessments: 1, - // hasApplicationAssessmentInProgress: false, - // isApplicationDirectlyAssessed: false, - // }); + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }), + + rest.get("/hub/archetypes", (req, res, ctx) => { + return res(ctx.json(mockArchetypes)); + }) + ); + + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + await waitForNextUpdate(); + expect(result.current).toEqual({ + allArchetypesAssessed: true, + countOfFullyAssessedArchetypes: 1, + countOfArchetypesWithRequiredAssessments: 1, + hasApplicationAssessmentInProgress: false, + isApplicationDirectlyAssessed: false, + }); }); - it.skip("Correctly calculates status given 1 started assessment for an applications only archetype.", () => { + + it("Correctly calculates status given 1 started assessment for an applications only archetype.", async () => { const mockAssessments = [ createMockAssessment({ id: 1, archetype: { id: 1, name: "archetype1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "started", + sections: [], }), ]; @@ -234,25 +255,37 @@ describe("useAssessmentStatus", () => { archetypes: [{ id: 1, name: "archetype1" }], assessed: false, }); + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }), - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - // expect(result.current).toEqual({ - // allArchetypesAssessed: false, - // countOfFullyAssessedArchetypes: 0, - // countOfArchetypesWithRequiredAssessments: 1, - // hasApplicationAssessmentInProgress: false, - // isApplicationDirectlyAssessed: false, - // }); + rest.get("/hub/archetypes", (req, res, ctx) => { + return res(ctx.json(mockArchetypes)); + }) + ); + + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + await waitForNextUpdate(); + + expect(result.current).toEqual({ + allArchetypesAssessed: false, + countOfFullyAssessedArchetypes: 0, + countOfArchetypesWithRequiredAssessments: 1, + hasApplicationAssessmentInProgress: false, + isApplicationDirectlyAssessed: false, + }); }); - it.skip("Correctly calculates status given one complete assessment for an application's inherited archetype with no direct assessment", () => { + it("Correctly calculates status given one complete assessment for an application's inherited archetype with no direct assessment", async () => { const mockAssessments = [ createMockAssessment({ id: 1, archetype: { id: 1, name: "archetype1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "complete", + sections: [], }), ]; @@ -273,24 +306,37 @@ describe("useAssessmentStatus", () => { assessed: false, }); - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - // expect(result.current).toEqual({ - // allArchetypesAssessed: true, - // countOfFullyAssessedArchetypes: 1, - // countOfArchetypesWithRequiredAssessments: 1, - // hasApplicationAssessmentInProgress: false, - // isApplicationDirectlyAssessed: false, - // }); + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }), + + rest.get("/hub/archetypes", (req, res, ctx) => { + return res(ctx.json(mockArchetypes)); + }) + ); + + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + await waitForNextUpdate(); + expect(result.current).toEqual({ + allArchetypesAssessed: true, + countOfFullyAssessedArchetypes: 1, + countOfArchetypesWithRequiredAssessments: 1, + hasApplicationAssessmentInProgress: false, + isApplicationDirectlyAssessed: false, + }); }); - it.skip("Correctly calculates status given one complete assessment for an application's inherited archetype with a direct assessment", () => { + + it("Correctly calculates status given one complete assessment for an application's inherited archetype with a direct assessment", async () => { const archetypeAssessments = [ createMockAssessment({ id: 1, archetype: { id: 1, name: "archetype1" }, questionnaire: { id: 1, name: "questionnaire1" }, status: "complete", + sections: [], }), ]; @@ -309,6 +355,7 @@ describe("useAssessmentStatus", () => { application: { id: 1, name: "app1" }, questionnaire: { id: 2, name: "questionnaire2" }, status: "complete", + sections: [], }), ]; @@ -324,16 +371,27 @@ describe("useAssessmentStatus", () => { ...archetypeAssessments, ...applicationAssessments, ]; + server.use( + rest.get("/hub/assessments", (req, res, ctx) => { + return res(ctx.json(mockAssessments)); + }), - // const { result } = renderHook(() => - // useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) - // ); - // expect(result.current).toEqual({ - // allArchetypesAssessed: true, - // countOfFullyAssessedArchetypes: 1, - // countOfArchetypesWithRequiredAssessments: 1, - // hasApplicationAssessmentInProgress: true, - // isApplicationDirectlyAssessed: true, - // }); + rest.get("/hub/archetypes", (req, res, ctx) => { + return res(ctx.json(mockArchetypes)); + }) + ); + + const { result, waitForNextUpdate } = renderHook(() => + useAssessmentStatus(mockApplication) + ); + await waitForNextUpdate(); + + expect(result.current).toEqual({ + allArchetypesAssessed: true, + countOfFullyAssessedArchetypes: 1, + countOfArchetypesWithRequiredAssessments: 1, + hasApplicationAssessmentInProgress: true, + isApplicationDirectlyAssessed: true, + }); }); });