Skip to content

Commit

Permalink
Merge branch 'MAIN-B-22621' into INT-B-22621
Browse files Browse the repository at this point in the history
  • Loading branch information
traskowskycaci committed Feb 17, 2025
2 parents 74a3d8e + f6060b5 commit cf83c45
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
7 changes: 3 additions & 4 deletions src/pages/Office/MoveAllowances/MoveAllowances.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ const validationSchema = Yup.object({
weightRestriction: Yup.number()
.transform((value) => (Number.isNaN(value) ? 0 : value))
.when('adminRestrictedWeightLocation', {
is: true, // Apply rules only if adminRestrictedWeightLocation is true
is: true,
then: (schema) =>
schema
.min(1, 'Weight restriction must be greater than 0')
.max(18000, 'Weight restriction cannot exceed 18,000 lbs')
.required('Weight restriction is required when Admin Restricted Weight Location is enabled'),
otherwise: (schema) => schema.notRequired().nullable(), // No validation when false
otherwise: (schema) => schema.notRequired().nullable(),
}),

adminRestrictedWeightLocation: Yup.boolean().notRequired(),
});

Expand Down Expand Up @@ -218,7 +217,7 @@ const MoveAllowances = () => {
<Restricted to={permissionTypes.updateAllowances}>
<div className={styles.bottom}>
<div className={styles.buttonGroup}>
<Button disabled={formik.isSubmitting} type="submit">
<Button disabled={formik.isSubmitting || !formik.isValid} type="submit">
Save
</Button>
<Button type="button" secondary onClick={handleClose}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,18 @@ const validationSchema = Yup.object({
.min(0, 'Storage in transit (days) must be greater than or equal to 0')
.transform((value) => (Number.isNaN(value) ? 0 : value))
.notRequired(),
adminRestrictedWeightLocation: Yup.bool(),
weightRestriction: Yup.number().when('adminRestrictedWeightLocation', {
is: (value) => {
return value === true;
},
then: (schema) => {
return schema
.required('Weight restriction is required when location is restricted')
.min(1, 'Weight restriction must be greater than 0')
.max(18000, 'Weight restriction cannot exceed 18,000 lbs')
.transform((value) => {
return Number.isNaN(value) ? 0 : value;
});
},
otherwise: (schema) => {
return schema
.transform((value) => {
return Number.isNaN(value) ? 0 : value;
})
.notRequired();
},
}),
weightRestriction: Yup.number()
.transform((value) => (Number.isNaN(value) ? 0 : value))
.when('adminRestrictedWeightLocation', {
is: true,
then: (schema) =>
schema
.min(1, 'Weight restriction must be greater than 0')
.max(18000, 'Weight restriction cannot exceed 18,000 lbs')
.required('Weight restriction is required when Admin Restricted Weight Location is enabled'),
otherwise: (schema) => schema.notRequired().nullable(),
}),
adminRestrictedWeightLocation: Yup.boolean().notRequired(),
});
const ServicesCounselingMoveAllowances = () => {
const { moveCode } = useParams();
Expand Down Expand Up @@ -107,6 +97,7 @@ const ServicesCounselingMoveAllowances = () => {
organizationalClothingAndIndividualEquipment,
storageInTransit,
gunSafe,
adminRestrictedWeightLocation,
weightRestriction,
accompaniedTour,
dependentsTwelveAndOver,
Expand All @@ -128,7 +119,7 @@ const ServicesCounselingMoveAllowances = () => {
storageInTransit: Number(storageInTransit),
organizationalClothingAndIndividualEquipment,
gunSafe,
weightRestriction: Number(weightRestriction),
weightRestriction: adminRestrictedWeightLocation && weightRestriction ? Number(weightRestriction) : null,
accompaniedTour,
dependentsTwelveAndOver: Number(dependentsTwelveAndOver),
dependentsUnderTwelve: Number(dependentsUnderTwelve),
Expand Down Expand Up @@ -160,12 +151,12 @@ const ServicesCounselingMoveAllowances = () => {
requiredMedicalEquipmentWeight: `${requiredMedicalEquipmentWeight}`,
storageInTransit: `${storageInTransit}`,
gunSafe,
weightRestriction: `${weightRestriction}`,
adminRestrictedWeightLocation: weightRestriction > 0,
weightRestriction: weightRestriction ? `${weightRestriction}` : '0',
organizationalClothingAndIndividualEquipment,
accompaniedTour,
dependentsUnderTwelve: `${dependentsUnderTwelve}`,
dependentsTwelveAndOver: `${dependentsTwelveAndOver}`,
adminRestrictedWeightLocation: false,
};

return (
Expand Down Expand Up @@ -202,7 +193,11 @@ const ServicesCounselingMoveAllowances = () => {
</div>
<div className={styles.bottom}>
<div className={styles.buttonGroup}>
<Button disabled={formik.isSubmitting} data-testid="scAllowancesSave" type="submit">
<Button
disabled={formik.isSubmitting || !formik.isValid}
data-testid="scAllowancesSave"
type="submit"
>
Save
</Button>
<Button type="button" secondary onClick={handleClose}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import ServicesCounselingMoveAllowances from 'pages/Office/ServicesCounselingMoveAllowances/ServicesCounselingMoveAllowances';
import { MockProviders } from 'testUtils';
import { useOrdersDocumentQueries } from 'hooks/queries';
import { permissionTypes } from 'constants/permissions';

const mockOriginDutyLocation = {
address: {
Expand Down Expand Up @@ -66,6 +68,7 @@ const useOrdersDocumentQueriesReturnValue = {
storageInTransit: 2,
totalDependents: 1,
totalWeight: 5000,
weightRestriction: 500,
},
first_name: 'Leo',
grade: 'E_1',
Expand Down Expand Up @@ -158,5 +161,45 @@ describe('MoveAllowances page', () => {

expect(screen.getByTestId('weightAllowance')).toHaveTextContent('5,000 lbs');
});

it('renders displays the allowances in the sidebar form and allows editing with correct permissions', async () => {
render(
<MockProviders permissions={[permissionTypes.updateAllowances]}>
<ServicesCounselingMoveAllowances />
</MockProviders>,
);

expect(await screen.findByTestId('proGearWeightInput')).toHaveDisplayValue('2,000');
expect(screen.getByTestId('proGearWeightSpouseInput')).toHaveDisplayValue('500');
expect(screen.getByTestId('rmeInput')).toHaveDisplayValue('1,000');
expect(screen.getByTestId('branchInput')).toHaveDisplayValue('Army');
expect(screen.getByTestId('sitInput')).toHaveDisplayValue('2');

expect(screen.getByLabelText('OCIE authorized (Army only)')).toBeChecked();
expect(screen.getByLabelText('Dependents authorized')).toBeChecked();

expect(screen.getByTestId('weightAllowance')).toHaveTextContent('5,000 lbs');
const adminWeightCheckbox = await screen.findByTestId('adminWeightLocation');
expect(adminWeightCheckbox).toBeChecked();
const weightRestrictionInput = screen.getByTestId('weightRestrictionInput');
expect(weightRestrictionInput).toHaveValue('500');

await userEvent.click(weightRestrictionInput);
await userEvent.clear(weightRestrictionInput);
await userEvent.type(weightRestrictionInput, '0');
fireEvent.blur(weightRestrictionInput);

await waitFor(() => {
expect(screen.getByText(/Weight restriction must be greater than 0/i)).toBeInTheDocument();
});

await userEvent.clear(weightRestrictionInput);

await waitFor(() => {
expect(
screen.getByText(/Weight restriction is required when Admin Restricted Weight Location is enabled/i),
).toBeInTheDocument();
});
});
});
});

0 comments on commit cf83c45

Please sign in to comment.