-
Notifications
You must be signed in to change notification settings - Fork 39
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
161fba8
commit c161f6f
Showing
5 changed files
with
438 additions
and
94 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,77 @@ | ||
// hooks/useAssessmentStatus.js | ||
import { Assessment, Archetype, Application } from "@app/api/models"; | ||
import { useFetchArchetypes } from "@app/queries/archetypes"; | ||
import { useFetchAssessments } from "@app/queries/assessments"; | ||
import { useMemo } from "react"; | ||
|
||
export const useAssessmentStatus = (application: Application) => { | ||
const { assessments } = useFetchAssessments(); | ||
const { archetypes } = useFetchArchetypes(); | ||
|
||
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 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 assessmentsFromArchetypesCount = assessmentsWithArchetypes.filter( | ||
({ assessments }) => assessments.some((assessment) => assessment.required) | ||
).length; | ||
|
||
const assessedArchetypesCount = assessmentsWithArchetypes.filter( | ||
({ assessments, inheritedArchetype }) => | ||
assessments.some( | ||
(assessment) => | ||
assessment.required && | ||
assessment.status === "complete" && | ||
inheritedArchetype.assessed | ||
) | ||
).length; | ||
|
||
const hasApplicationAssessmentInProgress = applicationAssessments?.some( | ||
(assessment: Assessment) => | ||
assessment.status === "started" || | ||
assessment.status === "empty" || | ||
assessment.status === "complete" | ||
); | ||
const isDirectlyAssessed = | ||
(application.assessed && (application.assessments?.length ?? 0) > 0) ?? | ||
false; | ||
|
||
return { | ||
allArchetypesAssessed, | ||
countOfFullyAssessedArchetypes: assessedArchetypesCount, | ||
countOfArchetypesWithRequiredAssessments: assessmentsFromArchetypesCount, | ||
hasApplicationAssessmentInProgress, | ||
isApplicationDirectlyAssessed: 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
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
Oops, something went wrong.