|
| 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 BackLink, {Theme} from '../BackLink'; |
| 7 | +import {History, LocationContext} from '../../../context/locationContext'; |
| 8 | + |
| 9 | +const backLinkProps = { |
| 10 | + url: '#', |
| 11 | + title: 'Button Title', |
| 12 | + theme: 'default' as Theme, |
| 13 | + size: 's' as ButtonSize, |
| 14 | + className: 'customClassName', |
| 15 | + shouldHandleBackAction: true, |
| 16 | + onClick: () => {}, |
| 17 | +}; |
| 18 | + |
| 19 | +describe('BackLink', () => { |
| 20 | + test('Default render', async () => { |
| 21 | + render(<BackLink {...backLinkProps} />); |
| 22 | + const backLink = screen.getByRole('button'); |
| 23 | + expect(backLink).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + test('Has custom class', async () => { |
| 27 | + render(<BackLink {...backLinkProps} />); |
| 28 | + const backLink = screen.getByRole('button'); |
| 29 | + expect(backLink).toHaveClass(backLinkProps.className); |
| 30 | + }); |
| 31 | + |
| 32 | + test('Should render <a /> tag', async () => { |
| 33 | + render(<BackLink {...backLinkProps} shouldHandleBackAction={false} />); |
| 34 | + const backLink = screen.getByRole('link'); |
| 35 | + expect(backLink).toBeVisible(); |
| 36 | + expect(backLink).toHaveAttribute('href', backLinkProps.url); |
| 37 | + }); |
| 38 | + |
| 39 | + test('Should render title', async () => { |
| 40 | + render(<BackLink {...backLinkProps} />); |
| 41 | + const backLink = screen.getByText(backLinkProps.title); |
| 42 | + expect(backLink).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + test('Call onClick', async () => { |
| 46 | + const user = userEvent.setup(); |
| 47 | + const handleClick = jest.fn(); |
| 48 | + render( |
| 49 | + <LocationContext.Provider value={{history: {push: jest.fn()} as unknown as History}}> |
| 50 | + <BackLink {...backLinkProps} onClick={handleClick} /> |
| 51 | + </LocationContext.Provider>, |
| 52 | + ); |
| 53 | + const backLink = screen.getByRole('button'); |
| 54 | + await user.click(backLink); |
| 55 | + expect(handleClick).toHaveBeenCalledTimes(1); |
| 56 | + }); |
| 57 | + |
| 58 | + test.each(new Array<ButtonSize>('s', 'm', 'l', 'xl'))('Render with given "%s" size', (size) => { |
| 59 | + render(<BackLink {...backLinkProps} size={size} />); |
| 60 | + const backLink = screen.getByRole('button'); |
| 61 | + expect(backLink).toHaveClass(`yc-button_size_${size}`); |
| 62 | + }); |
| 63 | + |
| 64 | + test.each(new Array<Theme>('default', 'special'))('Render with given "%s" theme', (theme) => { |
| 65 | + const matchView: Record<Theme, string> = { |
| 66 | + default: 'flat-secondary', |
| 67 | + special: 'flat-contrast', |
| 68 | + }; |
| 69 | + render(<BackLink {...backLinkProps} theme={theme} />); |
| 70 | + const backLink = screen.getByRole('button'); |
| 71 | + expect(backLink).toHaveClass(`yc-button_view_${matchView[theme]}`); |
| 72 | + }); |
| 73 | +}); |
0 commit comments