|
| 1 | +import { setupRtl } from '@codecademy/gamut-tests'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { act, useState } from 'react'; |
| 4 | + |
| 5 | +import { Tab, TabList, TabPanel, TabPanels, Tabs, TabsProps } from '../index'; |
| 6 | + |
| 7 | +const FullTabs = (props: TabsProps) => ( |
| 8 | + <Tabs {...props}> |
| 9 | + <TabList> |
| 10 | + <Tab id="tab1">Tab 1</Tab> |
| 11 | + <Tab id="tab2">Tab 2</Tab> |
| 12 | + <Tab id="tab3" isDisabled> |
| 13 | + Tab 3 |
| 14 | + </Tab> |
| 15 | + </TabList> |
| 16 | + <TabPanels> |
| 17 | + <TabPanel id="tab1"> |
| 18 | + <p>tab 1 content</p> |
| 19 | + </TabPanel> |
| 20 | + <TabPanel id="tab2"> |
| 21 | + <p>tab 2 content</p> |
| 22 | + </TabPanel> |
| 23 | + <TabPanel id="tab3"> |
| 24 | + <p>tab 3 content</p> |
| 25 | + </TabPanel> |
| 26 | + </TabPanels> |
| 27 | + </Tabs> |
| 28 | +); |
| 29 | + |
| 30 | +const FullTabsControlled = (props: TabsProps) => { |
| 31 | + const [activeTab, setActiveTab] = useState(props.selectedKey ?? 'tab1'); |
| 32 | + return ( |
| 33 | + <Tabs |
| 34 | + {...props} |
| 35 | + selectedKey={activeTab} |
| 36 | + onSelectionChange={(newTab) => { |
| 37 | + props.onSelectionChange?.(newTab); |
| 38 | + setActiveTab(newTab); |
| 39 | + }} |
| 40 | + > |
| 41 | + <TabList> |
| 42 | + <Tab id="tab1">Tab 1</Tab> |
| 43 | + <Tab id="tab2">Tab 2</Tab> |
| 44 | + <Tab id="tab3" isDisabled> |
| 45 | + Tab 3 |
| 46 | + </Tab> |
| 47 | + </TabList> |
| 48 | + <TabPanels> |
| 49 | + <TabPanel id="tab1"> |
| 50 | + <p>tab 1 content</p> |
| 51 | + </TabPanel> |
| 52 | + <TabPanel id="tab2"> |
| 53 | + <p>tab 2 content</p> |
| 54 | + </TabPanel> |
| 55 | + <TabPanel id="tab3"> |
| 56 | + <p>tab 3 content</p> |
| 57 | + </TabPanel> |
| 58 | + </TabPanels> |
| 59 | + </Tabs> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +const mockOnSelectionChange = jest.fn(); |
| 64 | +const renderView = setupRtl(FullTabs, { |
| 65 | + onSelectionChange: mockOnSelectionChange, |
| 66 | +}); |
| 67 | +const renderViewControlled = setupRtl(FullTabsControlled, { |
| 68 | + onSelectionChange: mockOnSelectionChange, |
| 69 | +}); |
| 70 | + |
| 71 | +describe('Tabs', () => { |
| 72 | + describe('Uncontrolled', () => { |
| 73 | + it('renders the first tab and first tab panel when no default selected key is passed', () => { |
| 74 | + const { view } = renderView(); |
| 75 | + |
| 76 | + view.getByText('Tab 1'); |
| 77 | + view.getByText('tab 1 content'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('renders the second tab tab panel and calls onSelectionChange when second tab is clicked', async () => { |
| 81 | + const { view } = renderView(); |
| 82 | + |
| 83 | + view.getByText('tab 1 content'); |
| 84 | + |
| 85 | + await act(() => userEvent.click(view.getByText('Tab 2'))); |
| 86 | + |
| 87 | + view.getByText('tab 2 content'); |
| 88 | + expect(mockOnSelectionChange).toHaveBeenCalledWith('tab2'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('renders the default selected key when passed', () => { |
| 92 | + const { view } = renderView({ defaultSelectedKey: 'tab2' }); |
| 93 | + |
| 94 | + view.getByText('Tab 2'); |
| 95 | + view.getByText('tab 2 content'); |
| 96 | + }); |
| 97 | + }); |
| 98 | + describe('Controlled', () => { |
| 99 | + it('renders the first tab and first tab panel', () => { |
| 100 | + const { view } = renderViewControlled(); |
| 101 | + |
| 102 | + view.getByText('Tab 1'); |
| 103 | + view.getByText('tab 1 content'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('renders new tab panel and calls onSelectionChange when a tab is clicked', async () => { |
| 107 | + const { view } = renderViewControlled(); |
| 108 | + |
| 109 | + view.getByText('tab 1 content'); |
| 110 | + |
| 111 | + await act(() => userEvent.click(view.getByText('Tab 2'))); |
| 112 | + |
| 113 | + expect(mockOnSelectionChange).toHaveBeenCalledWith('tab2'); |
| 114 | + view.getByText('tab 2 content'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + describe('Disabled', () => { |
| 118 | + it('renders tab as disabled when isDisabled is passed', () => { |
| 119 | + const { view } = renderView(); |
| 120 | + |
| 121 | + const tab = view.getByText('Tab 3'); |
| 122 | + expect(tab).toHaveAttribute('aria-disabled', 'true'); |
| 123 | + expect(tab).toHaveStyle('opacity: 0.25'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('renders tab keys as disabled when disabledKeys is passed', () => { |
| 127 | + const { view } = renderView({ disabledKeys: ['tab2'] }); |
| 128 | + |
| 129 | + const tab = view.getByText('Tab 2'); |
| 130 | + expect(tab).toHaveAttribute('aria-disabled', 'true'); |
| 131 | + expect(tab).toHaveStyle('opacity: 0.25'); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments