|
1 | 1 | import { fireEvent, render } from '@testing-library/react';
|
2 | 2 | import KeyCode from 'rc-util/lib/KeyCode';
|
3 | 3 | import { resetWarned } from 'rc-util/lib/warning';
|
4 |
| -import React from 'react'; |
| 4 | +import React, { useState } from 'react'; |
5 | 5 | import Cascader from '../src';
|
6 | 6 | import { optionsForActiveMenuItems } from './demoOptions';
|
7 | 7 | import type { ReactWrapper } from './enzyme';
|
@@ -73,6 +73,49 @@ describe('Cascader.Search', () => {
|
73 | 73 | expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything());
|
74 | 74 | });
|
75 | 75 |
|
| 76 | + it('externally controlled search',() => { |
| 77 | + const onSearch = jest.fn(); |
| 78 | + const onChange = jest.fn(); |
| 79 | + |
| 80 | + function doExternalSearch(wrapper: ReactWrapper, search: string) { |
| 81 | + wrapper.find('input[data-test="external-search"]').simulate('change', { |
| 82 | + target: { |
| 83 | + value: search, |
| 84 | + }, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + const ExternallyControlledSearch = () => { |
| 90 | + const [searchValue,setSearchValue] = useState('') |
| 91 | + return ( |
| 92 | + <> |
| 93 | + <input data-test="external-search" value={searchValue} onChange={e => setSearchValue(e.target.value)} /> |
| 94 | + <Cascader options={options} searchValue={searchValue} onChange={onChange} onSearch={onSearch} open showSearch={{displayInInput:false}} />, |
| 95 | + </> |
| 96 | + ); |
| 97 | + } |
| 98 | + const wrapper = mount(<ExternallyControlledSearch/>) |
| 99 | + |
| 100 | + // Leaf |
| 101 | + doExternalSearch(wrapper, 'toy'); |
| 102 | + let itemList = wrapper.find('div.rc-cascader-menu-item-content'); |
| 103 | + expect(itemList).toHaveLength(2); |
| 104 | + expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish'); |
| 105 | + expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards'); |
| 106 | + |
| 107 | + // Parent |
| 108 | + doExternalSearch(wrapper, 'Label Little'); |
| 109 | + itemList = wrapper.find('div.rc-cascader-menu-item-content'); |
| 110 | + expect(itemList).toHaveLength(2); |
| 111 | + expect(itemList.at(0).text()).toEqual('Label Bamboo / Label Little / Toy Fish'); |
| 112 | + expect(itemList.at(1).text()).toEqual('Label Bamboo / Label Little / Toy Cards'); |
| 113 | + |
| 114 | + // Change |
| 115 | + wrapper.clickOption(0, 0); |
| 116 | + expect(onChange).toHaveBeenCalledWith(['bamboo', 'little', 'fish'], expect.anything()); |
| 117 | + }) |
| 118 | + |
76 | 119 | it('changeOnSelect', () => {
|
77 | 120 | const onChange = jest.fn();
|
78 | 121 | const wrapper = mount(
|
|
0 commit comments