2022-09-05 19:41:32 +08:00
import { CloseOutlined } from '@ant-design/icons' ;
2023-04-04 17:17:36 +08:00
import React from 'react' ;
2022-09-05 19:41:32 +08:00
import type { SelectProps } from '..' ;
2017-11-19 01:41:40 +08:00
import Select from '..' ;
import focusTest from '../../../tests/shared/focusTest' ;
2019-08-26 22:53:20 +08:00
import mountTest from '../../../tests/shared/mountTest' ;
2020-01-02 19:10:16 +08:00
import rtlTest from '../../../tests/shared/rtlTest' ;
2023-04-04 17:17:36 +08:00
import { act , fireEvent , render } from '../../../tests/utils' ;
2022-09-08 14:33:11 +08:00
import { resetWarned } from '../../_util/warning' ;
2017-11-19 01:41:40 +08:00
2018-08-25 23:41:30 +08:00
const { Option } = Select ;
2017-11-19 01:41:40 +08:00
describe ( 'Select' , ( ) = > {
2020-10-24 20:33:18 +08:00
focusTest ( Select , { refFocus : true } ) ;
2019-08-26 22:53:20 +08:00
mountTest ( Select ) ;
2020-01-02 19:10:16 +08:00
rtlTest ( Select ) ;
2018-01-04 20:43:56 +08:00
2022-09-05 19:41:32 +08:00
function toggleOpen ( container : ReturnType < typeof render > [ 'container' ] ) : void {
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ! ) ;
2019-09-12 20:15:17 +08:00
act ( ( ) = > {
2023-06-07 21:59:21 +08:00
jest . runAllTimers ( ) ;
2019-09-12 20:15:17 +08:00
} ) ;
}
2018-01-04 20:43:56 +08:00
beforeEach ( ( ) = > {
2023-06-07 21:59:21 +08:00
jest . useFakeTimers ( ) ;
2018-01-04 20:43:56 +08:00
} ) ;
afterEach ( ( ) = > {
2023-06-07 21:59:21 +08:00
jest . useRealTimers ( ) ;
2018-01-04 20:43:56 +08:00
} ) ;
it ( 'should have default notFoundContent' , ( ) = > {
2022-05-19 16:27:24 +08:00
const { container } = render ( < Select mode = "multiple" / > ) ;
toggleOpen ( container ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 0 ) ;
expect ( container . querySelectorAll ( '.ant-empty' ) . length ) . toBeTruthy ( ) ;
2018-01-04 20:43:56 +08:00
} ) ;
it ( 'should support set notFoundContent to null' , ( ) = > {
2022-05-19 16:27:24 +08:00
const { container } = render ( < Select mode = "multiple" notFoundContent = { null } / > ) ;
toggleOpen ( container ) ;
expect ( container . querySelectorAll ( '.ant-empty' ) . length ) . toBe ( 0 ) ;
2018-01-04 20:43:56 +08:00
} ) ;
it ( 'should not have default notFoundContent when mode is combobox' , ( ) = > {
2022-09-05 19:41:32 +08:00
const { container } = render (
< Select mode = { Select . SECRET_COMBOBOX_MODE_DO_NOT_USE as SelectProps [ 'mode' ] } / > ,
) ;
2022-05-19 16:27:24 +08:00
toggleOpen ( container ) ;
expect ( container . querySelector ( '.ant-empty' ) ) . toBeFalsy ( ) ;
2018-01-04 20:43:56 +08:00
} ) ;
it ( 'should not have notFoundContent when mode is combobox and notFoundContent is set' , ( ) = > {
2022-05-19 16:27:24 +08:00
const { container } = render (
2022-09-05 19:41:32 +08:00
< Select
mode = { Select . SECRET_COMBOBOX_MODE_DO_NOT_USE as SelectProps [ 'mode' ] }
notFoundContent = "not at all"
/ > ,
2018-01-04 20:43:56 +08:00
) ;
2022-05-19 16:27:24 +08:00
toggleOpen ( container ) ;
expect ( container . querySelector ( '.ant-select-item-option' ) ) . toBeFalsy ( ) ;
expect ( container . querySelector ( '.ant-select-item-empty' ) ) . toHaveTextContent ( 'not at all' ) ;
2018-01-04 20:43:56 +08:00
} ) ;
2018-08-25 23:41:30 +08:00
it ( 'should be controlled by open prop' , ( ) = > {
2023-06-07 21:59:21 +08:00
const onDropdownVisibleChange = jest . fn ( ) ;
2022-09-05 19:41:32 +08:00
const TestComponent : React.FC = ( ) = > {
2022-05-20 12:18:27 +08:00
const [ open , setOpen ] = React . useState ( false ) ;
2022-11-19 13:47:33 +08:00
const handleChange : SelectProps [ 'onDropdownVisibleChange' ] = ( value ) = > {
2022-05-20 12:18:27 +08:00
onDropdownVisibleChange ( value ) ;
setOpen ( value ) ;
} ;
return (
< Select open = { open } onDropdownVisibleChange = { handleChange } >
< Option value = "1" > 1 < / Option >
< / Select >
) ;
} ;
const { container } = render ( < TestComponent / > ) ;
expect ( container . querySelector ( '.ant-select-dropdown' ) ) . toBeFalsy ( ) ;
2022-05-19 16:27:24 +08:00
toggleOpen ( container ) ;
expect ( container . querySelectorAll ( '.ant-select-dropdown' ) . length ) . toBe ( 1 ) ;
2018-08-25 23:41:30 +08:00
expect ( onDropdownVisibleChange ) . toHaveBeenLastCalledWith ( true ) ;
} ) ;
2018-11-12 12:18:21 +08:00
2020-06-11 18:01:45 +08:00
it ( 'should show search icon when showSearch and open' , ( ) = > {
2023-06-07 21:59:21 +08:00
jest . useFakeTimers ( ) ;
2022-05-19 16:27:24 +08:00
const { container } = render (
2020-06-11 18:01:45 +08:00
< Select showSearch >
< Option value = "1" > 1 < / Option >
< / Select > ,
) ;
2022-05-19 16:27:24 +08:00
expect ( container . querySelectorAll ( '.anticon-down' ) . length ) . toBe ( 1 ) ;
expect ( container . querySelectorAll ( '.anticon-search' ) . length ) . toBe ( 0 ) ;
toggleOpen ( container ) ;
expect ( container . querySelectorAll ( '.anticon-down' ) . length ) . toBe ( 0 ) ;
expect ( container . querySelectorAll ( '.anticon-search' ) . length ) . toBe ( 1 ) ;
2020-06-11 18:01:45 +08:00
} ) ;
2022-08-05 11:21:07 +08:00
2018-11-12 12:18:21 +08:00
describe ( 'Select Custom Icons' , ( ) = > {
it ( 'should support customized icons' , ( ) = > {
2022-05-19 16:27:24 +08:00
const { rerender , asFragment } = render (
< Select
2022-09-05 19:41:32 +08:00
removeIcon = { < CloseOutlined / > }
clearIcon = { < CloseOutlined / > }
menuItemSelectedIcon = { < CloseOutlined / > }
2022-05-19 16:27:24 +08:00
>
< Option value = "1" > 1 < / Option >
< / Select > ,
) ;
rerender (
2018-11-12 12:18:21 +08:00
< Select
2022-09-05 19:41:32 +08:00
removeIcon = { < CloseOutlined / > }
clearIcon = { < CloseOutlined / > }
menuItemSelectedIcon = { < CloseOutlined / > }
2018-11-12 12:18:21 +08:00
>
< Option value = "1" > 1 < / Option >
2018-12-07 20:02:01 +08:00
< / Select > ,
2018-11-12 12:18:21 +08:00
) ;
2022-08-08 13:31:55 +08:00
act ( ( ) = > {
2023-06-07 21:59:21 +08:00
jest . runAllTimers ( ) ;
2022-08-08 13:31:55 +08:00
} ) ;
2022-05-19 16:27:24 +08:00
expect ( asFragment ( ) . firstChild ) . toMatchSnapshot ( ) ;
2018-11-12 12:18:21 +08:00
} ) ;
} ) ;
2020-06-11 18:01:45 +08:00
describe ( 'Deprecated' , ( ) = > {
it ( 'should ignore mode="combobox"' , ( ) = > {
2022-05-19 16:27:24 +08:00
const { asFragment } = render (
2022-09-05 19:41:32 +08:00
< Select mode = { 'combobox' as SelectProps [ 'mode' ] } >
2020-06-11 18:01:45 +08:00
< Option value = "1" > 1 < / Option >
< / Select > ,
) ;
2022-05-19 16:27:24 +08:00
expect ( asFragment ( ) . firstChild ) . toMatchSnapshot ( ) ;
2020-06-11 18:01:45 +08:00
} ) ;
2022-09-08 14:33:11 +08:00
it ( 'dropdownClassName' , ( ) = > {
resetWarned ( ) ;
2023-06-07 21:59:21 +08:00
const errSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) = > { } ) ;
2022-09-08 14:33:11 +08:00
const { container } = render ( < Select dropdownClassName = "legacy" open / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Select] `dropdownClassName` is deprecated. Please use `popupClassName` instead.' ,
) ;
expect ( container . querySelector ( '.legacy' ) ) . toBeTruthy ( ) ;
errSpy . mockRestore ( ) ;
} ) ;
2023-04-04 17:17:36 +08:00
it ( 'warning for legacy dropdownMatchSelectWidth' , ( ) = > {
resetWarned ( ) ;
2023-06-07 21:59:21 +08:00
const errSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) = > { } ) ;
2023-04-04 17:17:36 +08:00
render ( < Select dropdownMatchSelectWidth open / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Select] `dropdownMatchSelectWidth` is deprecated. Please use `popupMatchSelectWidth` instead.' ,
) ;
errSpy . mockRestore ( ) ;
} ) ;
2023-07-19 20:27:09 +08:00
it ( 'deprecate showArrow' , ( ) = > {
resetWarned ( ) ;
const errSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) = > { } ) ;
const { container } = render ( < Select showArrow / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Select] `showArrow` is deprecated which will be removed in next major version. It will be a default behavior, you can hide it by setting `suffixIcon` to null.' ,
) ;
expect ( container . querySelector ( '.ant-select-show-arrow' ) ) . toBeTruthy ( ) ;
errSpy . mockRestore ( ) ;
} ) ;
2023-12-11 14:55:58 +08:00
it ( 'deprecate bordered' , ( ) = > {
resetWarned ( ) ;
const errSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) = > { } ) ;
const { container } = render ( < Select bordered = { false } / > ) ;
expect ( errSpy ) . toHaveBeenCalledWith (
expect . stringContaining ( 'Warning: [antd: Select] `bordered` is deprecated' ) ,
) ;
expect ( container . querySelector ( '.ant-select-borderless' ) ) . toBeTruthy ( ) ;
errSpy . mockRestore ( ) ;
} ) ;
2020-06-11 18:01:45 +08:00
} ) ;
2017-11-19 01:41:40 +08:00
} ) ;