Skip to content

Commit 9f851c3

Browse files
author
刘欢
committed
test: add test to “combine showSearch”
1 parent 378f957 commit 9f851c3

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

tests/Select.test.tsx

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LabelInValueType } from '@/Select';
1+
import type { DefaultOptionType, LabelInValueType, SearchConfig } from '@/Select';
22
import {
33
createEvent,
44
fireEvent,
@@ -2512,4 +2512,159 @@ describe('Select.Basic', () => {
25122512
expect(input).toHaveClass(customClassNames.input);
25132513
expect(input).toHaveStyle(customStyle.input);
25142514
});
2515+
// searchValue?: string;
2516+
// autoClearSearchValue?: boolean;
2517+
// onSearch?: (value: string) => void;
2518+
// tokenSeparators?: string[];
2519+
// filterOption?: boolean | FilterFunc<OptionType>;
2520+
// filterSort?: (optionA: OptionType, optionB: OptionType, info: { searchValue: string }) => number;
2521+
// optionFilterProp?: string;
2522+
// optionLabelProp?: string;
2523+
// const props = {
2524+
// searchValue: '1',
2525+
// autoClearSearchValue: false,
2526+
// filterSort: (optionA, optionB) => {
2527+
// return Number(optionB.value) - Number(optionA.value);
2528+
// },
2529+
// optionFilterProp: 'label',
2530+
// optionLabelProp: 'value',
2531+
// };
2532+
describe('combine showSearch', () => {
2533+
let errorSpy;
2534+
2535+
beforeAll(() => {
2536+
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => null);
2537+
});
2538+
2539+
beforeEach(() => {
2540+
errorSpy.mockReset();
2541+
resetWarned();
2542+
});
2543+
2544+
afterAll(() => {
2545+
errorSpy.mockRestore();
2546+
});
2547+
const currentSearchFn = jest.fn();
2548+
const legacySearchFn = jest.fn();
2549+
2550+
const LegacyDemo = (props) => {
2551+
return (
2552+
<Select
2553+
open
2554+
{...props}
2555+
options={[
2556+
{ value: 'a', label: '1' },
2557+
{ value: 'b', label: '2' },
2558+
{ value: 'c', label: '12' },
2559+
]}
2560+
/>
2561+
);
2562+
};
2563+
const CurrentDemo = (props) => {
2564+
return (
2565+
<Select
2566+
open
2567+
showSearch={props}
2568+
options={[
2569+
{ value: 'a', label: '1' },
2570+
{ value: 'b', label: '2' },
2571+
{ value: 'c', label: '12' },
2572+
]}
2573+
/>
2574+
);
2575+
};
2576+
it('onSearch', () => {
2577+
const { container } = render(
2578+
<>
2579+
<div id="test1">
2580+
<LegacyDemo onSearch={legacySearchFn} />
2581+
</div>
2582+
<div id="test2">
2583+
<CurrentDemo onSearch={currentSearchFn} />
2584+
</div>
2585+
</>,
2586+
);
2587+
const legacyInput = container.querySelector<HTMLInputElement>('#test1 input');
2588+
const currentInput = container.querySelector<HTMLInputElement>('#test2 input');
2589+
fireEvent.change(legacyInput, { target: { value: '2' } });
2590+
fireEvent.change(currentInput, { target: { value: '2' } });
2591+
expect(currentSearchFn).toHaveBeenCalledWith('2');
2592+
expect(legacySearchFn).toHaveBeenCalledWith('2');
2593+
});
2594+
it('searchValue', () => {
2595+
const { container } = render(
2596+
<>
2597+
<div id="test1">
2598+
<LegacyDemo searchValue="1" showSearch />
2599+
</div>
2600+
<div id="test2">
2601+
<CurrentDemo searchValue="1" />
2602+
</div>
2603+
</>,
2604+
);
2605+
const legacyInput = container.querySelector<HTMLInputElement>('#test1 input');
2606+
const currentInput = container.querySelector<HTMLInputElement>('#test2 input');
2607+
expect(legacyInput).toHaveValue('1');
2608+
expect(currentInput).toHaveValue('1');
2609+
expect(legacyInput.value).toBe(currentInput.value);
2610+
});
2611+
it('option:sort,FilterProp,LabelProp ', () => {
2612+
const { container } = render(
2613+
<>
2614+
<div id="test1">
2615+
<LegacyDemo
2616+
searchValue="2"
2617+
showSearch
2618+
filterSort={(a, b) => {
2619+
return Number(b.label) - Number(a.label);
2620+
}}
2621+
optionFilterProp="label"
2622+
optionLabelProp="value"
2623+
/>
2624+
</div>
2625+
<div id="test2">
2626+
<CurrentDemo
2627+
searchValue="2"
2628+
filterSort={(a, b) => {
2629+
return Number(b.label) - Number(a.label);
2630+
}}
2631+
optionFilterProp="label"
2632+
optionLabelProp="value"
2633+
/>
2634+
</div>
2635+
</>,
2636+
);
2637+
const items = container.querySelectorAll<HTMLDivElement>('.rc-select-item-option');
2638+
expect(items.length).toBe(4);
2639+
expect(items[0].title).toBe('12');
2640+
expect(items[2].title).toBe('12');
2641+
});
2642+
it('autoClearSearchValue,tokenSeparators', () => {
2643+
const { container } = render(
2644+
<>
2645+
<div id="test1">
2646+
<LegacyDemo showSearch autoClearSearchValue={false} tokenSeparators={[',', '*']} />
2647+
</div>
2648+
<div id="test2">
2649+
<CurrentDemo autoClearSearchValue={false} tokenSeparators={[',', '*']} />
2650+
</div>
2651+
</>,
2652+
);
2653+
const legacyInput = container.querySelector<HTMLInputElement>('#test1 input');
2654+
const currentInput = container.querySelector<HTMLInputElement>('#test2 input');
2655+
fireEvent.change(legacyInput, { target: { value: 'a' } });
2656+
fireEvent.change(currentInput, { target: { value: 'a' } });
2657+
expect(legacyInput).toHaveValue('a');
2658+
expect(currentInput).toHaveValue('a');
2659+
const items = container.querySelectorAll<HTMLDivElement>('.rc-select-item-option');
2660+
fireEvent.click(items[0]);
2661+
fireEvent.click(items[1]);
2662+
expect(legacyInput).toHaveValue('a');
2663+
expect(currentInput).toHaveValue('a');
2664+
fireEvent.change(legacyInput, { target: { value: '1,' } });
2665+
fireEvent.change(currentInput, { target: { value: '1,' } });
2666+
expect(legacyInput).toHaveValue('');
2667+
expect(currentInput).toHaveValue('');
2668+
});
2669+
});
25152670
});

0 commit comments

Comments
 (0)