|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent } from '@testing-library/react'; |
| 3 | + |
| 4 | +import { renderWithTheme } from '../../../test/utils'; |
| 5 | +import { Tab } from '..'; |
| 6 | +import Tabs from './Tabs'; |
| 7 | + |
| 8 | +describe('<Tabs />', () => { |
| 9 | + describe('prop: children', () => { |
| 10 | + it('should accept a null child', () => { |
| 11 | + const { getAllByRole } = renderWithTheme( |
| 12 | + <Tabs value={0}> |
| 13 | + {null} |
| 14 | + <Tab /> |
| 15 | + </Tabs> |
| 16 | + ); |
| 17 | + expect(getAllByRole('tab').length).toBe(1); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('prop: value', () => { |
| 22 | + const tabs = ( |
| 23 | + <Tabs value={1}> |
| 24 | + <Tab value={0} /> |
| 25 | + <Tab value={1} /> |
| 26 | + </Tabs> |
| 27 | + ); |
| 28 | + |
| 29 | + it('should pass selected prop to children', () => { |
| 30 | + const { getAllByRole } = renderWithTheme(tabs); |
| 31 | + const tabElements = getAllByRole('tab'); |
| 32 | + expect(tabElements[0]).toHaveAttribute('aria-selected', 'false'); |
| 33 | + expect(tabElements[1]).toHaveAttribute('aria-selected', 'true'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should accept any value as selected tab value', () => { |
| 37 | + const tab0 = {}; |
| 38 | + const tab1 = {}; |
| 39 | + expect(tab0).not.toBe(tab1); |
| 40 | + |
| 41 | + const { getAllByRole } = renderWithTheme( |
| 42 | + <Tabs value={tab0}> |
| 43 | + <Tab value={tab0} /> |
| 44 | + <Tab value={tab1} /> |
| 45 | + </Tabs> |
| 46 | + ); |
| 47 | + const tabElements = getAllByRole('tab'); |
| 48 | + expect(tabElements[0]).toHaveAttribute('aria-selected', 'true'); |
| 49 | + expect(tabElements[1]).toHaveAttribute('aria-selected', 'false'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + describe('prop: onChange', () => { |
| 53 | + it('should call onChange when clicking', () => { |
| 54 | + const handleChange = jest.fn(); |
| 55 | + const { getAllByRole } = renderWithTheme( |
| 56 | + <Tabs value={0} onChange={handleChange}> |
| 57 | + <Tab value={0} /> |
| 58 | + <Tab value={1} /> |
| 59 | + </Tabs> |
| 60 | + ); |
| 61 | + |
| 62 | + fireEvent.click(getAllByRole('tab')[1]); |
| 63 | + expect(handleChange).toBeCalledTimes(1); |
| 64 | + expect(handleChange).toHaveBeenCalledWith(1); |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments