Skip to content

Commit

Permalink
JN-590 - fixing consent form view for past versions (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
devonbush authored Sep 26, 2023
1 parent e6c3855 commit abab0e6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
41 changes: 38 additions & 3 deletions ui-admin/src/study/participants/enrolleeView/EnrolleeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'

import EnrolleeView from './EnrolleeView'
import { setupRouterTest } from 'test-utils/router-testing-utils'
import { mockEnrollee, mockStudyEnvContext, taskForSurvey } from 'test-utils/mocking-utils'
import { mockEnrollee, mockStudyEnvContext, taskForForm } from 'test-utils/mocking-utils'
import { render, screen } from '@testing-library/react'


Expand All @@ -19,7 +19,6 @@ test('renders survey links for configured surveys', async () => {
expect(surveyLink.querySelector('span')).toBeNull()
})


test('renders survey taken badges', async () => {
const studyEnvContext = mockStudyEnvContext()
const enrollee = mockEnrollee()
Expand All @@ -30,7 +29,8 @@ test('renders survey taken badges', async () => {
complete: false,
enrolleeId: enrollee.id
})
enrollee.participantTasks.push(taskForSurvey(studyEnvContext.currentEnv.configuredSurveys[0].survey, enrollee.id))
enrollee.participantTasks
.push(taskForForm(studyEnvContext.currentEnv.configuredSurveys[0].survey, enrollee.id, false))

const { RoutedComponent } = setupRouterTest(
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand All @@ -42,3 +42,38 @@ test('renders survey taken badges', async () => {
})


test('renders consent links for configured consents', async () => {
const studyEnvContext = mockStudyEnvContext()
const enrollee = mockEnrollee()

const { RoutedComponent } = setupRouterTest(
// eslint-disable-next-line @typescript-eslint/no-empty-function
<EnrolleeView enrollee={enrollee} studyEnvContext={studyEnvContext} onUpdate={() => {}}/>)
render(RoutedComponent)
const surveyLink = screen.getByText('Mock consent')
// should have no badge since the enrollee hasn't completed the consent
expect(surveyLink.querySelector('span')).toBeNull()
})

test('renders consent taken badges', async () => {
const studyEnvContext = mockStudyEnvContext()
const enrollee = mockEnrollee()
enrollee.consentResponses.push({
consentFormId: studyEnvContext.currentEnv.configuredConsents[0].consentFormId,
resumeData: '',
consented: true,
completed: true,
fullData: '',
enrolleeId: enrollee.id
})
enrollee.participantTasks
.push(taskForForm(studyEnvContext.currentEnv.configuredConsents[0].consentForm, enrollee.id, true))

const { RoutedComponent } = setupRouterTest(
// eslint-disable-next-line @typescript-eslint/no-empty-function
<EnrolleeView enrollee={enrollee} studyEnvContext={studyEnvContext} onUpdate={() => {}}/>)
render(RoutedComponent)
const consentLink = screen.getByText('Mock consent')
// should show a completed checkmark
expect(consentLink.querySelector('title')?.textContent).toEqual('completed')
})
12 changes: 9 additions & 3 deletions ui-admin/src/study/participants/enrolleeView/EnrolleeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ export default function EnrolleeView({ enrollee, studyEnvContext, onUpdate }:
.filter(task => task.targetStableId === configSurvey.survey.stableId)
.map(task => task.surveyResponseId)
const matchedResponses = enrollee.surveyResponses
.filter(response => matchedResponseIds.includes(response.id as string))
.filter(response => matchedResponseIds.includes(response.id))
responseMap[configSurvey.survey.stableId] = { survey: configSurvey, responses: matchedResponses }
})

const consents = currentEnv.configuredConsents
const consentMap: ConsentResponseMapT = {}
consents.forEach(configConsent => {
// to match responses to consents, filter using the tasks, since those have the stableIds
// this is valid since it's currently enforced that all consents are done as part of a task,
const matchedResponseIds = enrollee.participantTasks
.filter(task => task.targetStableId === configConsent.consentForm.stableId)
.map(task => task.consentResponseId)
const matchedResponses = enrollee.consentResponses
.filter(response => configConsent.consentForm.id === response.consentFormId)
.filter(response => matchedResponseIds.includes(response.id))
consentMap[configConsent.consentForm.stableId] = { consent: configConsent, responses: matchedResponses }
})

Expand Down Expand Up @@ -103,7 +108,8 @@ export default function EnrolleeView({ enrollee, studyEnvContext, onUpdate }:
<NavLink to={`consents/${stableId}`} className={getLinkCssClasses}>
{ consent.consentForm.name }
{ isConsented(consentMap[stableId].responses) &&
<FontAwesomeIcon className="text-success ms-2 fa-lg" icon={faCheck}/>
<FontAwesomeIcon className="text-success ms-2 fa-lg" icon={faCheck}
title="completed"/>
}
</NavLink>
</li>
Expand Down
52 changes: 45 additions & 7 deletions ui-admin/src/test-utils/mocking-utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { StudyEnvContextT } from 'study/StudyEnvironmentRouter'
import { DatasetDetails, Enrollee, KitRequest, KitType, NotificationConfig, ParticipantNote, Portal } from 'api/api'
import {
ConsentForm,
DatasetDetails,
Enrollee,
KitRequest,
KitType,
NotificationConfig,
ParticipantNote,
Portal,
StudyEnvironmentConsent
} from 'api/api'
import { Survey } from '@juniper/ui-core/build/types/forms'
import { ParticipantTask } from '@juniper/ui-core/build/types/task'

Expand Down Expand Up @@ -80,7 +90,7 @@ export const mockStudyEnvContext: () => StudyEnvContextT = () => ({
currentEnv: {
environmentName: 'sandbox',
id: 'studyEnvId',
configuredConsents: [],
configuredConsents: [mockConfiguredConsent()],
configuredSurveys: [mockConfiguredSurvey()],
notificationConfigs: [],
studyEnvironmentConfig: {
Expand Down Expand Up @@ -112,6 +122,33 @@ export const mockConfiguredSurvey: () => StudyEnvironmentSurvey = () => {
}
}

/** Mock StudyEnvironmentConsent */
export const mockConfiguredConsent = (): StudyEnvironmentConsent => {
return {
id: 'fakeGuid',
consentFormId: 'consentId1',
consentOrder: 1,
consentForm: mockConsentForm(),
allowAdminEdit: false,
allowParticipantReedit: false,
allowParticipantStart: true,
prepopulate: false
}
}

/** fake ConsentForm */
export const mockConsentForm = (): ConsentForm => {
return {
id: 'fakeGuid2',
content: '{"pages": []}',
stableId: 'form1',
version: 1,
name: 'Mock consent',
createdAt: 0,
lastUpdatedAt: 0
}
}

export const mockDatasetDetails: (datasetName: string, status: string) => DatasetDetails =
/** returns mock dataset for use/extension in tests */
(datasetName: string, status: string) => ({
Expand Down Expand Up @@ -203,7 +240,8 @@ export const mockEnrollee: () => Enrollee = () => {
}

/** helper function to generate a ParticipantTask object for a survey and enrollee */
export const taskForSurvey = (survey: Survey, enrolleeId: string): ParticipantTask => {
export const taskForForm = (form: Survey | ConsentForm, enrolleeId: string,
isConsent: boolean): ParticipantTask => {
return {
id: randomString(10),
blocksHub: false,
Expand All @@ -212,10 +250,10 @@ export const taskForSurvey = (survey: Survey, enrolleeId: string): ParticipantTa
portalParticipantUserId: randomString(10),
status: 'NEW',
studyEnvironmentId: randomString(10),
taskType: 'SURVEY',
targetName: survey.name,
targetStableId: survey.stableId,
targetAssignedVersion: survey.version,
taskType: isConsent ? 'CONSENT' : 'SURVEY',
targetName: form.name,
targetStableId: form.stableId,
targetAssignedVersion: form.version,
taskOrder: 1
}
}
Expand Down

0 comments on commit abab0e6

Please sign in to comment.