Skip to content

Commit

Permalink
Submit the form immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Dec 6, 2024
1 parent 8bae54b commit 59cdef8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,60 @@ describe('MonthlyGoalAccordion', () => {
});
});

it('resets goal to calculated goal', async () => {
const { getByRole, findByText } = render(
<Components
monthlyGoal={1000}
machineCalculatedGoal={1500}
expandedPanel={label}
/>,
);
const input = getByRole('spinbutton', { name: label });

expect(
await findByText(
'Based on the past year, NetSuite estimates that you need at least $1,500 of monthly support. You can use this amount or choose your own target monthly goal.',
),
).toBeInTheDocument();

const resetButton = getByRole('button', { name: /Reset/ });
userEvent.click(resetButton);
expect(input).toHaveValue(1500);
expect(resetButton).not.toBeInTheDocument();
describe('calculated goal', () => {
it('resets goal to calculated goal', async () => {
const { getByRole, findByText } = render(
<Components
monthlyGoal={1000}
machineCalculatedGoal={1500}
expandedPanel={label}
/>,
);
const input = getByRole('spinbutton', { name: label });

expect(
await findByText(
'Based on the past year, NetSuite estimates that you need at least $1,500 of monthly support. You can use this amount or choose your own target monthly goal.',
),
).toBeInTheDocument();

const resetButton = getByRole('button', { name: /Reset/ });
userEvent.click(resetButton);
expect(input).toHaveValue(1500);
expect(resetButton).not.toBeInTheDocument();

await waitFor(() =>
expect(mutationSpy).toHaveGraphqlOperation('UpdateAccountPreferences', {
input: {
id: accountListId,
attributes: {
settings: {
monthlyGoal: 1500,
},
},
},
}),
);
});

userEvent.clear(input);
userEvent.type(input, '500');
expect(getByRole('button', { name: /Reset/ })).toBeInTheDocument();
it('hides reset button if goal matches calculated goal', async () => {
const { getByRole, findByText, queryByRole } = render(
<Components
monthlyGoal={1000}
machineCalculatedGoal={1000}
expandedPanel={label}
/>,
);

expect(
await findByText(
'Based on the past year, NetSuite estimates that you need at least $1,000 of monthly support. You can use this amount or choose your own target monthly goal.',
),
).toBeInTheDocument();
expect(queryByRole('button', { name: /Reset/ })).not.toBeInTheDocument();

userEvent.type(getByRole('spinbutton', { name: label }), '0');
expect(getByRole('button', { name: /Reset/ })).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const MonthlyGoalAccordion: React.FC<MonthlyGoalAccordionProps> = ({
values: { monthlyGoal },
errors,
handleSubmit,
submitForm,
isSubmitting,
isValid,
handleChange,
Expand Down Expand Up @@ -180,7 +181,10 @@ export const MonthlyGoalAccordion: React.FC<MonthlyGoalAccordionProps> = ({
<Button
variant="outlined"
type="button"
onClick={() => setFieldValue('monthlyGoal', calculatedGoal)}
onClick={() => {
setFieldValue('monthlyGoal', calculatedGoal);
submitForm();
}}
>
{t('Reset to Calculated Goal')}
</Button>
Expand Down

0 comments on commit 59cdef8

Please sign in to comment.