|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + defaultTheme, |
| 5 | + Item, |
| 6 | + Provider, |
| 7 | + TabList, |
| 8 | + Tabs, |
| 9 | +} from '@adobe/react-spectrum'; |
| 10 | +import { DHCTabPanels } from './TabPanels'; |
| 11 | + |
| 12 | +function Counter({ label }: { label: string }) { |
| 13 | + const [count, setCount] = useState(0); |
| 14 | + return ( |
| 15 | + <div> |
| 16 | + <button type="button" onClick={() => setCount(count + 1)}> |
| 17 | + {label}: {count} |
| 18 | + </button> |
| 19 | + </div> |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +describe('TabPanels', () => { |
| 24 | + it('should not persist panel state by default when switching tabs', () => { |
| 25 | + render( |
| 26 | + <Provider theme={defaultTheme}> |
| 27 | + <Tabs aria-label="test"> |
| 28 | + <TabList> |
| 29 | + <Item key="1">Tab 1</Item> |
| 30 | + <Item key="2">Tab 2</Item> |
| 31 | + </TabList> |
| 32 | + <DHCTabPanels> |
| 33 | + <Item key="1"> |
| 34 | + <Counter label="foo" /> |
| 35 | + </Item> |
| 36 | + <Item key="2"> |
| 37 | + <Counter label="bar" /> |
| 38 | + </Item> |
| 39 | + </DHCTabPanels> |
| 40 | + </Tabs> |
| 41 | + </Provider> |
| 42 | + ); |
| 43 | + |
| 44 | + screen.getByRole('button', { name: /foo/ }).click(); |
| 45 | + expect(screen.getByText('foo: 1')).toBeInTheDocument(); |
| 46 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 47 | + |
| 48 | + screen.getByText('Tab 2', { selector: 'span' }).click(); |
| 49 | + expect(screen.queryByText(/foo/)).not.toBeInTheDocument(); |
| 50 | + expect(screen.queryByText('bar: 0')).toBeInTheDocument(); |
| 51 | + |
| 52 | + screen.getByText('Tab 1', { selector: 'span' }).click(); |
| 53 | + expect(screen.getByText('foo: 0')).toBeInTheDocument(); |
| 54 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should persist panel state when keepMounted is true', () => { |
| 58 | + render( |
| 59 | + <Provider theme={defaultTheme}> |
| 60 | + <Tabs aria-label="test"> |
| 61 | + <TabList> |
| 62 | + <Item key="1">Tab 1</Item> |
| 63 | + <Item key="2">Tab 2</Item> |
| 64 | + </TabList> |
| 65 | + <DHCTabPanels keepMounted> |
| 66 | + <Item key="1"> |
| 67 | + <Counter label="foo" /> |
| 68 | + </Item> |
| 69 | + <Item key="2"> |
| 70 | + <Counter label="bar" /> |
| 71 | + </Item> |
| 72 | + </DHCTabPanels> |
| 73 | + </Tabs> |
| 74 | + </Provider> |
| 75 | + ); |
| 76 | + |
| 77 | + screen.getByRole('button', { name: /foo/ }).click(); |
| 78 | + expect(screen.getByText('foo: 1')).toBeInTheDocument(); |
| 79 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 80 | + |
| 81 | + screen.getByText('Tab 2', { selector: 'span' }).click(); |
| 82 | + expect(screen.queryByText(/foo/)).not.toBeInTheDocument(); |
| 83 | + expect(screen.queryByText('bar: 0')).toBeInTheDocument(); |
| 84 | + |
| 85 | + screen.getByText('Tab 1', { selector: 'span' }).click(); |
| 86 | + expect(screen.getByText('foo: 1')).toBeInTheDocument(); |
| 87 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should not persist panel state when using a render function', () => { |
| 91 | + const tabs = [ |
| 92 | + { |
| 93 | + id: '1', |
| 94 | + label: 'Tab 1', |
| 95 | + content: <Counter label="foo" />, |
| 96 | + }, |
| 97 | + { |
| 98 | + id: '2', |
| 99 | + label: 'Tab 2', |
| 100 | + content: <Counter label="bar" />, |
| 101 | + }, |
| 102 | + ]; |
| 103 | + type Tab = (typeof tabs)[0]; |
| 104 | + render( |
| 105 | + <Provider theme={defaultTheme}> |
| 106 | + <Tabs items={tabs} aria-label="test"> |
| 107 | + <TabList>{(tab: Tab) => <Item>{tab.label}</Item>}</TabList> |
| 108 | + <DHCTabPanels keepMounted> |
| 109 | + {(tab: Tab) => <Item>{tab.content}</Item>} |
| 110 | + </DHCTabPanels> |
| 111 | + </Tabs> |
| 112 | + </Provider> |
| 113 | + ); |
| 114 | + |
| 115 | + screen.getByRole('button', { name: /foo/ }).click(); |
| 116 | + expect(screen.getByText('foo: 1')).toBeInTheDocument(); |
| 117 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 118 | + |
| 119 | + screen.getByText('Tab 2', { selector: 'span' }).click(); |
| 120 | + expect(screen.queryByText(/foo/)).not.toBeInTheDocument(); |
| 121 | + expect(screen.queryByText('bar: 0')).toBeInTheDocument(); |
| 122 | + |
| 123 | + screen.getByText('Tab 1', { selector: 'span' }).click(); |
| 124 | + expect(screen.getByText('foo: 0')).toBeInTheDocument(); |
| 125 | + expect(screen.queryByText(/bar/)).not.toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should pass through style props', () => { |
| 129 | + render( |
| 130 | + <Provider theme={defaultTheme}> |
| 131 | + <Tabs aria-label="test"> |
| 132 | + <TabList> |
| 133 | + <Item key="1">Tab 1</Item> |
| 134 | + <Item key="2">Tab 2</Item> |
| 135 | + </TabList> |
| 136 | + <DHCTabPanels |
| 137 | + aria-label="panels" |
| 138 | + UNSAFE_style={{ backgroundColor: 'red' }} |
| 139 | + > |
| 140 | + <Item key="1"> |
| 141 | + <Counter label="foo" /> |
| 142 | + </Item> |
| 143 | + <Item key="2"> |
| 144 | + <Counter label="bar" /> |
| 145 | + </Item> |
| 146 | + </DHCTabPanels> |
| 147 | + </Tabs> |
| 148 | + </Provider> |
| 149 | + ); |
| 150 | + |
| 151 | + expect(screen.getByLabelText('panels')).toHaveStyle( |
| 152 | + 'background-color: red' |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
0 commit comments