Skip to content

Commit a76b137

Browse files
author
刘欢
committed
test: add test to “combine showSearch”
1 parent 656e611 commit a76b137

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

0 commit comments

Comments
 (0)