|
1 | 1 | import * as React from 'react';
|
2 | 2 | import { expect } from 'chai';
|
3 | 3 | import { spy } from 'sinon';
|
4 |
| -import { describeConformance, act, createRenderer } from 'test/utils'; |
| 4 | +import { describeConformance, act, createRenderer, fireEvent } from 'test/utils'; |
5 | 5 | import FormControl, { formControlClasses as classes } from '@mui/material-next/FormControl';
|
6 | 6 | // TODO: replace with material-next/OutlinedInput
|
7 | 7 | import InputBase from '@mui/material-next/InputBase';
|
@@ -137,6 +137,65 @@ describe('<FormControl />', () => {
|
137 | 137 | });
|
138 | 138 | });
|
139 | 139 |
|
| 140 | + describe('registering input', () => { |
| 141 | + it("should warn if more than one input is rendered regardless how it's nested", () => { |
| 142 | + expect(() => { |
| 143 | + render( |
| 144 | + <FormControl> |
| 145 | + <InputBase /> |
| 146 | + <div> |
| 147 | + {/* should work regardless how it's nested */} |
| 148 | + <InputBase /> |
| 149 | + </div> |
| 150 | + </FormControl>, |
| 151 | + ); |
| 152 | + }).toErrorDev([ |
| 153 | + 'MUI: There are multiple `InputBase` components inside a FormControl.\nThis creates visual inconsistencies, only use one `InputBase`.', |
| 154 | + // React 18 Strict Effects run mount effects twice |
| 155 | + React.version.startsWith('18') && |
| 156 | + 'MUI: There are multiple `InputBase` components inside a FormControl.\nThis creates visual inconsistencies, only use one `InputBase`.', |
| 157 | + ]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should not warn if only one input is rendered', () => { |
| 161 | + expect(() => { |
| 162 | + render( |
| 163 | + <FormControl> |
| 164 | + <InputBase /> |
| 165 | + </FormControl>, |
| 166 | + ); |
| 167 | + }).not.toErrorDev(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should not warn when toggling between inputs', () => { |
| 171 | + // this will ensure that deregistering was called during unmount |
| 172 | + function ToggleFormInputs() { |
| 173 | + const [flag, setFlag] = React.useState(true); |
| 174 | + |
| 175 | + return ( |
| 176 | + <FormControl> |
| 177 | + {flag ? ( |
| 178 | + <InputBase /> |
| 179 | + ) : ( |
| 180 | + // TODO: use material-next/Select |
| 181 | + <Select native> |
| 182 | + <option value="">empty</option> |
| 183 | + </Select> |
| 184 | + )} |
| 185 | + <button type="button" onClick={() => setFlag(!flag)}> |
| 186 | + toggle |
| 187 | + </button> |
| 188 | + </FormControl> |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + const { getByText } = render(<ToggleFormInputs />); |
| 193 | + expect(() => { |
| 194 | + fireEvent.click(getByText('toggle')); |
| 195 | + }).not.toErrorDev(); |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
140 | 199 | // TODO: needs Outlined|FilledInput + FormControl integrated
|
141 | 200 | // eslint-disable-next-line mocha/no-skipped-tests
|
142 | 201 | describe.skip('input', () => {
|
|
0 commit comments