Skip to content

Commit

Permalink
Merge branch 'main' into fix/registration-form-touched-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen authored Sep 21, 2024
2 parents 99fe8b7 + 691f65b commit 0170a65
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getConfig } from '@openmrs/esm-framework';
import { type RegistrationConfig } from '../../config-schema';
import { getValidationSchema } from './patient-registration-validation';
import dayjs from 'dayjs';

const mockGetConfig = jest.mocked(getConfig);

Expand Down Expand Up @@ -144,7 +145,7 @@ describe('Patient registration validation', () => {
expect(validationError).toBeFalsy();
});

it('should throw error when date of birth is a future date', async () => {
it('should throw an error when date of birth is a future date', async () => {
const invalidFormValues = {
...validFormValues,
birthdate: new Date('2100-01-01'),
Expand All @@ -153,6 +154,15 @@ describe('Patient registration validation', () => {
expect(validationError.errors).toContain('birthdayNotInTheFuture');
});

it('should throw an error when date of birth is more than 140 years ago', async () => {
const invalidFormValues = {
...validFormValues,
birthdate: dayjs().subtract(141, 'years').toDate(),
};
const validationError = await validateFormValues(invalidFormValues);
expect(validationError.errors).toContain('birthdayNotOver140YearsAgo');
});

it('should require yearsEstimated when birthdateEstimated is true', async () => {
const invalidFormValues = {
...validFormValues,
Expand All @@ -162,7 +172,7 @@ describe('Patient registration validation', () => {
expect(validationError.errors).toContain('yearsEstimateRequired');
});

it('should throw error when monthEstimated is negative', async () => {
it('should throw an error when monthEstimated is negative', async () => {
const invalidFormValues = {
...validFormValues,
birthdateEstimated: true,
Expand All @@ -173,7 +183,17 @@ describe('Patient registration validation', () => {
expect(validationError.errors).toContain('negativeMonths');
});

it('should throw error when deathDate is in future', async () => {
it('should throw an error when yearsEstimated is more than 140', async () => {
const invalidFormValues = {
...validFormValues,
birthdateEstimated: true,
yearsEstimated: 141,
};
const validationError = await validateFormValues(invalidFormValues);
expect(validationError.errors).toContain('nonsensicalYears');
});

it('should throw an error when deathDate is in future', async () => {
const invalidFormValues = {
...validFormValues,
deathDate: new Date('2100-01-01'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ export function getValidationSchema(config: RegistrationConfig) {
then: Yup.date()
.required(t('birthdayRequired', 'Birthday is required'))
.max(Date(), t('birthdayNotInTheFuture', 'Birthday cannot be in future'))
.min(
dayjs().subtract(140, 'years').toDate(),
t('birthdayNotOver140YearsAgo', 'Birthday cannot be more than 140 years ago'),
)
.nullable(),
otherwise: Yup.date().nullable(),
}),
yearsEstimated: Yup.number().when('birthdateEstimated', {
is: true,
then: Yup.number()
.required(t('yearsEstimateRequired', 'Estimated years required'))
.min(0, t('negativeYears', 'Estimated years cannot be negative')),
.min(0, t('negativeYears', 'Estimated years cannot be negative'))
.max(140, t('nonsensicalYears', 'Estimated years cannot be more than 140')),
otherwise: Yup.number().nullable(),
}),
monthsEstimated: Yup.number().min(0, t('negativeMonths', 'Estimated months cannot be negative')),
Expand Down
2 changes: 2 additions & 0 deletions packages/esm-patient-registration-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"allFieldsRequiredText": "All fields are required unless marked optional",
"autoGeneratedPlaceholderText": "Auto-generated",
"birthdayNotInTheFuture": "Birthday cannot be in future",
"birthdayNotOver140YearsAgo": "Birthday cannot be more than 140 years ago",
"birthdayRequired": "Birthday is required",
"birthFieldLabelText": "Birth",
"cancel": "Cancel",
Expand Down Expand Up @@ -72,6 +73,7 @@
"no": "No",
"nonCodedCauseOfDeath": "Non-coded cause of death",
"nonCodedCauseOfDeathRequired": "Cause of death is required",
"nonsensicalYears": "Estimated years cannot be more than 140",
"numberInNameDubious": "Number in name is dubious",
"obsFieldUnknownDatatype": "Concept for obs field '{{fieldDefinitionId}}' has unknown datatype '{{datatypeName}}'",
"optional": "optional",
Expand Down

0 comments on commit 0170a65

Please sign in to comment.