|
| 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