Skip to content

Commit 7586fae

Browse files
committed
Convert test to TS
1 parent cf605c7 commit 7586fae

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

packages/mui-material-next/src/FormControl/FormControl.test.js renamed to packages/mui-material-next/src/FormControl/FormControl.test.tsx

+38-23
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import * as React from 'react';
22
import { expect } from 'chai';
33
import { spy } from 'sinon';
44
import { describeConformance, act, createRenderer, fireEvent } from 'test/utils';
5-
import FormControl, { formControlClasses as classes } from '@mui/material-next/FormControl';
5+
import FormControl, {
6+
formControlClasses as classes,
7+
FormControlClasses,
8+
} from '@mui/material-next/FormControl';
69
// TODO v6: replace with material-next/FilledInput
710
import InputBase from '@mui/material-next/InputBase';
811
import { CssVarsProvider, extendTheme } from '@mui/material-next/styles';
912
// TODO v6: replace with material-next/Select
1013
import Select from '@mui/material/Select';
14+
import { FormControlContextValue } from './FormControlContext';
1115
import useFormControl from './useFormControl';
1216

1317
describe('<FormControl />', () => {
1418
const { render } = createRenderer();
1519

16-
function TestComponent(props) {
20+
function TestComponent(props: { contextCallback: (context?: FormControlContextValue) => void }) {
1721
const context = useFormControl();
1822
React.useEffect(() => {
1923
props.contextCallback(context);
@@ -47,15 +51,19 @@ describe('<FormControl />', () => {
4751
const root = container.firstChild;
4852

4953
expect(root).not.to.have.class(classes.marginNormal);
50-
expect(root).not.to.have.class(classes.sizeSmall);
54+
expect(root).not.to.have.class(
55+
(classes as FormControlClasses & { sizeSmall: string }).sizeSmall,
56+
);
5157
});
5258

5359
it('can have the margin normal class', () => {
5460
const { container } = render(<FormControl margin="normal" />);
5561
const root = container.firstChild;
5662

5763
expect(root).to.have.class(classes.marginNormal);
58-
expect(root).not.to.have.class(classes.sizeSmall);
64+
expect(root).not.to.have.class(
65+
(classes as FormControlClasses & { sizeSmall: string }).sizeSmall,
66+
);
5967
});
6068

6169
it('can have the margin dense class', () => {
@@ -106,7 +114,7 @@ describe('<FormControl />', () => {
106114
expect(readContext.args[0][0]).to.have.property('focused', false);
107115

108116
act(() => {
109-
container.querySelector('input').focus();
117+
container.querySelector('input')?.focus();
110118
});
111119
expect(readContext.lastCall.args[0]).to.have.property('focused', true);
112120

@@ -126,7 +134,7 @@ describe('<FormControl />', () => {
126134
);
127135

128136
expect(readContext.args[0][0]).to.have.property('focused', true);
129-
container.querySelector('input').blur();
137+
container.querySelector('input')?.blur();
130138
expect(readContext.args[0][0]).to.have.property('focused', true);
131139
});
132140

@@ -216,12 +224,12 @@ describe('<FormControl />', () => {
216224
expect(readContext.args[0][0]).to.have.property('filled', true);
217225
});
218226

219-
it('should be filled when a value is set through inputProps', () => {
227+
it('should be filled when a value is set through slotProps', () => {
220228
const readContext = spy();
221229
render(
222230
<FormControl>
223231
{/* TODO v6: use material-next/FilledInput */}
224-
<InputBase inputProps={{ value: 'bar' }} />
232+
<InputBase slotProps={{ input: { value: 'bar' } }} />
225233
<TestComponent contextCallback={readContext} />
226234
</FormControl>,
227235
);
@@ -287,7 +295,7 @@ describe('<FormControl />', () => {
287295
<TestComponent contextCallback={readContext} />
288296
</FormControl>,
289297
);
290-
expect(readContext.args[0][0].adornedStart, true);
298+
expect(readContext.args[0][0].adornedStart, 'true');
291299
});
292300
});
293301

@@ -348,22 +356,29 @@ describe('<FormControl />', () => {
348356
});
349357
});
350358

359+
type FormControlledComponent = {
360+
onFilled: () => {};
361+
onEmpty: () => {};
362+
onFocus: () => {};
363+
onBlur: () => {};
364+
};
365+
351366
describe('callbacks', () => {
352367
describe('onFilled', () => {
353368
it('should set the filled state', () => {
354-
const formControlRef = React.createRef();
369+
const formControlRef = React.createRef<FormControlledComponent>();
355370
render(<FormControlled ref={formControlRef} />);
356371

357372
expect(formControlRef.current).to.have.property('filled', false);
358373

359374
act(() => {
360-
formControlRef.current.onFilled();
375+
formControlRef.current?.onFilled();
361376
});
362377

363378
expect(formControlRef.current).to.have.property('filled', true);
364379

365380
act(() => {
366-
formControlRef.current.onFilled();
381+
formControlRef.current?.onFilled();
367382
});
368383

369384
expect(formControlRef.current).to.have.property('filled', true);
@@ -372,23 +387,23 @@ describe('<FormControl />', () => {
372387

373388
describe('onEmpty', () => {
374389
it('should clean the filled state', () => {
375-
const formControlRef = React.createRef();
390+
const formControlRef = React.createRef<FormControlledComponent>();
376391
render(<FormControlled ref={formControlRef} />);
377392

378393
act(() => {
379-
formControlRef.current.onFilled();
394+
formControlRef.current?.onFilled();
380395
});
381396

382397
expect(formControlRef.current).to.have.property('filled', true);
383398

384399
act(() => {
385-
formControlRef.current.onEmpty();
400+
formControlRef.current?.onEmpty();
386401
});
387402

388403
expect(formControlRef.current).to.have.property('filled', false);
389404

390405
act(() => {
391-
formControlRef.current.onEmpty();
406+
formControlRef.current?.onEmpty();
392407
});
393408

394409
expect(formControlRef.current).to.have.property('filled', false);
@@ -397,18 +412,18 @@ describe('<FormControl />', () => {
397412

398413
describe('handleFocus', () => {
399414
it('should set the focused state', () => {
400-
const formControlRef = React.createRef();
415+
const formControlRef = React.createRef<FormControlledComponent>();
401416
render(<FormControlled ref={formControlRef} />);
402417
expect(formControlRef.current).to.have.property('focused', false);
403418

404419
act(() => {
405-
formControlRef.current.onFocus();
420+
formControlRef.current?.onFocus();
406421
});
407422

408423
expect(formControlRef.current).to.have.property('focused', true);
409424

410425
act(() => {
411-
formControlRef.current.onFocus();
426+
formControlRef.current?.onFocus();
412427
});
413428

414429
expect(formControlRef.current).to.have.property('focused', true);
@@ -417,24 +432,24 @@ describe('<FormControl />', () => {
417432

418433
describe('handleBlur', () => {
419434
it('should clear the focused state', () => {
420-
const formControlRef = React.createRef();
435+
const formControlRef = React.createRef<FormControlledComponent>();
421436
render(<FormControlled ref={formControlRef} />);
422437
expect(formControlRef.current).to.have.property('focused', false);
423438

424439
act(() => {
425-
formControlRef.current.onFocus();
440+
formControlRef.current?.onFocus();
426441
});
427442

428443
expect(formControlRef.current).to.have.property('focused', true);
429444

430445
act(() => {
431-
formControlRef.current.onBlur();
446+
formControlRef.current?.onBlur();
432447
});
433448

434449
expect(formControlRef.current).to.have.property('focused', false);
435450

436451
act(() => {
437-
formControlRef.current.onBlur();
452+
formControlRef.current?.onBlur();
438453
});
439454

440455
expect(formControlRef.current).to.have.property('focused', false);

0 commit comments

Comments
 (0)