Skip to content

Commit

Permalink
fix: submit check without unsaved modal prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
ckbedwell committed Jan 7, 2025
1 parent 4ad984d commit 17d7fb3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 94 deletions.
5 changes: 4 additions & 1 deletion src/components/CheckForm/CheckForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ export const CheckForm = ({ check, disabled }: CheckFormProps) => {
</Stack>
);

const hasUnsavedChanges = checkHasChanges(defaultValues, formMethods.getValues());
const hasUnsavedChanges = error
? true
: checkHasChanges(defaultValues, formMethods.getValues()) && !formMethods.formState.isSubmitSuccessful;

const navModel = useMemo(() => {
return isExistingCheck
? createNavModel(
Expand Down

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions src/page/EditCheck/__tests__/EditCheck.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ describe(`<EditCheck />`, () => {
const submitButton = await screen.findByTestId(DataTestIds.CHECK_FORM_SUBMIT_BUTTON);
expect(submitButton).toBeDisabled();
});

it(`disables the save button when no edits have been made`, async () => {
await renderEditForm(BASIC_HTTP_CHECK.id);
expect(await screen.findByTestId(DataTestIds.CHECK_FORM_SUBMIT_BUTTON)).not.toBeEnabled();
});
});
36 changes: 36 additions & 0 deletions src/page/NewCheck/__tests__/NewCheck.journey.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,42 @@ describe(`<NewCheck /> journey`, () => {
expect(testButton).toBeDisabled();
});

it(`disables the submit button by default`, async () => {
await renderNewForm(CheckType.HTTP);
expect(screen.getByTestId(DataTestIds.CHECK_FORM_SUBMIT_BUTTON)).not.toBeEnabled();
});

it(`enables the submit button when a field is edited`, async () => {
const { user } = await renderNewForm(CheckType.HTTP);
const jobNameInput = await screen.findByLabelText('Job name', { exact: false });
await user.type(jobNameInput, 'My Job Name');
const submitButton = await screen.findByTestId(DataTestIds.CHECK_FORM_SUBMIT_BUTTON);
expect(submitButton).toBeEnabled();
});

it(`has the save button enabled after a failed submission`, async () => {
const { user } = await renderNewForm(CheckType.HTTP);

server.use(
apiRoute(`addCheck`, {
result: () => {
return {
status: 409,
json: {
err: 'target/job combination already exists',
msg: 'Failed to add check to database',
},
};
},
})
);

await fillMandatoryFields({ user, checkType: CheckType.HTTP });
await submitForm(user);
const submitButton = await screen.findByTestId(DataTestIds.CHECK_FORM_SUBMIT_BUTTON);
expect(submitButton).toBeEnabled();
});

// jsdom doesn't give us back the submitter of the form, so we can't test this
// https://github.com/jsdom/jsdom/issues/3117
it.skip(`should show an error message when it fails to test a check`, async () => {});
Expand Down

0 comments on commit 17d7fb3

Please sign in to comment.