|
| 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 ButtonTabs, {ButtonTabsItemProps} from '../ButtonTabs'; |
| 7 | + |
| 8 | +const qaId = 'button-tabs-component'; |
| 9 | + |
| 10 | +const items: ButtonTabsItemProps[] = [ |
| 11 | + { |
| 12 | + id: '0', |
| 13 | + title: 'tab-1', |
| 14 | + }, |
| 15 | + { |
| 16 | + id: '1', |
| 17 | + title: 'tab-2', |
| 18 | + }, |
| 19 | + { |
| 20 | + id: '2', |
| 21 | + title: 'tab-3', |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +describe('ButtonTabs', () => { |
| 26 | + test('render ButtonTabs by default', async () => { |
| 27 | + render(<ButtonTabs items={items} qa={qaId} />); |
| 28 | + const buttonTabs = screen.getByTestId(qaId); |
| 29 | + |
| 30 | + expect(buttonTabs).toBeInTheDocument(); |
| 31 | + expect(buttonTabs).toBeVisible(); |
| 32 | + expect(buttonTabs).not.toBeDisabled(); |
| 33 | + }); |
| 34 | + |
| 35 | + test('has active tab', async () => { |
| 36 | + const activeTabId = 1; |
| 37 | + render(<ButtonTabs items={items} qa={qaId} activeTab={String(activeTabId)} />); |
| 38 | + const buttons = screen.getAllByRole('button'); |
| 39 | + |
| 40 | + buttons.forEach((button, index) => { |
| 41 | + if (index === activeTabId) { |
| 42 | + expect(button).toHaveClass('pc-button-tabs__item_active'); |
| 43 | + } |
| 44 | + |
| 45 | + expect(button).toHaveClass('pc-button-block_theme_normal'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + test('add className', () => { |
| 50 | + const className = 'my-class'; |
| 51 | + |
| 52 | + render(<ButtonTabs items={items} qa={qaId} className={className} />); |
| 53 | + const buttonTabs = screen.getByTestId(qaId); |
| 54 | + |
| 55 | + expect(buttonTabs).toHaveClass(className); |
| 56 | + }); |
| 57 | + |
| 58 | + test('call onSelectTab', async () => { |
| 59 | + const user = userEvent.setup(); |
| 60 | + const handleOnClick = jest.fn(); |
| 61 | + render(<ButtonTabs items={items} qa={qaId} onSelectTab={handleOnClick} />); |
| 62 | + |
| 63 | + const buttons = screen.getAllByRole('button'); |
| 64 | + |
| 65 | + buttons.forEach(async (button, i) => { |
| 66 | + await user.click(button); |
| 67 | + expect(handleOnClick).toHaveBeenCalledTimes(i + 1); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments