@@ -2,18 +2,22 @@ import * as React from 'react';
2
2
import { expect } from 'chai' ;
3
3
import { spy } from 'sinon' ;
4
4
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' ;
6
9
// TODO v6: replace with material-next/FilledInput
7
10
import InputBase from '@mui/material-next/InputBase' ;
8
11
import { CssVarsProvider , extendTheme } from '@mui/material-next/styles' ;
9
12
// TODO v6: replace with material-next/Select
10
13
import Select from '@mui/material/Select' ;
14
+ import { FormControlContextValue } from './FormControlContext' ;
11
15
import useFormControl from './useFormControl' ;
12
16
13
17
describe ( '<FormControl />' , ( ) => {
14
18
const { render } = createRenderer ( ) ;
15
19
16
- function TestComponent ( props ) {
20
+ function TestComponent ( props : { contextCallback : ( context ?: FormControlContextValue ) => void } ) {
17
21
const context = useFormControl ( ) ;
18
22
React . useEffect ( ( ) => {
19
23
props . contextCallback ( context ) ;
@@ -47,15 +51,19 @@ describe('<FormControl />', () => {
47
51
const root = container . firstChild ;
48
52
49
53
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
+ ) ;
51
57
} ) ;
52
58
53
59
it ( 'can have the margin normal class' , ( ) => {
54
60
const { container } = render ( < FormControl margin = "normal" /> ) ;
55
61
const root = container . firstChild ;
56
62
57
63
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
+ ) ;
59
67
} ) ;
60
68
61
69
it ( 'can have the margin dense class' , ( ) => {
@@ -106,7 +114,7 @@ describe('<FormControl />', () => {
106
114
expect ( readContext . args [ 0 ] [ 0 ] ) . to . have . property ( 'focused' , false ) ;
107
115
108
116
act ( ( ) => {
109
- container . querySelector ( 'input' ) . focus ( ) ;
117
+ container . querySelector ( 'input' ) ? .focus ( ) ;
110
118
} ) ;
111
119
expect ( readContext . lastCall . args [ 0 ] ) . to . have . property ( 'focused' , true ) ;
112
120
@@ -126,7 +134,7 @@ describe('<FormControl />', () => {
126
134
) ;
127
135
128
136
expect ( readContext . args [ 0 ] [ 0 ] ) . to . have . property ( 'focused' , true ) ;
129
- container . querySelector ( 'input' ) . blur ( ) ;
137
+ container . querySelector ( 'input' ) ? .blur ( ) ;
130
138
expect ( readContext . args [ 0 ] [ 0 ] ) . to . have . property ( 'focused' , true ) ;
131
139
} ) ;
132
140
@@ -216,12 +224,12 @@ describe('<FormControl />', () => {
216
224
expect ( readContext . args [ 0 ] [ 0 ] ) . to . have . property ( 'filled' , true ) ;
217
225
} ) ;
218
226
219
- it ( 'should be filled when a value is set through inputProps ' , ( ) => {
227
+ it ( 'should be filled when a value is set through slotProps ' , ( ) => {
220
228
const readContext = spy ( ) ;
221
229
render (
222
230
< FormControl >
223
231
{ /* TODO v6: use material-next/FilledInput */ }
224
- < InputBase inputProps = { { value : 'bar' } } />
232
+ < InputBase slotProps = { { input : { value : 'bar' } } } />
225
233
< TestComponent contextCallback = { readContext } />
226
234
</ FormControl > ,
227
235
) ;
@@ -287,7 +295,7 @@ describe('<FormControl />', () => {
287
295
< TestComponent contextCallback = { readContext } />
288
296
</ FormControl > ,
289
297
) ;
290
- expect ( readContext . args [ 0 ] [ 0 ] . adornedStart , true ) ;
298
+ expect ( readContext . args [ 0 ] [ 0 ] . adornedStart , ' true' ) ;
291
299
} ) ;
292
300
} ) ;
293
301
@@ -348,22 +356,29 @@ describe('<FormControl />', () => {
348
356
} ) ;
349
357
} ) ;
350
358
359
+ type FormControlledComponent = {
360
+ onFilled : ( ) => { } ;
361
+ onEmpty : ( ) => { } ;
362
+ onFocus : ( ) => { } ;
363
+ onBlur : ( ) => { } ;
364
+ } ;
365
+
351
366
describe ( 'callbacks' , ( ) => {
352
367
describe ( 'onFilled' , ( ) => {
353
368
it ( 'should set the filled state' , ( ) => {
354
- const formControlRef = React . createRef ( ) ;
369
+ const formControlRef = React . createRef < FormControlledComponent > ( ) ;
355
370
render ( < FormControlled ref = { formControlRef } /> ) ;
356
371
357
372
expect ( formControlRef . current ) . to . have . property ( 'filled' , false ) ;
358
373
359
374
act ( ( ) => {
360
- formControlRef . current . onFilled ( ) ;
375
+ formControlRef . current ? .onFilled ( ) ;
361
376
} ) ;
362
377
363
378
expect ( formControlRef . current ) . to . have . property ( 'filled' , true ) ;
364
379
365
380
act ( ( ) => {
366
- formControlRef . current . onFilled ( ) ;
381
+ formControlRef . current ? .onFilled ( ) ;
367
382
} ) ;
368
383
369
384
expect ( formControlRef . current ) . to . have . property ( 'filled' , true ) ;
@@ -372,23 +387,23 @@ describe('<FormControl />', () => {
372
387
373
388
describe ( 'onEmpty' , ( ) => {
374
389
it ( 'should clean the filled state' , ( ) => {
375
- const formControlRef = React . createRef ( ) ;
390
+ const formControlRef = React . createRef < FormControlledComponent > ( ) ;
376
391
render ( < FormControlled ref = { formControlRef } /> ) ;
377
392
378
393
act ( ( ) => {
379
- formControlRef . current . onFilled ( ) ;
394
+ formControlRef . current ? .onFilled ( ) ;
380
395
} ) ;
381
396
382
397
expect ( formControlRef . current ) . to . have . property ( 'filled' , true ) ;
383
398
384
399
act ( ( ) => {
385
- formControlRef . current . onEmpty ( ) ;
400
+ formControlRef . current ? .onEmpty ( ) ;
386
401
} ) ;
387
402
388
403
expect ( formControlRef . current ) . to . have . property ( 'filled' , false ) ;
389
404
390
405
act ( ( ) => {
391
- formControlRef . current . onEmpty ( ) ;
406
+ formControlRef . current ? .onEmpty ( ) ;
392
407
} ) ;
393
408
394
409
expect ( formControlRef . current ) . to . have . property ( 'filled' , false ) ;
@@ -397,18 +412,18 @@ describe('<FormControl />', () => {
397
412
398
413
describe ( 'handleFocus' , ( ) => {
399
414
it ( 'should set the focused state' , ( ) => {
400
- const formControlRef = React . createRef ( ) ;
415
+ const formControlRef = React . createRef < FormControlledComponent > ( ) ;
401
416
render ( < FormControlled ref = { formControlRef } /> ) ;
402
417
expect ( formControlRef . current ) . to . have . property ( 'focused' , false ) ;
403
418
404
419
act ( ( ) => {
405
- formControlRef . current . onFocus ( ) ;
420
+ formControlRef . current ? .onFocus ( ) ;
406
421
} ) ;
407
422
408
423
expect ( formControlRef . current ) . to . have . property ( 'focused' , true ) ;
409
424
410
425
act ( ( ) => {
411
- formControlRef . current . onFocus ( ) ;
426
+ formControlRef . current ? .onFocus ( ) ;
412
427
} ) ;
413
428
414
429
expect ( formControlRef . current ) . to . have . property ( 'focused' , true ) ;
@@ -417,24 +432,24 @@ describe('<FormControl />', () => {
417
432
418
433
describe ( 'handleBlur' , ( ) => {
419
434
it ( 'should clear the focused state' , ( ) => {
420
- const formControlRef = React . createRef ( ) ;
435
+ const formControlRef = React . createRef < FormControlledComponent > ( ) ;
421
436
render ( < FormControlled ref = { formControlRef } /> ) ;
422
437
expect ( formControlRef . current ) . to . have . property ( 'focused' , false ) ;
423
438
424
439
act ( ( ) => {
425
- formControlRef . current . onFocus ( ) ;
440
+ formControlRef . current ? .onFocus ( ) ;
426
441
} ) ;
427
442
428
443
expect ( formControlRef . current ) . to . have . property ( 'focused' , true ) ;
429
444
430
445
act ( ( ) => {
431
- formControlRef . current . onBlur ( ) ;
446
+ formControlRef . current ? .onBlur ( ) ;
432
447
} ) ;
433
448
434
449
expect ( formControlRef . current ) . to . have . property ( 'focused' , false ) ;
435
450
436
451
act ( ( ) => {
437
- formControlRef . current . onBlur ( ) ;
452
+ formControlRef . current ? .onBlur ( ) ;
438
453
} ) ;
439
454
440
455
expect ( formControlRef . current ) . to . have . property ( 'focused' , false ) ;
0 commit comments