Skip to content

Commit ebca55f

Browse files
authored
test(Control): test for Control added (#394)
* test(Control): test for Control added
1 parent c3c02ff commit ebca55f

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/components/Control/Control.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import './Control.scss';
1111

1212
const b = block('control');
1313

14+
export const defaultIconId = 'icon-test-id';
15+
1416
export interface ControlProps {
1517
icon: SVGIconData;
1618
theme?: 'primary' | 'secondary' | 'link' | 'accent';
@@ -19,6 +21,7 @@ export interface ControlProps {
1921
disabled?: boolean;
2022
className?: string;
2123
onClick?: (event: React.MouseEvent) => void;
24+
qa?: string;
2225
}
2326

2427
const Control = (props: ControlProps) => {
@@ -30,6 +33,7 @@ const Control = (props: ControlProps) => {
3033
disabled = false,
3134
onClick,
3235
className,
36+
qa,
3337
} = props;
3438

3539
return (
@@ -39,8 +43,9 @@ const Control = (props: ControlProps) => {
3943
className={b({size, theme, disabled}, className)}
4044
onClick={disabled ? undefined : onClick}
4145
disabled={disabled}
46+
data-qa={qa}
4247
>
43-
<Icon data={icon} size={iconSize} />
48+
<Icon data={icon} size={iconSize} qa={defaultIconId} />
4449
</button>
4550
);
4651
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React from 'react';
2+
3+
import {render, screen} from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
6+
import {testCustomClassName} from '../../../../test-utils/shared/common';
7+
import Control, {ControlProps, defaultIconId} from '../Control';
8+
9+
const qaId = 'control-component';
10+
11+
const icon = `<path d="M1 2H0v12h16v-2H1V2zm11.5 5.5L9 4 5.5 7.5 2 4v7h14V4l-3.5 3.5z"></path>`;
12+
const themes: ControlProps['theme'][] = ['primary', 'secondary', 'link', 'accent'];
13+
const sizes: ControlProps['size'][] = ['xs', 's', 'm', 'l'];
14+
15+
describe('Control', () => {
16+
test('render Control by default', async () => {
17+
render(<Control icon={icon} qa={qaId} />);
18+
const control = screen.getByTestId(qaId);
19+
20+
expect(control).toBeInTheDocument();
21+
expect(control).toBeVisible();
22+
expect(control).not.toBeDisabled();
23+
24+
const controlIcon = screen.getByTestId(defaultIconId);
25+
26+
expect(controlIcon).toHaveAttribute(`width`, '16');
27+
expect(controlIcon).toHaveAttribute(`height`, '16');
28+
});
29+
30+
test('add disabled', async () => {
31+
render(<Control icon={icon} qa={qaId} disabled />);
32+
const control = screen.getByTestId(qaId);
33+
34+
expect(control).toBeInTheDocument();
35+
expect(control).toBeVisible();
36+
expect(control).toBeDisabled();
37+
});
38+
39+
test('add className', () => {
40+
testCustomClassName<ControlProps>({
41+
component: Control,
42+
props: {icon, qa: qaId},
43+
});
44+
});
45+
46+
test.each(new Array<ControlProps['theme']>(...themes))(
47+
'render with given "%s" theme',
48+
(theme) => {
49+
render(<Control icon={icon} qa={qaId} theme={theme} />);
50+
const control = screen.getByTestId(qaId);
51+
52+
expect(control).toHaveClass(`pc-control_theme_${theme}`);
53+
},
54+
);
55+
56+
test.each(new Array<ControlProps['size']>(...sizes))('render with given "%s" size', (size) => {
57+
render(<Control icon={icon} qa={qaId} size={size} />);
58+
const control = screen.getByTestId(qaId);
59+
60+
expect(control).toHaveClass(`pc-control_size_${size}`);
61+
});
62+
63+
test('add iconSize', () => {
64+
const iconSize = 24;
65+
render(<Control icon={icon} qa={qaId} iconSize={iconSize} />);
66+
67+
const controlIcon = screen.getByTestId(defaultIconId);
68+
69+
expect(controlIcon).toHaveAttribute(`width`, iconSize.toString());
70+
expect(controlIcon).toHaveAttribute(`height`, iconSize.toString());
71+
});
72+
73+
test('add onClick', async () => {
74+
const handleOnClick = jest.fn();
75+
const user = userEvent.setup();
76+
render(<Control icon={icon} qa={qaId} onClick={handleOnClick} />);
77+
78+
const control = screen.getByTestId(qaId);
79+
80+
await user.click(control);
81+
expect(handleOnClick).toHaveBeenCalledTimes(1);
82+
});
83+
});

0 commit comments

Comments
 (0)