Skip to content

Commit

Permalink
fix: group size field validation when no limits are set
Browse files Browse the repository at this point in the history
PT-1772.

The group sizes can be left empty, especially when 1 group is set to
fill the event. In those cases, the group size children and adult inputs
should not validate themselves against the unset limits. The fields
should only be required when the limits are not set.
  • Loading branch information
nikomakela committed Oct 7, 2024
1 parent 62f4108 commit 6524c7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/domain/enrolment/enrolmentForm/EnrolmentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const EnrolmentForm: React.FC<Props> = ({
submitting,
onCloseForm,
minGroupSize,
maxGroupSize = 20,
maxGroupSize,
actionType = 'enrolment',
}) => {
const { t } = useTranslation();
Expand Down
32 changes: 19 additions & 13 deletions src/domain/enrolment/enrolmentForm/ValidationSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as Yup from 'yup';
import { VALIDATION_MESSAGE_KEYS } from '../../app/forms/constants';

type EnrolmentFormValidationSchemaProps = {
minGroupSize: number;
maxGroupSize: number;
minGroupSize?: number;
maxGroupSize?: number;
isQueueEnrolment: boolean;
};

Expand Down Expand Up @@ -55,10 +55,12 @@ export default function getValidationSchema({
// The used validation error message will be the same for both the fields.
// This is also preventing negative param.max to occur in validation.
if (
!sizePair ||
sizePair < 0 ||
sizePair < minGroupSize ||
sizePair > maxGroupSize
minGroupSize &&
maxGroupSize &&
(!sizePair ||
sizePair < 0 ||
sizePair < minGroupSize ||
sizePair > maxGroupSize)
) {
return (
schema
Expand All @@ -78,13 +80,17 @@ export default function getValidationSchema({
);
}

// After the field pair is given, count how many seats are left
// and use that as max limit.
// The used validation error message will be unique for both the fields.
return schema.max(maxGroupSize - sizePair, (param) => ({
max: param.max,
key: fieldMaxLimitValidationMessage,
}));
if (maxGroupSize && sizePair) {
// After the field pair is given, count how many seats are left
// and use that as max limit.
// The used validation error message will be unique for both the fields.
return schema.max(maxGroupSize - sizePair, (param) => ({
max: param.max,
key: fieldMaxLimitValidationMessage,
}));
}

return schema;
};

return schema.shape(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ test('render and focuses error notification correctly', async () => {

describe('max group size validation of the Children and Adults -fields', () => {
const createEnrolmentForm = async (
childrenCount: string,
adultsCount: string,
childrenCount: string | undefined,
adultsCount: string | undefined,
minGroupSize: number | undefined = 10,
maxGroupSize: number | undefined = 20
) => {
Expand Down Expand Up @@ -330,6 +330,13 @@ describe('max group size validation of the Children and Adults -fields', () => {
).toHaveLength(2);
});
});

test('The field is still required when the limits are unset', async () => {
await createEnrolmentForm(undefined, undefined, undefined, undefined);
await waitFor(() => {
expect(screen.getAllByText(/Tämä kenttä on pakollinen/i)).toHaveLength(2);
});
});
});

test('mandatory additional information forces extraNeeds field to be required', async () => {
Expand Down

0 comments on commit 6524c7c

Please sign in to comment.