Skip to content

Commit 1085409

Browse files
committed
chore(@clayui/autocomplete): LPD-55597 Add infinite scrolling tests
1 parent 73ed17c commit 1085409

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

packages/clay-autocomplete/src/__tests__/IncrementalInteractions.tsx

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import ClayAutocomplete from '..';
7-
import {cleanup, fireEvent, render} from '@testing-library/react';
7+
import {cleanup, fireEvent, render, waitFor} from '@testing-library/react';
88
import userEvent from '@testing-library/user-event';
99
import React from 'react';
1010

@@ -753,4 +753,118 @@ describe('Autocomplete incremental interactions', () => {
753753
expect(onClickMock).toHaveBeenCalled();
754754
});
755755
});
756+
757+
describe('Infinite scrolling interactions', () => {
758+
it('calls onLoadMore when last item is active using keyboard', async () => {
759+
const onLoadMoreMock = jest.fn();
760+
761+
const {getByRole} = render(
762+
<ClayAutocomplete onLoadMore={onLoadMoreMock}>
763+
{Array(20)
764+
.fill(0)
765+
.map((_, index) => (
766+
<ClayAutocomplete.Item
767+
key={index}
768+
textValue={`Item ${index + 1}`}
769+
>
770+
Item {index + 1}
771+
</ClayAutocomplete.Item>
772+
))}
773+
</ClayAutocomplete>
774+
);
775+
776+
const input = getByRole('combobox');
777+
778+
userEvent.click(input);
779+
780+
expect(onLoadMoreMock).not.toHaveBeenCalled();
781+
782+
userEvent.type(input, '{arrowup}');
783+
784+
await waitFor(() => {
785+
expect(onLoadMoreMock).toHaveBeenCalled();
786+
});
787+
});
788+
789+
it('announces the initial amount of items', async () => {
790+
const itemsCount = 5;
791+
792+
const {getAllByRole, getByRole} = render(
793+
<ClayAutocomplete>
794+
{Array(itemsCount)
795+
.fill(0)
796+
.map((_, index) => (
797+
<ClayAutocomplete.Item
798+
key={index}
799+
textValue={`Item ${index + 1}`}
800+
>
801+
Item {index + 1}
802+
</ClayAutocomplete.Item>
803+
))}
804+
</ClayAutocomplete>
805+
);
806+
807+
const combobox = getByRole('combobox');
808+
809+
userEvent.click(combobox);
810+
811+
userEvent.type(combobox, '{arrowdown}');
812+
813+
const [announcer] = getAllByRole('log');
814+
815+
await waitFor(() => {
816+
expect(announcer?.innerHTML).toContain(
817+
`${itemsCount} items loaded. Reach the last item to load more.`
818+
);
819+
});
820+
});
821+
822+
it('announces when more items are loaded', async () => {
823+
const initialCount = 10;
824+
const batchLoadCount = 20;
825+
const onLoadMoreMock = jest.fn().mockImplementation(() => {
826+
return new Promise<void>((resolve) => {
827+
setTimeout(() => {
828+
resolve();
829+
}, 500);
830+
});
831+
});
832+
833+
const {getAllByRole, getByRole} = render(
834+
<ClayAutocomplete
835+
batchLoadCount={batchLoadCount}
836+
onLoadMore={onLoadMoreMock}
837+
>
838+
{Array(initialCount)
839+
.fill(0)
840+
.map((_, index) => (
841+
<ClayAutocomplete.Item
842+
key={index}
843+
textValue={`Item ${index + 1}`}
844+
>
845+
Item {index + 1}
846+
</ClayAutocomplete.Item>
847+
))}
848+
</ClayAutocomplete>
849+
);
850+
851+
const input = getByRole('combobox');
852+
853+
userEvent.click(input);
854+
855+
userEvent.type(input, '{arrowup}');
856+
857+
const [announcer] = getAllByRole('log');
858+
859+
await waitFor(() => {
860+
expect(announcer?.innerHTML).toContain(
861+
`Loading ${batchLoadCount} more items.`
862+
);
863+
864+
expect(announcer?.innerHTML).toContain(
865+
`${initialCount} items loaded.`
866+
);
867+
});
868+
});
869+
});
756870
});

packages/clay-autocomplete/src/useInfiniteScrolling.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function useInfiniteScrolling({
109109
lastCountAnnounced.current = null;
110110

111111
lastPositionBeforeLoad.current = menuRef.current?.scrollTop ?? null;
112-
menuRef.current?.scrollTo({
112+
menuRef.current?.scrollTo?.({
113113
top: menuRef.current?.scrollHeight,
114114
});
115115
}
@@ -137,7 +137,7 @@ export function useInfiniteScrolling({
137137
isInitialLoadAnnouncementPending.current = false;
138138

139139
if (lastPositionBeforeLoad.current !== null) {
140-
menuRef.current?.scrollTo({
140+
menuRef.current?.scrollTo?.({
141141
top: lastPositionBeforeLoad.current,
142142
});
143143

@@ -196,7 +196,7 @@ export function useInfiniteScrolling({
196196

197197
const InfiniteScrollingTrigger = useCallback(
198198
() =>
199-
isInfiniteScrollingEnabled ? (
199+
isInfiniteScrollingEnabled && window.IntersectionObserver ? (
200200
<div aria-hidden="true" className="pt-2" ref={triggerRef}>
201201
{isLoading && (
202202
<LoadingIndicator className="mb-2" size="sm" />

0 commit comments

Comments
 (0)