1
1
import { mount , render } from 'enzyme' ;
2
2
import KeyCode from 'rc-util/lib/KeyCode' ;
3
- import React from 'react' ;
4
- import { act } from 'react-dom/test-utils' ;
5
- import { resetWarned } from 'rc-util/lib/warning' ;
6
- import type { ScrollConfig } from 'rc-virtual-list/lib/List' ;
7
3
import { spyElementPrototype } from 'rc-util/lib/test/domHook' ;
4
+ import { resetWarned } from 'rc-util/lib/warning' ;
8
5
import VirtualList from 'rc-virtual-list' ;
6
+ import type { ScrollConfig } from 'rc-virtual-list/lib/List' ;
7
+ import React from 'react' ;
8
+ import { act } from 'react-dom/test-utils' ;
9
9
import type { SelectProps } from '../src' ;
10
10
import Select , { OptGroup , Option , useBaseProps } from '../src' ;
11
- import focusTest from './shared/focusTest' ;
11
+ import type { BaseSelectRef } from '../src/BaseSelect' ;
12
+ import allowClearTest from './shared/allowClearTest' ;
12
13
import blurTest from './shared/blurTest' ;
13
- import keyDownTest from './shared/keyDownTest ' ;
14
+ import focusTest from './shared/focusTest ' ;
14
15
import inputFilterTest from './shared/inputFilterTest' ;
16
+ import keyDownTest from './shared/keyDownTest' ;
15
17
import openControlledTest from './shared/openControlledTest' ;
16
- import allowClearTest from './shared/allowClearTest' ;
17
18
import {
18
19
expectOpen ,
19
- toggleOpen ,
20
- selectItem ,
21
20
findSelection ,
22
21
injectRunAllTimers ,
22
+ selectItem ,
23
+ toggleOpen ,
23
24
} from './utils/common' ;
24
- import type { BaseSelectRef } from '../src/BaseSelect' ;
25
25
26
26
describe ( 'Select.Basic' , ( ) => {
27
27
injectRunAllTimers ( jest ) ;
@@ -602,7 +602,7 @@ describe('Select.Basic', () => {
602
602
</ Select > ,
603
603
) ;
604
604
605
- const inputSpy = jest . spyOn ( wrapper2 . find ( 'input' ) . instance ( ) , 'focus' ) ;
605
+ const inputSpy = jest . spyOn ( wrapper2 . find ( 'input' ) . instance ( ) , 'focus' as any ) ;
606
606
607
607
wrapper2 . find ( '.rc-select-selection-placeholder' ) . simulate ( 'mousedown' ) ;
608
608
wrapper2 . find ( '.rc-select-selection-placeholder' ) . simulate ( 'click' ) ;
@@ -807,9 +807,9 @@ describe('Select.Basic', () => {
807
807
}
808
808
}
809
809
const wrapper = mount ( < Controlled /> ) ;
810
- expect ( wrapper . state ( ) . open ) . toBe ( true ) ;
810
+ expect ( ( wrapper . state ( ) as any ) . open ) . toBe ( true ) ;
811
811
toggleOpen ( wrapper ) ;
812
- expect ( wrapper . state ( ) . open ) . toBe ( false ) ;
812
+ expect ( ( wrapper . state ( ) as any ) . open ) . toBe ( false ) ;
813
813
814
814
selectItem ( wrapper ) ;
815
815
expectOpen ( wrapper , false ) ;
@@ -822,7 +822,7 @@ describe('Select.Basic', () => {
822
822
</ Select > ,
823
823
) ;
824
824
825
- const focusSpy = jest . spyOn ( wrapper . find ( 'input' ) . instance ( ) , 'focus' ) ;
825
+ const focusSpy = jest . spyOn ( wrapper . find ( 'input' ) . instance ( ) , 'focus' as any ) ;
826
826
wrapper . find ( '.rc-select-selection-placeholder' ) . simulate ( 'mousedown' ) ;
827
827
wrapper . find ( '.rc-select-selection-placeholder' ) . simulate ( 'click' ) ;
828
828
expect ( focusSpy ) . toHaveBeenCalled ( ) ;
@@ -1646,28 +1646,90 @@ describe('Select.Basic', () => {
1646
1646
} ) ;
1647
1647
} ) ;
1648
1648
1649
- it ( '`null` is a value' , ( ) => {
1650
- const onChange = jest . fn ( ) ;
1649
+ describe ( '`null` is a value' , ( ) => {
1650
+ let errorSpy ;
1651
+ const warningMessage = 'Warning: `value` in Select options should not be `null`.' ;
1651
1652
1652
- const wrapper = mount (
1653
- < Select onChange = { onChange } >
1654
- < Option value = { 1 } > 1</ Option >
1655
- < Option value = { null } > No</ Option >
1656
- < Option value = { 0 } > 0</ Option >
1657
- < Option value = "" > Empty</ Option >
1658
- </ Select > ,
1659
- ) ;
1653
+ beforeAll ( ( ) => {
1654
+ errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => null ) ;
1655
+ } ) ;
1660
1656
1661
- [
1662
- [ 1 , '1' ] ,
1663
- [ null , 'No' ] ,
1664
- [ 0 , '0' ] ,
1665
- [ '' , 'Empty' ] ,
1666
- ] . forEach ( ( [ value , showValue ] , index ) => {
1667
- toggleOpen ( wrapper ) ;
1668
- selectItem ( wrapper , index ) ;
1669
- expect ( onChange ) . toHaveBeenCalledWith ( value , expect . anything ( ) ) ;
1670
- expect ( wrapper . find ( '.rc-select-selection-item' ) . text ( ) ) . toEqual ( showValue ) ;
1657
+ beforeEach ( ( ) => {
1658
+ errorSpy . mockReset ( ) ;
1659
+ resetWarned ( ) ;
1660
+ } ) ;
1661
+
1662
+ afterAll ( ( ) => {
1663
+ errorSpy . mockRestore ( ) ;
1664
+ } ) ;
1665
+
1666
+ it ( '`null` is a value and should throw a warning' , ( ) => {
1667
+ const onChange = jest . fn ( ) ;
1668
+
1669
+ const wrapper = mount (
1670
+ < Select onChange = { onChange } >
1671
+ < Option value = { 1 } > 1</ Option >
1672
+ < Option value = { null } > No</ Option >
1673
+ < Option value = { 0 } > 0</ Option >
1674
+ < Option value = "" > Empty</ Option >
1675
+ </ Select > ,
1676
+ ) ;
1677
+
1678
+ [
1679
+ [ 1 , '1' ] ,
1680
+ [ null , 'No' ] ,
1681
+ [ 0 , '0' ] ,
1682
+ [ '' , 'Empty' ] ,
1683
+ ] . forEach ( ( [ value , showValue ] , index ) => {
1684
+ toggleOpen ( wrapper ) ;
1685
+ selectItem ( wrapper , index ) ;
1686
+ expect ( onChange ) . toHaveBeenCalledWith ( value , expect . anything ( ) ) ;
1687
+ expect ( wrapper . find ( '.rc-select-selection-item' ) . text ( ) ) . toEqual ( showValue ) ;
1688
+ } ) ;
1689
+
1690
+ expect ( errorSpy ) . toHaveBeenCalledWith ( warningMessage ) ;
1691
+ } ) ;
1692
+
1693
+ it ( '`null` is a value in OptGroup should throw a warning' , ( ) => {
1694
+ mount (
1695
+ < Select >
1696
+ < OptGroup >
1697
+ < Option value = "1" > 1</ Option >
1698
+ < Option value = { null } > null</ Option >
1699
+ </ OptGroup >
1700
+ </ Select > ,
1701
+ ) ;
1702
+
1703
+ expect ( errorSpy ) . toHaveBeenCalledWith ( warningMessage ) ;
1704
+ } ) ;
1705
+
1706
+ it ( '`null` is a value in fieldNames should throw a warning' , ( ) => {
1707
+ mount (
1708
+ < Select
1709
+ fieldNames = { {
1710
+ label : 'fieldLabel' ,
1711
+ value : 'fieldValue' ,
1712
+ options : 'fieldOptions' ,
1713
+ } }
1714
+ options = { [
1715
+ {
1716
+ fieldLabel : 'label' ,
1717
+ fieldOptions : [
1718
+ {
1719
+ fieldLabel : '1' ,
1720
+ fieldValue : '1' ,
1721
+ } ,
1722
+ {
1723
+ fieldLabel : '2' ,
1724
+ fieldValue : null ,
1725
+ } ,
1726
+ ] ,
1727
+ } ,
1728
+ ] }
1729
+ /> ,
1730
+ ) ;
1731
+
1732
+ expect ( errorSpy ) . toHaveBeenCalledWith ( warningMessage ) ;
1671
1733
} ) ;
1672
1734
} ) ;
1673
1735
0 commit comments