|
| 1 | +import React from 'react'; |
| 2 | +import {render, screen} from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import {ButtonSize} from '@gravity-ui/uikit'; |
| 5 | + |
| 6 | +import {ButtonImagePosition, ButtonTheme} from '../../../models'; |
| 7 | +import {ICON_QA} from '../utils'; |
| 8 | + |
| 9 | +import Button from '../Button'; |
| 10 | + |
| 11 | +const qaId = 'button-component'; |
| 12 | + |
| 13 | +const buttonProps = { |
| 14 | + text: 'Button Text', |
| 15 | + url: 'https://github.com/gravity-ui/', |
| 16 | + target: '_blank', |
| 17 | + img: { |
| 18 | + url: 'https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/icon_1_light.svg', |
| 19 | + position: 'left' as ButtonImagePosition, |
| 20 | + alt: 'alt-text', |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const buttonViews: ButtonTheme[] = [ |
| 25 | + 'normal', |
| 26 | + 'action', |
| 27 | + 'outlined', |
| 28 | + 'outlined-info', |
| 29 | + 'outlined-danger', |
| 30 | + 'raised', |
| 31 | + 'flat', |
| 32 | + 'flat-info', |
| 33 | + 'flat-danger', |
| 34 | + 'flat-secondary', |
| 35 | + 'normal-contrast', |
| 36 | + 'outlined-contrast', |
| 37 | + 'flat-contrast', |
| 38 | + 'github', |
| 39 | + 'scale', |
| 40 | + 'monochrome', |
| 41 | +]; |
| 42 | + |
| 43 | +describe('Button', () => { |
| 44 | + test('render button by default', async () => { |
| 45 | + render(<Button text={buttonProps.text} />); |
| 46 | + const button = screen.getByRole('button'); |
| 47 | + |
| 48 | + expect(button).toBeInTheDocument(); |
| 49 | + expect(button).toBeVisible(); |
| 50 | + expect(button).not.toBeDisabled(); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should render <a /> tag', async () => { |
| 54 | + render( |
| 55 | + <Button text={buttonProps.text} url={buttonProps.url} target={buttonProps.target} />, |
| 56 | + ); |
| 57 | + const button = screen.getByRole('link'); |
| 58 | + |
| 59 | + expect(button).toBeVisible(); |
| 60 | + expect(button).toHaveAttribute('href', buttonProps.url); |
| 61 | + expect(button).toHaveAttribute('target', buttonProps.target); |
| 62 | + }); |
| 63 | + |
| 64 | + test('call onClick', async () => { |
| 65 | + const user = userEvent.setup(); |
| 66 | + const handleOnClick = jest.fn(); |
| 67 | + render(<Button text={buttonProps.text} onClick={handleOnClick} />); |
| 68 | + |
| 69 | + const button = screen.getByRole('button'); |
| 70 | + |
| 71 | + await user.click(button); |
| 72 | + expect(handleOnClick).toHaveBeenCalledTimes(1); |
| 73 | + }); |
| 74 | + |
| 75 | + test.each(new Array<ButtonSize>('s', 'm', 'l', 'xl'))('render with given "%s" size', (size) => { |
| 76 | + render(<Button text={buttonProps.text} size={size} qa={qaId} />); |
| 77 | + const button = screen.getByTestId(qaId); |
| 78 | + |
| 79 | + expect(button).toHaveClass(`pc-button-block_size_${size}`); |
| 80 | + }); |
| 81 | + |
| 82 | + test.each(new Array<ButtonTheme>(...buttonViews))('render with given "%s" view', (theme) => { |
| 83 | + render(<Button text={buttonProps.text} theme={theme} qa={qaId} />); |
| 84 | + const button = screen.getByTestId(qaId); |
| 85 | + |
| 86 | + expect(button).toHaveClass(`pc-button-block_theme_${theme}`); |
| 87 | + }); |
| 88 | + |
| 89 | + test('add className', () => { |
| 90 | + const className = 'my-class'; |
| 91 | + |
| 92 | + render(<Button text={buttonProps.text} className={className} qa={qaId} />); |
| 93 | + const button = screen.getByTestId(qaId); |
| 94 | + |
| 95 | + expect(button).toHaveClass(className); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should render icon', () => { |
| 99 | + render(<Button text={buttonProps.text} img={buttonProps.img} />); |
| 100 | + |
| 101 | + const button = screen.getByRole('button'); |
| 102 | + const iconComponent = screen.getByRole('img'); |
| 103 | + |
| 104 | + expect(iconComponent).toBeVisible(); |
| 105 | + expect(button).toContainElement(iconComponent); |
| 106 | + }); |
| 107 | + |
| 108 | + test('should render github icon', () => { |
| 109 | + render(<Button text={buttonProps.text} img={buttonProps.img} theme="github" />); |
| 110 | + |
| 111 | + const button = screen.getByRole('button'); |
| 112 | + const iconComponent = screen.getByTestId(ICON_QA); |
| 113 | + |
| 114 | + expect(iconComponent).toBeVisible(); |
| 115 | + expect(button).toContainElement(iconComponent); |
| 116 | + }); |
| 117 | +}); |
0 commit comments