Skip to content

Commit

Permalink
Finish wiring up msw data to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Mar 16, 2024
1 parent 3132da4 commit fefce1f
Showing 1 changed file with 143 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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" }))
);
Expand All @@ -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,
Expand All @@ -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 = [
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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: [],
}),
];

Expand All @@ -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: [],
}),
];

Expand All @@ -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: [],
}),
];

Expand All @@ -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: [],
}),
];

Expand All @@ -309,6 +355,7 @@ describe("useAssessmentStatus", () => {
application: { id: 1, name: "app1" },
questionnaire: { id: 2, name: "questionnaire2" },
status: "complete",
sections: [],
}),
];

Expand All @@ -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,
});
});
});

0 comments on commit fefce1f

Please sign in to comment.