|
| 1 | +import type { OptionListProps, RefOptionListProps } from '@/OptionList'; |
| 2 | +import { fireEvent, render } from '@testing-library/react'; |
| 3 | +import { forwardRef } from 'react'; |
| 4 | +import BaseSelect from '../src/BaseSelect'; |
| 5 | +import Select, { Option } from '../src'; |
| 6 | + |
| 7 | +const OptionList = forwardRef<RefOptionListProps, OptionListProps>(() => ( |
| 8 | + <div className="popup">Popup</div> |
| 9 | +)); |
| 10 | + |
| 11 | +describe('Select.Blur', () => { |
| 12 | + it('mode with undefined, onBlur source is blur', () => { |
| 13 | + const onSearch = jest.fn(); |
| 14 | + const { container } = render( |
| 15 | + <BaseSelect |
| 16 | + prefixCls="rc-select" |
| 17 | + id="test" |
| 18 | + displayValues={[]} |
| 19 | + onDisplayValuesChange={() => {}} |
| 20 | + searchValue="1" |
| 21 | + showSearch |
| 22 | + onSearch={onSearch} |
| 23 | + OptionList={OptionList} |
| 24 | + emptyOptions |
| 25 | + />, |
| 26 | + ); |
| 27 | + expect(container.querySelector('div.rc-select')).toBeTruthy(); |
| 28 | + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); |
| 29 | + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); |
| 30 | + fireEvent.blur(container.querySelector('div.rc-select')); |
| 31 | + expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('mode with multiple, onBlur source is blur', () => { |
| 35 | + const onSearch = jest.fn(); |
| 36 | + const { container } = render( |
| 37 | + <BaseSelect |
| 38 | + prefixCls="rc-select" |
| 39 | + mode="multiple" |
| 40 | + id="test" |
| 41 | + displayValues={[]} |
| 42 | + onDisplayValuesChange={() => {}} |
| 43 | + searchValue="1" |
| 44 | + showSearch |
| 45 | + onSearch={onSearch} |
| 46 | + OptionList={OptionList} |
| 47 | + emptyOptions |
| 48 | + />, |
| 49 | + ); |
| 50 | + expect(container.querySelector('div.rc-select')).toBeTruthy(); |
| 51 | + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); |
| 52 | + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); |
| 53 | + fireEvent.blur(container.querySelector('div.rc-select')); |
| 54 | + expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('mode with multiple, onBlur source is blur', () => { |
| 58 | + const onSearch = jest.fn(); |
| 59 | + const { container } = render( |
| 60 | + <BaseSelect |
| 61 | + prefixCls="rc-select" |
| 62 | + mode="multiple" |
| 63 | + id="test" |
| 64 | + displayValues={[]} |
| 65 | + onDisplayValuesChange={() => {}} |
| 66 | + searchValue="1" |
| 67 | + showSearch |
| 68 | + onSearch={onSearch} |
| 69 | + OptionList={OptionList} |
| 70 | + emptyOptions |
| 71 | + />, |
| 72 | + ); |
| 73 | + expect(container.querySelector('div.rc-select')).toBeTruthy(); |
| 74 | + fireEvent.change(container.querySelector('input'), { target: { value: '2' } }); |
| 75 | + expect(onSearch).toHaveBeenCalledWith('2', { source: 'typing' }); |
| 76 | + fireEvent.blur(container.querySelector('div.rc-select')); |
| 77 | + expect(onSearch).toHaveBeenCalledWith('', { source: 'blur' }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('click item and blur should trigger onBlur but not trigger onSearch', () => { |
| 81 | + const onSearch = jest.fn(); |
| 82 | + const onBlur = jest.fn(); |
| 83 | + |
| 84 | + const Demo = () => ( |
| 85 | + <Select onSearch={onSearch} showSearch onBlur={onBlur}> |
| 86 | + <Option value="11">11</Option> |
| 87 | + <Option value="22">22</Option> |
| 88 | + <Option value="33">33</Option> |
| 89 | + </Select> |
| 90 | + ); |
| 91 | + |
| 92 | + const { container } = render(<Demo />); |
| 93 | + const input = container.querySelector('input'); |
| 94 | + fireEvent.change(input, { target: { value: '1' } }); |
| 95 | + fireEvent.click( |
| 96 | + container.querySelectorAll('.rc-select-dropdown .rc-select-item-option-content')[0], |
| 97 | + ); |
| 98 | + fireEvent.blur(input); |
| 99 | + expect(container.querySelector('.rc-select-dropdown-hidden')).toBeTruthy(); |
| 100 | + expect(onSearch).toHaveBeenCalledTimes(1); |
| 101 | + expect(onBlur).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | +}); |
0 commit comments