Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ export const StartSessionForm = ({
subjectId: z.string().optional(),
subjectDateOfBirth: z
.date()
.max(currentDate, {
message: t({
en: 'Date of birth must be in the past',
es: 'La fecha de nacimiento debe estar en el pasado',
fr: 'La date de naissance doit être dans le passé'
})
})
.optional()
.refine(
(date) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,39 @@ describe('StartSessionForm session type', () => {
expect(screen.getByText('Date Assessed')).toBeTruthy();
});
});

describe('StartSessionForm date of birth', () => {
/** The date input commits typed text when it loses focus, not on each keystroke. */
const enterDateOfBirth = (date: string) => {
fireEvent.focus(dateOfBirthInput());
fireEvent.change(dateOfBirthInput(), { target: { value: date } });
fireEvent.blur(dateOfBirthInput());
};

const enterNewIdentifier = () => {
typeIdentifier('gamma');
closeIdentifierPopup();
};

it('should reject a date of birth in the future, since no subject can be born after today', async () => {
const form = renderForm([]);
enterNewIdentifier();
enterDateOfBirth(`${new Date().getFullYear() + 1}-01-01`);
submit(form);
await waitFor(() =>
expect(errorMessages()).toContainEqual({
field: 'subjectDateOfBirth',
message: 'Date of birth must be in the past'
})
);
expect(onSubmit).not.toHaveBeenCalled();
});

it('should accept a date of birth in the past, so the check does not block a valid session', async () => {
const form = renderForm([]);
enterNewIdentifier();
enterDateOfBirth('1990-01-01');
await submitAndSettle(form);
expect(onSubmit.mock.lastCall?.[0].subjectData.dateOfBirth).toStrictEqual(new Date('1990-01-01'));
});
});
6 changes: 6 additions & 0 deletions testing/src/pages/_app/session/start-session.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export class StartSessionPage extends AppPage {
await this.fillSessionDetails(sex);
}

/** The date input commits what was typed only when it loses focus, so it is blurred explicitly. */
async fillDateOfBirth(date: string) {
await this.dateOfBirthField.fill(date);
await this.dateOfBirthField.blur();
}

/** Everything the form needs beyond how the subject was identified. */
async fillSessionDetails(sex: string) {
await this.fillSubjectDetails(sex);
Expand Down
18 changes: 18 additions & 0 deletions testing/src/specs/start-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ test.describe('start session', () => {
await expect(startSessionPage.subjectIdOption(identifier)).toBeVisible();
});

test('should refuse to start a session for a subject whose date of birth is in the future', async ({
getPageModel,
uniqueId
}) => {
const startSessionPage = await getPageModel('/session/start-session');
await startSessionPage.sessionForm.waitFor({ state: 'visible' });
await startSessionPage.selectIdentificationMethod('CUSTOM_ID');
await startSessionPage.typeSubjectId(`future-${uniqueId}`);
await startSessionPage.dismissSubjectIdOptions();

await startSessionPage.fillDateOfBirth(`${new Date().getFullYear() + 1}-01-01`);
await startSessionPage.sexField.selectOption('Male');
await startSessionPage.submitForm();

await expect(startSessionPage.errorMessages).toContainText(['Date of birth must be in the past']);
await expect(startSessionPage.successMessage).toHaveCount(0);
});

test('should show a required-field error for every missing field when submitting the personal information form empty', async ({
getPageModel
}) => {
Expand Down
Loading