Skip to content

Commit 65b5ce5

Browse files
committed
fix: fixed codecov
1 parent 2c50a81 commit 65b5ce5

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

tests/OptionList.test.tsx

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,179 @@ describe('OptionList', () => {
510510
expect(onSelect).toHaveBeenCalledTimes(1);
511511
});
512512

513+
describe('filterSort scroll behavior', () => {
514+
beforeEach(() => {
515+
jest.useFakeTimers();
516+
});
517+
518+
afterEach(() => {
519+
jest.useRealTimers();
520+
});
521+
522+
it('should scroll to index 0 when filterSort is provided and searchValue exists', () => {
523+
const onActiveValue = jest.fn();
524+
const listRef = React.createRef<RefOptionListProps>();
525+
const filterSort = jest.fn((a, b) => 0); // Mock filterSort function
526+
527+
render(
528+
generateList({
529+
options: [
530+
{ value: '1', label: 'Option 1' },
531+
{ value: '2', label: 'Option 2' },
532+
{ value: '3', label: 'Option 3' },
533+
],
534+
values: new Set(['2']), // Pre-select option '2'
535+
searchValue: 'Option', // Has search value
536+
filterSort, // Has filterSort
537+
open: true,
538+
multiple: false, // Single mode
539+
onActiveValue,
540+
ref: listRef,
541+
}),
542+
);
543+
544+
// Verify that when filterSort and searchValue are both present,
545+
// the index should be 0 (first option)
546+
expect(onActiveValue).toHaveBeenCalledWith('1', 0, expect.anything());
547+
});
548+
549+
it('should scroll to matching option when filterSort is provided but no searchValue', () => {
550+
const onActiveValue = jest.fn();
551+
const listRef = React.createRef<RefOptionListProps>();
552+
const filterSort = jest.fn((a, b) => 0); // Mock filterSort function
553+
554+
render(
555+
generateList({
556+
options: [
557+
{ value: '1', label: 'Option 1' },
558+
{ value: '2', label: 'Option 2' },
559+
{ value: '3', label: 'Option 3' },
560+
],
561+
values: new Set(['2']), // Pre-select option '2'
562+
searchValue: '', // No search value
563+
filterSort, // Has filterSort
564+
open: true,
565+
multiple: false, // Single mode
566+
onActiveValue,
567+
ref: listRef,
568+
}),
569+
);
570+
571+
// Verify that when filterSort is provided but no searchValue,
572+
// it should find the index of the selected value (option '2' at index 1)
573+
expect(onActiveValue).toHaveBeenCalledWith('2', 1, expect.anything());
574+
});
575+
576+
it('should scroll to matching option when no filterSort but searchValue exists', () => {
577+
const onActiveValue = jest.fn();
578+
const listRef = React.createRef<RefOptionListProps>();
579+
580+
render(
581+
generateList({
582+
options: [
583+
{ value: '1', label: 'Option 1' },
584+
{ value: '2', label: 'Option 2' },
585+
{ value: '3', label: 'Option 3' },
586+
],
587+
values: new Set(['2']), // Pre-select option '2'
588+
searchValue: '2', // Search value that matches option '2'
589+
filterSort: undefined, // No filterSort
590+
open: true,
591+
multiple: false, // Single mode
592+
onActiveValue,
593+
ref: listRef,
594+
}),
595+
);
596+
597+
// Verify that when no filterSort but searchValue exists,
598+
// it should find the index of the option that starts with searchValue
599+
expect(onActiveValue).toHaveBeenCalledWith('2', 1, expect.anything());
600+
});
601+
602+
it('should scroll to selected value when no filterSort and no searchValue', () => {
603+
const onActiveValue = jest.fn();
604+
const listRef = React.createRef<RefOptionListProps>();
605+
606+
render(
607+
generateList({
608+
options: [
609+
{ value: '1', label: 'Option 1' },
610+
{ value: '2', label: 'Option 2' },
611+
{ value: '3', label: 'Option 3' },
612+
],
613+
values: new Set(['2']), // Pre-select option '2'
614+
searchValue: '', // No search value
615+
filterSort: undefined, // No filterSort
616+
open: true,
617+
multiple: false, // Single mode
618+
onActiveValue,
619+
ref: listRef,
620+
}),
621+
);
622+
623+
// Verify that when no filterSort and no searchValue,
624+
// it should find the index of the selected value (option '2' at index 1)
625+
expect(onActiveValue).toHaveBeenCalledWith('2', 1, expect.anything());
626+
});
627+
628+
it('should call scrollIntoView with correct index when filterSort is used', () => {
629+
const onActiveValue = jest.fn();
630+
const listRef = React.createRef<RefOptionListProps>();
631+
const filterSort = jest.fn((a, b) => 0); // Mock filterSort function
632+
633+
render(
634+
generateList({
635+
options: [
636+
{ value: '1', label: 'Option 1' },
637+
{ value: '2', label: 'Option 2' },
638+
{ value: '3', label: 'Option 3' },
639+
],
640+
values: new Set(['2']), // Pre-select option '2'
641+
searchValue: 'test', // Has search value
642+
filterSort, // Has filterSort
643+
open: true,
644+
multiple: false, // Single mode
645+
onActiveValue,
646+
ref: listRef,
647+
}),
648+
);
649+
650+
// Fast-forward timers to trigger the setTimeout for scrollIntoView
651+
act(() => {
652+
jest.runAllTimers();
653+
});
654+
655+
// Verify that scrollIntoView was called with index 0 when filterSort and searchValue are present
656+
expect(global.scrollToArgs).toEqual({ index: 0 });
657+
});
658+
659+
it('should not scroll when selected value is not found in options', () => {
660+
const onActiveValue = jest.fn();
661+
const listRef = React.createRef<RefOptionListProps>();
662+
663+
render(
664+
generateList({
665+
options: [
666+
{ value: '1', label: 'Option 1' },
667+
{ value: '2', label: 'Option 2' },
668+
{ value: '3', label: 'Option 3' },
669+
],
670+
values: new Set(['4']), // Pre-select option '4' which doesn't exist
671+
searchValue: '',
672+
filterSort: undefined,
673+
open: true,
674+
multiple: false, // Single mode
675+
onActiveValue,
676+
ref: listRef,
677+
}),
678+
);
679+
680+
// When the selected value is not found, the component will still activate the first available option
681+
// This is the expected behavior based on the current implementation
682+
expect(onActiveValue).toHaveBeenCalledWith('1', 0, expect.anything());
683+
});
684+
});
685+
513686
describe('List.ScrollBar', () => {
514687
let mockElement;
515688
let boundingRect = {

0 commit comments

Comments
 (0)