Skip to content
Merged
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
12 changes: 4 additions & 8 deletions src/components/HrTools/PdsGoalCalculator/Setup/SetupStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,10 @@
.positive(t('Hours Worked must be a positive number')),
otherwise: (s) => s.optional().nullable(),
}),
benefits: yup.number().when('status', {
is: (val: string) => val !== DesignationSupportStatus.PartTime,
then: (s) =>
s
.required(t('Benefits is a required field'))
.positive(t('Benefits must be a positive number')),
otherwise: (s) => s.optional().nullable(),
}),
benefits: yup
Comment thread
wjames111 marked this conversation as resolved.
.number()
.nullable()
.positive(t('Benefits must be a positive number')),

Check notice on line 71 in src/components/HrTools/PdsGoalCalculator/Setup/SetupStep.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

✅ Getting better: Large Method

SetupStep:React.FC decreases from 244 to 240 lines of code, threshold = 100. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
}),
[t],
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { MenuItem } from '@mui/material';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as yup from 'yup';
import {
Expand Down Expand Up @@ -221,28 +221,60 @@ describe('AutosaveTextField', () => {
</PdsGoalCalculatorTestWrapper>,
);

it('hides validation error for an empty untouched field', async () => {
it('shows validation error for an empty required field on load', async () => {
Comment thread
wjames111 marked this conversation as resolved.
const { findByRole } = renderRequired();

const input = await findByRole('textbox', { name: 'Goal Name' });
await waitFor(() => expect(input).toHaveValue(''));

expect(input).toHaveAccessibleDescription('Enter the goal name');
expect(input).not.toHaveAttribute('aria-invalid', 'true');
await waitFor(() =>
expect(input).toHaveAccessibleDescription('Goal Name is required'),
);
expect(input).toHaveAttribute('aria-invalid', 'true');
});

it('shows validation error after the field is touched', async () => {
it('clears validation error after user fills required field', async () => {
const { findByRole } = renderRequired();

const input = await findByRole('textbox', { name: 'Goal Name' });
await waitFor(() => expect(input).toHaveValue(''));
await waitFor(() =>
expect(input).toHaveAttribute('aria-invalid', 'true'),
);

fireEvent.focus(input);
fireEvent.blur(input);
userEvent.type(input, 'Filled in');

await waitFor(() =>
expect(input).toHaveAccessibleDescription('Goal Name is required'),
expect(input).not.toHaveAttribute('aria-invalid', 'true'),
);
expect(input).toHaveAccessibleDescription('Enter the goal name');
});
});

describe('optional field', () => {
const optionalSchema = yup.object({
name: yup.string().nullable(),
});

it('does not show validation error for an empty optional field on load', async () => {
const { findByRole } = render(
<PdsGoalCalculatorTestWrapper
calculationMock={{ ...calculationMock, name: '' }}
onCall={mutationSpy}
>
<AutosaveTextField
label="Goal Name"
fieldName="name"
schema={optionalSchema}
helperText="Enter the goal name"
/>
</PdsGoalCalculatorTestWrapper>,
);

const input = await findByRole('textbox', { name: 'Goal Name' });
await waitFor(() => expect(input).toHaveValue(''));

expect(input).not.toHaveAttribute('aria-invalid', 'true');
expect(input).toHaveAccessibleDescription('Enter the goal name');
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React from 'react';
import { TextField, TextFieldProps } from '@mui/material';
import * as yup from 'yup';
import { DesignationSupportCalculationUpdateInput } from 'src/graphql/types.generated';
Expand All @@ -23,11 +23,8 @@ export const AutosaveTextField: React.FC<AutosaveTextFieldProps> = ({
schema,
saveOnChange: props.select,
});
const [touched, setTouched] = useState(false);

const showValidationError = Boolean(
fieldProps.error && (touched || fieldProps.value !== ''),
);
const showValidationError = Boolean(fieldProps.error);
Comment thread
wjames111 marked this conversation as resolved.

return (
<TextField
Expand All @@ -36,14 +33,6 @@ export const AutosaveTextField: React.FC<AutosaveTextFieldProps> = ({
variant="outlined"
{...fieldProps}
{...props}
onChange={(event) => {
setTouched(true);
fieldProps.onChange?.(event as React.ChangeEvent<HTMLInputElement>);
}}
onBlur={() => {
setTouched(true);
fieldProps.onBlur?.();
}}
disabled={fieldProps.disabled || props.disabled}
error={showValidationError || undefined}
helperText={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ describe('isSetupComplete', () => {
expect(isSetupComplete(partTime)).toBe(true);
});

it('requires benefits when status is Full-time', () => {
expect(isSetupComplete({ ...baseCalculation, benefits: null })).toBe(false);
it('does not require benefits when status is Full-time', () => {
expect(isSetupComplete({ ...baseCalculation, benefits: null })).toBe(true);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
DesignationSupportSalaryType,
DesignationSupportStatus,
} from 'src/graphql/types.generated';
import { DesignationSupportSalaryType } from 'src/graphql/types.generated';
import { PdsGoalCalculationFieldsFragment } from '../GoalsList/PdsGoalCalculations.generated';
import { PdsSummaryData } from '../calculations/usePdsSummaryData';

Expand All @@ -14,15 +11,13 @@ export const isSetupComplete = (calculation: Calculation): boolean => {

const isSalaried =
calculation.salaryOrHourly === DesignationSupportSalaryType.Salaried;
const isPartTime = calculation.status === DesignationSupportStatus.PartTime;

return Boolean(
calculation.name &&
calculation.status &&
calculation.salaryOrHourly &&
(calculation.payRate ?? 0) > 0 &&
(isSalaried || (calculation.hoursWorkedPerWeek ?? 0) > 0) &&
(isPartTime || (calculation.benefits ?? 0) > 0),
(isSalaried || (calculation.hoursWorkedPerWeek ?? 0) > 0),
);
};

Expand Down
Loading