Skip to content

Commit d483516

Browse files
committed
Implement test and story
Signed-off-by: Milos Dzepina <milos@aragon.org>
1 parent 4766dfe commit d483516

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/core/components/clipboard/clipboard.stories.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Meta, StoryObj } from '@storybook/react-vite';
2+
import { Button } from '../button';
23
import { Link } from '../link';
34
import { Clipboard } from './clipboard';
45

@@ -54,4 +55,26 @@ export const WithText: Story = {
5455
),
5556
};
5657

58+
/**
59+
* Example of the Clipboard component inside a form, demonstrating that clicking the copy
60+
* button does not trigger form submission.
61+
*/
62+
export const InsideForm: Story = {
63+
args: {
64+
copyValue: '0x123456789',
65+
},
66+
render: (props) => (
67+
<form
68+
className="flex flex-col gap-6"
69+
onSubmit={(e) => {
70+
e.preventDefault();
71+
alert('Form submitted!');
72+
}}
73+
>
74+
<Clipboard {...props} />
75+
<Button type="submit">Submit form</Button>
76+
</form>
77+
),
78+
};
79+
5780
export default meta;

src/core/components/clipboard/clipboard.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ describe('<Clipboard /> component', () => {
5656
expect(handleCopySpy).toHaveBeenCalledWith(textToCopy);
5757
});
5858

59+
it('does not trigger form submission when clicked', async () => {
60+
const handleSubmit = jest.fn();
61+
62+
const variants: IClipboardProps['variant'][] = ['avatar', 'avatar-white-bg', 'button'];
63+
64+
for (const variant of variants) {
65+
handleSubmit.mockReset();
66+
const { unmount } = render(
67+
<form onSubmit={handleSubmit}>
68+
{createTestComponent({ variant })}
69+
</form>,
70+
);
71+
72+
await userEvent.click(screen.getByRole('button'));
73+
74+
expect(handleSubmit).not.toHaveBeenCalled();
75+
unmount();
76+
}
77+
});
78+
5979
it('optionally renders children besides the clipboard', () => {
6080
const childText = 'Child text';
6181
const icon = IconType.COPY;

0 commit comments

Comments
 (0)