MCPcopy Create free account
hub / github.com/react-component/select / allowClearTest

Function allowClearTest

tests/shared/allowClearTest.tsx:5–51  ·  view source on GitHub ↗
(mode: any, value: any)

Source from the content-addressed store, hash-verified

3import { fireEvent, render } from '@testing-library/react';
4
5export default function allowClearTest(mode: any, value: any) {
6 describe('allowClear', () => {
7 it('renders correctly', () => {
8 const { container } = render(<Select mode={mode} value={value} allowClear />);
9 expect(container.querySelector('.rc-select-clear-icon')).toBeTruthy();
10 });
11 it('clears value', () => {
12 const onClear = jest.fn();
13 const onChange = jest.fn();
14 const onDeselect = jest.fn();
15 const useArrayValue = ['tags', 'multiple'].includes(mode);
16
17 const renderDemo = (disabled?: boolean) => (
18 <Select
19 defaultValue={useArrayValue ? ['1'] : '1'}
20 allowClear
21 mode={mode}
22 onClear={onClear}
23 onChange={onChange}
24 onDeselect={onDeselect}
25 disabled={disabled}
26 >
27 <Option value="1">1</Option>
28 <Option value="2">2</Option>
29 </Select>
30 );
31
32 const { container, rerender } = render(renderDemo());
33
34 // disabled
35 rerender(renderDemo(true));
36 expect(container.querySelector('.rc-select-clear')).toBeFalsy();
37
38 // enabled
39 rerender(renderDemo(false));
40 fireEvent.mouseDown(container.querySelector('.rc-select-clear'));
41 if (useArrayValue) {
42 expect(onChange).toHaveBeenCalledWith([], []);
43 } else {
44 expect(onChange).toHaveBeenCalledWith(undefined, undefined);
45 }
46 expect(onDeselect).not.toBeCalled();
47 expect(container.querySelector('input').value).toEqual('');
48 expect(onClear).toHaveBeenCalled();
49 });
50 });
51}

Callers 4

Combobox.test.tsxFile · 0.85
Select.test.tsxFile · 0.85
Tags.test.tsxFile · 0.85
Multiple.test.tsxFile · 0.85

Calls 1

renderDemoFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…