-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👻 Add unit tests and hook for useAssessmentStatus
Signed-off-by: Ian Bolton <[email protected]>
- Loading branch information
1 parent
3e3d690
commit 426f36b
Showing
4 changed files
with
269 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// hooks/useAssessmentStatus.js | ||
import { Assessment, Archetype, Application } from "@app/api/models"; | ||
import { useMemo } from "react"; | ||
|
||
export const useAssessmentStatus = ( | ||
assessments: Assessment[], | ||
archetypes: Archetype[], | ||
application: Application | ||
) => { | ||
return useMemo(() => { | ||
const applicationAssessments = assessments?.filter( | ||
(assessment: Assessment) => assessment.application?.id === application.id | ||
); | ||
const inheritedArchetypes = archetypes?.filter( | ||
(archetype: Archetype) => | ||
archetype.applications?.map((app) => app.id).includes(application.id) | ||
); | ||
|
||
const assessmentsWithArchetypes = inheritedArchetypes.map( | ||
(inheritedArchetype) => ({ | ||
inheritedArchetype, | ||
assessments: assessments.filter( | ||
(assessment) => assessment.archetype?.id === inheritedArchetype.id | ||
), | ||
}) | ||
); | ||
|
||
const someArchetypesAssessed = assessmentsWithArchetypes.some( | ||
({ assessments }) => assessments.length > 0 | ||
); | ||
|
||
const allArchetypesAssessed = | ||
assessmentsWithArchetypes.length > 0 && | ||
assessmentsWithArchetypes.every(({ inheritedArchetype, assessments }) => { | ||
const requiredAssessments = assessments.filter( | ||
(assessment) => assessment.required | ||
); | ||
return ( | ||
inheritedArchetype.assessed && | ||
assessments.length > 0 && | ||
requiredAssessments.length > 0 && | ||
requiredAssessments.every( | ||
(assessment) => assessment.status === "complete" | ||
) | ||
); | ||
}); | ||
|
||
const hasInProgressOrNotStartedRequiredAssessments = | ||
assessmentsWithArchetypes.some(({ assessments }) => | ||
assessments.some( | ||
(assessment) => | ||
assessment.required && | ||
["empty", "started"].includes(assessment.status) | ||
) | ||
); | ||
|
||
const assessedArchetypesWithARequiredAssessment = | ||
assessmentsWithArchetypes.filter(({ assessments, inheritedArchetype }) => | ||
assessments.some( | ||
(assessment) => | ||
assessment.required && | ||
assessment.status === "complete" && | ||
inheritedArchetype.assessed | ||
) | ||
); | ||
const assessedArchetypeCount = | ||
inheritedArchetypes?.filter( | ||
(inheritedArchetype) => | ||
inheritedArchetype?.assessments?.length ?? | ||
(0 > 0 && inheritedArchetype.assessed) | ||
).length || 0; | ||
|
||
const hasAssessmentInProgress = applicationAssessments?.some( | ||
(assessment: Assessment) => | ||
assessment.status === "started" || | ||
assessment.status === "empty" || | ||
assessment.status === "complete" | ||
); | ||
const isDirectlyAssessed = | ||
application.assessed && (application.assessments?.length ?? 0) > 0; | ||
|
||
return { | ||
applicationAssessments, | ||
assessmentsWithArchetypes, | ||
someArchetypesAssessed, | ||
allArchetypesAssessed, | ||
hasInProgressOrNotStartedRequiredAssessments, | ||
assessedArchetypesWithARequiredAssessment, | ||
assessedArchetypeCount, | ||
hasAssessmentInProgress, | ||
isDirectlyAssessed, | ||
}; | ||
}, [assessments, archetypes, application]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...ons/components/application-assessment-status/tests/application-assessment-status.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import "@testing-library/jest-dom"; | ||
import { useAssessmentStatus } from "@app/hooks/useAssessmentStatus"; | ||
import { | ||
createMockApplication, | ||
createMockArchetype, | ||
createMockAssessment, | ||
} from "@app/test-config/test-utils"; | ||
|
||
describe("useAssessmentStatus", () => { | ||
it("Correctly calculates status given one started assessment and one complete assessment for an application", () => { | ||
const mockAssessments = [ | ||
createMockAssessment({ | ||
id: 1, | ||
application: { id: 1, name: "app1" }, | ||
questionnaire: { id: 1, name: "questionnaire1" }, | ||
status: "started", | ||
}), | ||
|
||
createMockAssessment({ | ||
id: 2, | ||
application: { id: 1, name: "app1" }, | ||
questionnaire: { id: 2, name: "questionnaire2" }, | ||
status: "complete", | ||
}), | ||
]; | ||
|
||
const mockArchetypes = [ | ||
createMockArchetype({ | ||
id: 1, | ||
name: "archetype1", | ||
applications: [{ id: 1, name: "app1" }], | ||
}), | ||
]; | ||
|
||
const mockApplication = createMockApplication({ id: 1, name: "app1" }); | ||
|
||
const { result } = renderHook(() => | ||
useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) | ||
); | ||
expect(result.current).toEqual({ | ||
applicationAssessments: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
id: expect.any(Number), | ||
required: true, | ||
status: "started", | ||
}), | ||
expect.objectContaining({ | ||
id: expect.any(Number), | ||
required: true, | ||
status: "complete", | ||
}), | ||
]), | ||
assessmentsWithArchetypes: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
inheritedArchetype: expect.any(Object), | ||
assessments: expect.any(Array), | ||
}), | ||
]), | ||
someArchetypesAssessed: false, | ||
allArchetypesAssessed: false, | ||
hasInProgressOrNotStartedRequiredAssessments: false, | ||
assessedArchetypesWithARequiredAssessment: expect.any(Array), | ||
assessedArchetypeCount: 0, | ||
hasAssessmentInProgress: true, | ||
}); | ||
}); | ||
|
||
it("Correctly calculates status given two complete assessments for an application", () => { | ||
const mockAssessments = [ | ||
createMockAssessment({ | ||
id: 1, | ||
application: { id: 1, name: "app1" }, | ||
questionnaire: { id: 1, name: "questionnaire1" }, | ||
status: "complete", | ||
}), | ||
|
||
createMockAssessment({ | ||
id: 2, | ||
application: { id: 1, name: "app1" }, | ||
questionnaire: { id: 2, name: "questionnaire2" }, | ||
status: "complete", | ||
}), | ||
]; | ||
|
||
const mockArchetypes = [ | ||
createMockArchetype({ | ||
id: 1, | ||
name: "archetype1", | ||
applications: [{ id: 1, name: "app1" }], | ||
}), | ||
]; | ||
|
||
const mockApplication = createMockApplication({ | ||
id: 1, | ||
name: "app1", | ||
assessed: true, | ||
assessments: mockAssessments, | ||
}); | ||
|
||
const { result } = renderHook(() => | ||
useAssessmentStatus(mockAssessments, mockArchetypes, mockApplication) | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
applicationAssessments: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
id: expect.any(Number), | ||
required: true, | ||
status: "complete", | ||
}), | ||
expect.objectContaining({ | ||
id: expect.any(Number), | ||
required: true, | ||
status: "complete", | ||
}), | ||
]), | ||
assessmentsWithArchetypes: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
inheritedArchetype: expect.any(Object), | ||
assessments: expect.any(Array), | ||
}), | ||
]), | ||
someArchetypesAssessed: false, | ||
allArchetypesAssessed: false, | ||
hasInProgressOrNotStartedRequiredAssessments: false, | ||
assessedArchetypesWithARequiredAssessment: expect.any(Array), | ||
assessedArchetypeCount: 0, | ||
hasAssessmentInProgress: true, | ||
isDirectlyAssessed: true, | ||
}); | ||
}); | ||
it("Correctly calculates status given no complete assessments for an application", () => {}); | ||
it("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 a direct assessment", () => {}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters