Skip to content

Commit 59f66ac

Browse files
committed
Button: update unit test and add iconOnly test
1 parent 8068493 commit 59f66ac

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

client/common/Button.test.tsx

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,75 @@ const MockIcon = (props: React.SVGProps<SVGSVGElement>) => (
88
);
99

1010
describe('Button', () => {
11-
it('renders children', () => {
12-
render(<Button>Click Me</Button>);
13-
expect(screen.getByText('Click Me')).toBeInTheDocument();
14-
});
15-
16-
it('calls onClick handler when clicked', () => {
17-
const handleClick = jest.fn();
18-
render(<Button onClick={handleClick}>Click</Button>);
19-
fireEvent.click(screen.getByText('Click'));
20-
expect(handleClick).toHaveBeenCalledTimes(1);
21-
});
22-
11+
// Tag
2312
it('renders as an anchor when href is provided', () => {
2413
render(<Button href="https://example.com">Link</Button>);
2514
const anchor = screen.getByRole('link');
15+
expect(anchor.tagName.toLowerCase()).toBe('a');
2616
expect(anchor).toHaveAttribute('href', 'https://example.com');
2717
});
2818

29-
it('renders as a React Router link when `to` is provided', () => {
19+
it('renders as a React Router <Link> when `to` is provided', () => {
3020
render(<Button to="/dashboard">Go</Button>);
3121
const link = screen.getByRole('link');
22+
expect(link.tagName.toLowerCase()).toBe('a'); // Link renders as <a>
3223
expect(link).toHaveAttribute('href', '/dashboard');
3324
});
3425

35-
it('renders with iconBefore', () => {
26+
it('renders as a <button> by default', () => {
27+
render(<Button>Click Me</Button>);
28+
const el = screen.getByRole('button');
29+
expect(el.tagName.toLowerCase()).toBe('button');
30+
});
31+
32+
// Children & Icons
33+
it('renders children', () => {
34+
render(<Button>Click Me</Button>);
35+
expect(screen.getByText('Click Me')).toBeInTheDocument();
36+
});
37+
38+
it('renders an iconBefore and button text', () => {
39+
render(
40+
<Button iconBefore={<MockIcon aria-label="iconbefore" />}>
41+
This has a before icon
42+
</Button>
43+
);
44+
expect(screen.getByLabelText('iconbefore')).toBeInTheDocument();
45+
expect(screen.getByRole('button')).toHaveTextContent(
46+
'This has a before icon'
47+
);
48+
});
49+
50+
it('renders with iconAfter', () => {
3651
render(
37-
<Button iconBefore={<MockIcon aria-label="github" />}>Create</Button>
52+
<Button iconAfter={<MockIcon aria-label="iconafter" />}>
53+
This has an after icon
54+
</Button>
55+
);
56+
expect(screen.getByLabelText('iconafter')).toBeInTheDocument();
57+
expect(screen.getByRole('button')).toHaveTextContent(
58+
'This has an after icon'
3859
);
39-
expect(screen.getByLabelText('github')).toBeInTheDocument();
60+
});
61+
62+
it('renders only the icon if iconOnly', () => {
63+
render(
64+
<Button iconAfter={<MockIcon aria-label="iconafter" />} iconOnly>
65+
This has an after icon
66+
</Button>
67+
);
68+
expect(screen.getByLabelText('iconafter')).toBeInTheDocument();
69+
expect(screen.getByRole('button')).not.toHaveTextContent(
70+
'This has an after icon'
71+
);
72+
});
73+
74+
// HTML attributes
75+
it('calls onClick handler when clicked', () => {
76+
const handleClick = jest.fn();
77+
render(<Button onClick={handleClick}>Click</Button>);
78+
fireEvent.click(screen.getByText('Click'));
79+
expect(handleClick).toHaveBeenCalledTimes(1);
4080
});
4181

4282
it('renders disabled state', () => {

client/common/Button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const Button = ({
201201
const content = (
202202
<>
203203
{iconBefore}
204-
{hasChildren && <span>{children}</span>}
204+
{hasChildren && !iconOnly && <span>{children}</span>}
205205
{iconAfter}
206206
</>
207207
);

0 commit comments

Comments
 (0)