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

If wrapped in form don't submit when clicking buttons #89

Merged
merged 1 commit into from
Jul 15, 2024
Merged
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
If wrapped in form don't submit when clicking buttons
  • Loading branch information
mbarton committed Jul 15, 2024
commit dc18cc5061d0667ad586b964a3f5a07ff323fc34
17 changes: 16 additions & 1 deletion src/CentileChart/CentileChart.test.tsx
Original file line number Diff line number Diff line change
@@ -1005,7 +1005,14 @@ describe("All tests relating to testing the copy button", () => {
fireEvent.mouseOver(screen.getByTestId('copy-button'));
expect(screen.getByTestId('copy-button')).toHaveStyle('background-color: ButtonFace');
})

it("should not trigger form submission when copy button clicked", () => {
const onSubmit = jest.fn();
render(<form onSubmit={onSubmit}>
<CentileChart {...props} />
</form>);
fireEvent.click(screen.getByTestId('copy-button'));
expect(onSubmit).not.toHaveBeenCalled();
});
})

describe("Tests relating to exportChartCallback function", () => {
@@ -1132,6 +1139,14 @@ describe("All tests relating to the zoom functionality where enableZoom needs to
fireEvent.click(screen.getByTestId('zoom-button'));
expect(screen.queryByTestId("resetzoom-button")).toBeInTheDocument();
})
it("should not trigger form submission when zoom button clicked", () => {
const onSubmit = jest.fn();
render(<form onSubmit={onSubmit}>
<CentileChart {...props} />
</form>);
fireEvent.click(screen.getByTestId('zoom-button'));
expect(onSubmit).not.toHaveBeenCalled();
});
})

describe("Tests relating to negative settings on the zoom button", () => {
7 changes: 7 additions & 0 deletions src/SubComponents/CommonButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

export const CommonButton = ({ children, ...props }) => {
// if chart wrapped in a form tag prevent submitting it
// as the default button type=submit
return <button {...props} type="button">{children}</button>;
}
3 changes: 2 additions & 1 deletion src/SubComponents/StyledErrorButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledErrorButton = styled.button<{
export const StyledErrorButton = styled(CommonButton)<{
$activeColour: string;
$inactiveColour: string;
fontFamily: string;
3 changes: 2 additions & 1 deletion src/SubComponents/StyledFullScreenButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledFullScreenButton = styled.button<{
export const StyledFullScreenButton = styled(CommonButton)<{
$color?: string,
size?: number
}>`
3 changes: 2 additions & 1 deletion src/SubComponents/StyledResetZoomButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledResetZoomButton = styled.button<{
export const StyledResetZoomButton = styled(CommonButton)<{
$activeColour: string;
$inactiveColour: string;
$fontFamily: string;
3 changes: 2 additions & 1 deletion src/SubComponents/StyledShareButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledShareButton = styled.button<{
export const StyledShareButton = styled(CommonButton)<{
$color?: string,
size?: number
}>`