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 @@ -21,9 +21,25 @@

const defaultSchema = yup.object({
mortgageOrRentPayment: yup.number().required('Mortgage Payment is required'),
avgUtilityOne: yup.number().nullable(),
avgUtilityTwo: yup.number().nullable(),
});

const TestComponent: React.FC = () => (
interface TestComponentProps {
fieldName?: keyof import('../../Steps/StepThree/Calculation').CalculationFormValues &
string;
additionalSaveFields?: Array<
keyof import('../../Steps/StepThree/Calculation').CalculationFormValues &
string
>;
requestAttributes?: Record<string, unknown>;
}

const TestComponent: React.FC<TestComponentProps> = ({
fieldName = 'mortgageOrRentPayment',
additionalSaveFields,
requestAttributes = { mortgageOrRentPayment: null },
}) => (
<ThemeProvider theme={theme}>
<SnackbarProvider>
<GqlMockedProvider<{
Expand All @@ -38,14 +54,15 @@
trackMutation,
requestData: {
id: 'request-id',
requestAttributes: { mortgageOrRentPayment: null },
requestAttributes,
},
} as unknown as ContextType
}
>
<Formik initialValues={{}} onSubmit={submit}>
<AutosaveCustomTextField
fieldName="mortgageOrRentPayment"
fieldName={fieldName}
additionalSaveFields={additionalSaveFields}
schema={defaultSchema}
/>
</Formik>
Expand Down Expand Up @@ -111,4 +128,62 @@

await waitFor(() => expect(input).toHaveValue(''));
});

it('saves to additional fields alongside the primary field', async () => {
const { getByRole } = render(
<TestComponent
fieldName="avgUtilityOne"
additionalSaveFields={['avgUtilityTwo']}
requestAttributes={{ avgUtilityOne: null, avgUtilityTwo: null }}
/>,
);

const input = getByRole('textbox');
userEvent.type(input, '200');
input.blur();

await waitFor(() =>
expect(mutationSpy).toHaveGraphqlOperation(
'UpdateMinistryHousingAllowanceRequest',
{
input: {
requestId: 'request-id',
requestAttributes: {
avgUtilityOne: 200,
avgUtilityTwo: 200,
},
},
},
),
);
});

it('saves null to all fields when additional fields are specified and input is cleared', async () => {
const { getByRole } = render(
<TestComponent
fieldName="avgUtilityOne"
additionalSaveFields={['avgUtilityTwo']}
requestAttributes={{ avgUtilityOne: 100, avgUtilityTwo: 100 }}
/>,
);

const input = getByRole('textbox');
userEvent.clear(input);
userEvent.tab();

await waitFor(() =>
expect(mutationSpy).toHaveGraphqlOperation(
'UpdateMinistryHousingAllowanceRequest',
{
input: {
requestId: 'request-id',
requestAttributes: {
avgUtilityOne: null,
avgUtilityTwo: null,
},
},
},
),
);
});

Check warning on line 188 in src/components/Reports/MinisterHousingAllowance/Shared/AutoSave/AutosaveCustomTextField.test.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: 'saves null to all fields when additional fields are specified and input is cleared','saves to additional fields alongside the primary field'. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TextField, TextFieldProps } from '@mui/material';
import { useFormikContext } from 'formik';
import * as yup from 'yup';
import { MinistryHousingAllowanceRequestAttributesInput } from 'pages/api/graphql-rest.page.generated';
import { PageEnum } from 'src/components/Reports/Shared/CalculationReports/Shared/sharedTypes';
import { useCustomAutoSave } from '../../../Shared/CalculationReports/CustomAutosave/useCustomAutosave';
import { CalculationFormValues } from '../../Steps/StepThree/Calculation';
Expand All @@ -14,12 +15,13 @@ export interface AutosaveCustomTextFieldProps
> {
variant?: 'standard' | 'outlined';
fieldName: keyof CalculationFormValues & string;
additionalSaveFields?: Array<keyof CalculationFormValues & string>;
schema: yup.Schema;
}

export const AutosaveCustomTextField: React.FC<
AutosaveCustomTextFieldProps
> = ({ variant, fieldName, schema, ...props }) => {
> = ({ variant, fieldName, additionalSaveFields, schema, ...props }) => {
const { pageType, requestData } = useMinisterHousingAllowance();
const request = requestData?.requestAttributes;

Expand All @@ -34,7 +36,14 @@ export const AutosaveCustomTextField: React.FC<

const fieldProps = useCustomAutoSave({
value: request?.[fieldName],
saveValue: (value) => saveField({ [fieldName]: value }),
saveValue: (value) => {
const attributes: Partial<MinistryHousingAllowanceRequestAttributesInput> =
{ [fieldName]: value };
additionalSaveFields?.forEach((field) => {
attributes[field] = value;
});
return saveField(attributes);
},
fieldName,
schema,
setFieldValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
placeholder={currencyFormat(0, currency, locale)}
InputProps={{ disableUnderline: true, inputMode: 'decimal' }}
fieldName="avgUtilityTwo"
additionalSaveFields={['avgUtilityOne']}

Check warning on line 156 in src/components/Reports/MinisterHousingAllowance/Steps/StepThree/CalcComponents/CostOfHome.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ Getting worse: Complex Method

CostOfHome:React.FC<CostOfHomeProps> already has high cyclomatic complexity, and now it increases in Lines of Code from 204 to 205. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
schema={schema}
/>
</TableCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
placeholder={currencyFormat(0, currency, locale)}
InputProps={{ disableUnderline: true, inputMode: 'decimal' }}
fieldName="avgUtilityOne"
additionalSaveFields={['avgUtilityTwo']}

Check warning on line 126 in src/components/Reports/MinisterHousingAllowance/Steps/StepThree/CalcComponents/FairRentalValue.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ Getting worse: Large Method

FairRentalValue:React.FC<FairRentalValueProps> increases from 145 to 146 lines of code, threshold = 120. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
schema={schema}
/>
</TableCell>
Expand Down
Loading