Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix : Fixed Improper Handling for Non-Numeric Values in Allotted hours section of Create Action item modal #3417

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
53 changes: 53 additions & 0 deletions src/screens/OrganizationActionItems/ItemModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,48 @@ describe('Testing ItemModal', () => {
});
});

it('should not allow letters or negative values in allotted hours', async () => {
renderItemModal(link1, itemProps[0]);

const allottedHours = screen.getByLabelText(t.allottedHours);
expect(allottedHours).toBeInTheDocument();

// Test letter input
fireEvent.change(allottedHours, { target: { value: 'abc' } });
await waitFor(() => {
expect(allottedHours).toHaveValue('');
});

// Test negative value
fireEvent.change(allottedHours, { target: { value: '-5' } });
await waitFor(() => {
expect(allottedHours).toHaveValue('');
});

// Test valid positive number
fireEvent.change(allottedHours, { target: { value: '5' } });
await waitFor(() => {
expect(allottedHours).toHaveValue('5');
});
});

it('validates allottedHours edge cases', async () => {
renderItemModal(link1, itemProps[0]);
const allottedHours = screen.getByLabelText(t.allottedHours);

// Test invalid string
fireEvent.change(allottedHours, { target: { value: 'invalid' } });
expect(allottedHours).toHaveValue('');

// Test NaN
fireEvent.change(allottedHours, { target: { value: NaN } });
expect(allottedHours).toHaveValue('');

// Test negative number
fireEvent.change(allottedHours, { target: { value: -5 } });
expect(allottedHours).toHaveValue('');
});

it('should fail to Create Action Item', async () => {
renderItemModal(link2, itemProps[0]);
// Click Submit
Expand All @@ -802,6 +844,17 @@ describe('Testing ItemModal', () => {
});
});

it('handles null date change', async () => {
renderItemModal(link1, itemProps[0]);

const dateInput = screen.getByLabelText(t.dueDate);
fireEvent.change(dateInput, { target: { value: null } });

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

it('should fail to Update Action Item', async () => {
renderItemModal(link2, itemProps[2]);
expect(screen.getAllByText(t.updateActionItem)).toHaveLength(2);
Expand Down
16 changes: 14 additions & 2 deletions src/screens/OrganizationActionItems/ItemModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@
field: keyof InterfaceFormStateType,
value: string | number | boolean | Date | undefined | null,
): void => {
// Special handling for allottedHours
if (field === 'allottedHours') {
// If the value is not a valid number or is negative, set to null
const numValue = typeof value === 'string' ? Number(value) : value;
if (
typeof numValue !== 'number' ||
Number.isNaN(numValue) ||
numValue < 0
) {
setFormState((prevState) => ({ ...prevState, [field]: null }));
return;

Check warning on line 239 in src/screens/OrganizationActionItems/ItemModal.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/OrganizationActionItems/ItemModal.tsx#L238-L239

Added lines #L238 - L239 were not covered by tests
}
}

setFormState((prevState) => ({ ...prevState, [field]: value }));
};

Expand Down Expand Up @@ -574,8 +588,6 @@
className={styles.noOutline}
value={dayjs(dueDate)}
onChange={(date: Dayjs | null): void => {
// Added istanbul ignore else, which will ignore else condition, we are not using else condition here
/* istanbul ignore else -- @preserve */
if (date) handleFormChange('dueDate', date.toDate());
}}
/>
Expand Down
Loading