Skip to content

Commit 58dd266

Browse files
committed
Add back registering input tests
1 parent a4d4fc1 commit 58dd266

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

packages/mui-material-next/src/FormControl/FormControl.test.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { expect } from 'chai';
33
import { spy } from 'sinon';
4-
import { describeConformance, act, createRenderer } from 'test/utils';
4+
import { describeConformance, act, createRenderer, fireEvent } from 'test/utils';
55
import FormControl, { formControlClasses as classes } from '@mui/material-next/FormControl';
66
// TODO: replace with material-next/OutlinedInput
77
import InputBase from '@mui/material-next/InputBase';
@@ -137,6 +137,65 @@ describe('<FormControl />', () => {
137137
});
138138
});
139139

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+
140199
// TODO: needs Outlined|FilledInput + FormControl integrated
141200
// eslint-disable-next-line mocha/no-skipped-tests
142201
describe.skip('input', () => {

packages/mui-material-next/src/InputBase/InputBase.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ const InputBase = React.forwardRef(function InputBase<
509509
{endAdornment}
510510
{renderSuffix
511511
? renderSuffix({
512-
// TODO: make sure ['color', 'disabled', 'error', 'hiddenLabel', 'size', 'required', 'filled'] are all passed to renderSuffix
512+
// TODO: make sure ['color', 'disabled', 'error', 'hiddenLabel', 'size', 'required', 'filled'] are all passed to renderSuffix when integrating OutlinedInput
513513
...muiFormControl,
514514
startAdornment,
515515
})

0 commit comments

Comments
 (0)