- Notifications
You must be signed in to change notification settings - Fork1.1k
fix: only allow submitting form if changes have been made#14602
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
DanielleMaywood merged 5 commits intomainfromdm-disable-submit-button-for-workspace-params-when-no-changesSep 13, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
13ca57d fix: only allow submitting form if dirty
DanielleMaywood0f4657d test: add test for submit button behaviour
DanielleMaywoodc947bc6 fix: apply 'make fmt'
DanielleMaywood230de7f chore: rename 'Submit' to 'Submit and restart'
DanielleMaywoodbdc659d test: fix tests
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletionsite/src/pages/WorkspaceSettingsPage/WorkspaceParametersPage/WorkspaceParametersForm.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
59 changes: 58 additions & 1 deletion.../src/pages/WorkspaceSettingsPage/WorkspaceParametersPage/WorkspaceParametersPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -61,7 +61,9 @@ test("Submit the workspace settings page successfully", async () => { | ||
| ); | ||
| await user.clear(parameter2); | ||
| await user.type(parameter2, "1"); | ||
| await user.click( | ||
| within(form).getByRole("button", { name: "Submit and restart" }), | ||
| ); | ||
| // Assert that the API calls were made with the correct data | ||
| await waitFor(() => { | ||
| expect(postWorkspaceBuildSpy).toHaveBeenCalledWith(MockWorkspace.id, { | ||
| @@ -73,3 +75,58 @@ test("Submit the workspace settings page successfully", async () => { | ||
| }); | ||
| }); | ||
| }); | ||
| test("Submit button is only enabled when changes are made", async () => { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think for now, having a test for this in Jest is ok, but in the future, you can try to usestorybook and interaction tests :) | ||
| // Mock the API calls that loads data | ||
| jest | ||
| .spyOn(API, "getWorkspaceByOwnerAndName") | ||
| .mockResolvedValueOnce(MockWorkspace); | ||
| jest.spyOn(API, "getTemplateVersionRichParameters").mockResolvedValueOnce([ | ||
| MockTemplateVersionParameter1, | ||
| MockTemplateVersionParameter2, | ||
| // Immutable parameters | ||
| MockTemplateVersionParameter4, | ||
| ]); | ||
| jest.spyOn(API, "getWorkspaceBuildParameters").mockResolvedValueOnce([ | ||
| MockWorkspaceBuildParameter1, | ||
| MockWorkspaceBuildParameter2, | ||
| // Immutable value | ||
| MockWorkspaceBuildParameter4, | ||
| ]); | ||
| // Setup event and rendering | ||
| const user = userEvent.setup(); | ||
| renderWithWorkspaceSettingsLayout(<WorkspaceParametersPage />, { | ||
| route: "/@test-user/test-workspace/settings", | ||
| path: "/:username/:workspace/settings", | ||
| // Need this because after submit the user is redirected | ||
| extraRoutes: [{ path: "/:username/:workspace", element: <div /> }], | ||
| }); | ||
| await waitForLoaderToBeRemoved(); | ||
| const submitButton: HTMLButtonElement = screen.getByRole("button", { | ||
| name: "Submit and restart", | ||
| }); | ||
| const form = screen.getByTestId("form"); | ||
| const parameter1 = within(form).getByLabelText( | ||
| MockWorkspaceBuildParameter1.name, | ||
| { exact: false }, | ||
| ); | ||
| // There are no changes, the button should be disabled. | ||
| expect(submitButton.disabled).toBeTruthy(); | ||
| // Make changes to the form | ||
| await user.clear(parameter1); | ||
| await user.type(parameter1, "new-value"); | ||
| // There are now changes, the button should be enabled. | ||
| expect(submitButton.disabled).toBeFalsy(); | ||
| // Change form value back to default | ||
| await user.clear(parameter1); | ||
| await user.type(parameter1, MockWorkspaceBuildParameter1.value); | ||
| // There are now no changes, the button should be disabled. | ||
| expect(submitButton.disabled).toBeTruthy(); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.