Skip to content

Commit 178b905

Browse files
committed
IconButton.tsx: add unit test and fix icon resolve
1 parent 835b464 commit 178b905

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@
131131
"rules": {
132132
"no-use-before-define": "off",
133133
"import/no-extraneous-dependencies": "off",
134-
"no-unused-vars": "off"
134+
"no-unused-vars": "off",
135+
"react/require-default-props": "off"
135136
}
136137
},
137138
{

client/common/IconButton.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import { render, screen } from '../test-utils';
3+
import IconButton from './IconButton';
4+
5+
const MockIcon = (props: React.SVGProps<SVGSVGElement>) => (
6+
<svg data-testid="mock-icon" {...props} />
7+
);
8+
9+
describe('IconButton', () => {
10+
test('renders with an icon', () => {
11+
render(<IconButton icon={MockIcon} aria-label="test button" />);
12+
expect(screen.getByTestId('mock-icon')).toBeInTheDocument();
13+
expect(
14+
screen.getByRole('button', { name: 'test button' })
15+
).toBeInTheDocument();
16+
});
17+
18+
test('renders without an icon', () => {
19+
render(<IconButton />);
20+
expect(screen.queryByTestId('mock-icon')).not.toBeInTheDocument();
21+
});
22+
23+
test('passes other props to the button', () => {
24+
render(<IconButton icon={MockIcon} id="my-button" />);
25+
expect(screen.getByRole('button')).toHaveAttribute('id', 'my-button');
26+
});
27+
});

client/common/IconButton.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ type IconButtonProps = Omit<
2222
icon?: ComponentType<IconProps> | null
2323
};
2424

25-
const IconButton = ({ icon = null, ...otherProps }: IconButtonProps) => {
26-
const Icon = icon;
27-
28-
return (
29-
<ButtonWrapper
30-
iconBefore={icon && <Icon />}
31-
iconOnly
32-
display={Button.displays.inline}
33-
focusable="false"
34-
{...otherProps}
35-
/>
36-
);
37-
};
25+
const IconButton = ({ icon: Icon, ...otherProps }: IconButtonProps) => (
26+
<ButtonWrapper
27+
iconBefore={Icon ? <Icon /> : undefined}
28+
iconOnly
29+
display={Button.displays.inline}
30+
focusable="false"
31+
{...otherProps}
32+
/>
33+
);
3834

3935
export default IconButton;

0 commit comments

Comments
 (0)