|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import Tab from '../Tab'; |
| 5 | +import TabList from '../TabList'; |
| 6 | +import TabPanel from '../TabPanel'; |
| 7 | +import Tabs from '../Tabs'; |
| 8 | +import { reset as resetIdCounter } from '../../helpers/uuid'; |
| 9 | + |
| 10 | +describe('<Tabs />', () => { |
| 11 | + let consoleErrorMock; |
| 12 | + |
| 13 | + function assertPropTypeWarning(message, nth = 1) { |
| 14 | + expect(consoleErrorMock).toHaveBeenNthCalledWith( |
| 15 | + nth, |
| 16 | + expect.anything(), |
| 17 | + expect.anything(), |
| 18 | + expect.stringMatching(message), |
| 19 | + expect.anything(), |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + resetIdCounter(); |
| 25 | + consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + consoleErrorMock.mockRestore(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('errors', () => { |
| 33 | + test('should result with warning when tabs/panels are imbalanced and it should ignore non tab children', () => { |
| 34 | + render( |
| 35 | + <Tabs> |
| 36 | + <TabList> |
| 37 | + <Tab>Foo</Tab> |
| 38 | + <div>+</div> |
| 39 | + </TabList> |
| 40 | + |
| 41 | + <TabPanel>Hello Foo</TabPanel> |
| 42 | + <TabPanel>Hello Bar</TabPanel> |
| 43 | + </Tabs>, |
| 44 | + ); |
| 45 | + |
| 46 | + assertPropTypeWarning( |
| 47 | + "There should be an equal number of 'Tab' and 'TabPanel' in `Tabs`. Received 1 'Tab' and 2 'TabPanel'.", |
| 48 | + 1, |
| 49 | + ); |
| 50 | + assertPropTypeWarning( |
| 51 | + "There should be an equal number of 'Tab' and 'TabPanel' in `UncontrolledTabs`. Received 1 'Tab' and 2 'TabPanel'.", |
| 52 | + 2, |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should result with warning when multiple tablist components exist', () => { |
| 57 | + render( |
| 58 | + <Tabs> |
| 59 | + <TabList> |
| 60 | + <Tab>Foo</Tab> |
| 61 | + </TabList> |
| 62 | + <TabList> |
| 63 | + <Tab>Foo</Tab> |
| 64 | + </TabList> |
| 65 | + <TabPanel /> |
| 66 | + <TabPanel /> |
| 67 | + </Tabs>, |
| 68 | + ); |
| 69 | + |
| 70 | + assertPropTypeWarning( |
| 71 | + "Found multiple 'TabList' components inside 'Tabs'. Only one is allowed.", |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + test('should result with warning when tab outside of tablist', () => { |
| 76 | + render( |
| 77 | + <Tabs> |
| 78 | + <TabList> |
| 79 | + <Tab>Foo</Tab> |
| 80 | + </TabList> |
| 81 | + <Tab>Foo</Tab> |
| 82 | + <TabPanel /> |
| 83 | + <TabPanel /> |
| 84 | + </Tabs>, |
| 85 | + ); |
| 86 | + |
| 87 | + assertPropTypeWarning( |
| 88 | + "Found a 'Tab' component outside of the 'TabList' component. 'Tab' components have to be inside the 'TabList' component.", |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + test('should result with warning when defaultIndex and selectedIndex set', () => { |
| 93 | + render( |
| 94 | + <Tabs selectedIndex={1} defaultIndex={1} onSelect={() => {}}> |
| 95 | + <TabList> |
| 96 | + <Tab>Foo</Tab> |
| 97 | + </TabList> |
| 98 | + <TabPanel>Foo</TabPanel> |
| 99 | + </Tabs>, |
| 100 | + ); |
| 101 | + |
| 102 | + assertPropTypeWarning( |
| 103 | + 'The prop `selectedIndex` cannot be used together with `defaultIndex` in `Tabs`.\nEither remove `selectedIndex` to let `Tabs` handle the selected tab internally or remove `defaultIndex` to handle it yourself.', |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should throw when mode of component changes', () => { |
| 108 | + const onSelect = () => {}; |
| 109 | + const { rerender } = render( |
| 110 | + <Tabs defaultIndex={1} onSelect={onSelect}> |
| 111 | + <TabList> |
| 112 | + <Tab>Foo</Tab> |
| 113 | + <Tab>Foo2</Tab> |
| 114 | + </TabList> |
| 115 | + <TabPanel>Foo</TabPanel> |
| 116 | + <TabPanel>Foo2</TabPanel> |
| 117 | + </Tabs>, |
| 118 | + ); |
| 119 | + |
| 120 | + const consoleLogMock = jest.spyOn(console, 'log').mockImplementation(); |
| 121 | + try { |
| 122 | + rerender( |
| 123 | + <Tabs selectedIndex={0} onSelect={onSelect}> |
| 124 | + <TabList> |
| 125 | + <Tab>Foo</Tab> |
| 126 | + <Tab>Foo2</Tab> |
| 127 | + </TabList> |
| 128 | + <TabPanel>Foo</TabPanel> |
| 129 | + <TabPanel>Foo2</TabPanel> |
| 130 | + </Tabs>, |
| 131 | + ); |
| 132 | + } catch (e) { |
| 133 | + expect(e.message).toContain( |
| 134 | + 'Switching between controlled mode (by using `selectedIndex`) and uncontrolled mode is not supported in `Tabs`.', |
| 135 | + ); |
| 136 | + } finally { |
| 137 | + consoleLogMock.mockRestore(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + test('should result with warning when tabs/panels are imbalanced', () => { |
| 142 | + render( |
| 143 | + <Tabs> |
| 144 | + <TabList> |
| 145 | + <Tab>Foo</Tab> |
| 146 | + </TabList> |
| 147 | + </Tabs>, |
| 148 | + ); |
| 149 | + |
| 150 | + assertPropTypeWarning( |
| 151 | + "There should be an equal number of 'Tab' and 'TabPanel' in `Tabs`. Received 1 'Tab' and 0 'TabPanel'.", |
| 152 | + 1, |
| 153 | + ); |
| 154 | + assertPropTypeWarning( |
| 155 | + "There should be an equal number of 'Tab' and 'TabPanel' in `UncontrolledTabs`. Received 1 'Tab' and 0 'TabPanel'.", |
| 156 | + 2, |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + test('should result with warning when onSelect missing when selectedIndex set', () => { |
| 161 | + render( |
| 162 | + <Tabs selectedIndex={1}> |
| 163 | + <TabList> |
| 164 | + <Tab>Foo</Tab> |
| 165 | + </TabList> |
| 166 | + <TabPanel>Foo</TabPanel> |
| 167 | + </Tabs>, |
| 168 | + ); |
| 169 | + |
| 170 | + assertPropTypeWarning( |
| 171 | + 'The prop `onSelect` is marked as required in `Tabs`, but its value is `undefined` or `null`.\n`onSelect` is required when `selectedIndex` is also set. Not doing so will make the tabs not do anything, as `selectedIndex` indicates that you want to handle the selected tab yourself.\nIf you only want to set the inital tab replace `selectedIndex` with `defaultIndex`.', |
| 172 | + ); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments