|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import ClayAutocomplete from '..'; |
7 | | -import {cleanup, fireEvent, render} from '@testing-library/react'; |
| 7 | +import {cleanup, fireEvent, render, waitFor} from '@testing-library/react'; |
8 | 8 | import userEvent from '@testing-library/user-event'; |
9 | 9 | import React from 'react'; |
10 | 10 |
|
@@ -753,4 +753,118 @@ describe('Autocomplete incremental interactions', () => { |
753 | 753 | expect(onClickMock).toHaveBeenCalled(); |
754 | 754 | }); |
755 | 755 | }); |
| 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 | + }); |
756 | 870 | }); |
0 commit comments