|
| 1 | +import { mockContentLibrary, mockGetContentLibraryV2List } from '@src/library-authoring/data/api.mocks'; |
| 2 | +import { |
| 3 | + initializeMocks, render, screen, waitFor, |
| 4 | +} from '@src/testUtils'; |
| 5 | +import { userEvent } from '@testing-library/user-event'; |
| 6 | +import { LibraryDropdownFilter } from './LibraryDropdownFilter'; |
| 7 | + |
| 8 | +mockContentLibrary.applyMock(); |
| 9 | + |
| 10 | +const mockSetValue = jest.fn(); |
| 11 | +let mockValue: string[] = []; |
| 12 | +jest.mock('@src/library-authoring/common/context/LibraryContext', () => ({ |
| 13 | + useLibraryContext: () => ({ |
| 14 | + selectedLibraries: mockValue, |
| 15 | + setSelectedLibraries: mockSetValue, |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +const renderComponent = () => render(<LibraryDropdownFilter />); |
| 20 | + |
| 21 | +describe('LibraryDropdownFilter', () => { |
| 22 | + beforeEach(() => { |
| 23 | + initializeMocks(); |
| 24 | + mockValue = []; |
| 25 | + }); |
| 26 | + |
| 27 | + it('should render the loading status', async () => { |
| 28 | + const user = userEvent.setup(); |
| 29 | + mockGetContentLibraryV2List.applyMockLoading(); |
| 30 | + renderComponent(); |
| 31 | + |
| 32 | + const dropdownTrigger = await screen.findByRole('button', { name: 'All libraries' }); |
| 33 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 34 | + await user.click(dropdownTrigger); |
| 35 | + |
| 36 | + expect(await screen.findByText('Loading...')).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render the empty list', async () => { |
| 40 | + const user = userEvent.setup(); |
| 41 | + mockGetContentLibraryV2List.applyMockNoPaginationEmpty(); |
| 42 | + renderComponent(); |
| 43 | + |
| 44 | + const dropdownTrigger = await screen.findByRole('button', { name: 'All libraries' }); |
| 45 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 46 | + await user.click(dropdownTrigger); |
| 47 | + |
| 48 | + expect(await screen.findByText('No libraries found!')).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should render LibraryDropdownFilter with dropdown options', async () => { |
| 52 | + const user = userEvent.setup(); |
| 53 | + mockGetContentLibraryV2List.applyMockNoPagination(); |
| 54 | + renderComponent(); |
| 55 | + |
| 56 | + const dropdownTrigger = await screen.findByRole('button', { name: 'All libraries' }); |
| 57 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 58 | + await user.click(dropdownTrigger); |
| 59 | + |
| 60 | + const item = await screen.findByRole('checkbox', { name: 'Test Library 1' }); |
| 61 | + expect(item).toBeInTheDocument(); |
| 62 | + await user.click(item); |
| 63 | + const passedFunction = mockSetValue.mock.calls[0][0]; |
| 64 | + expect(passedFunction([])).toEqual(['lib:SampleTaxonomyOrg1:TL1']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('toggle selected value', async () => { |
| 68 | + const user = userEvent.setup(); |
| 69 | + mockGetContentLibraryV2List.applyMockNoPagination(); |
| 70 | + renderComponent(); |
| 71 | + |
| 72 | + const dropdownTrigger = await screen.findByRole('button', { name: 'All libraries' }); |
| 73 | + await user.click(dropdownTrigger); |
| 74 | + |
| 75 | + const item = await screen.findByRole('checkbox', { name: 'Test Library 1' }); |
| 76 | + await user.click(item); |
| 77 | + const passedFunction = mockSetValue.mock.calls[0][0]; |
| 78 | + // Should remove it from list if already selected, i.e., it means user unselected it. |
| 79 | + expect(passedFunction([ |
| 80 | + 'lib:SampleTaxonomyOrg1:TL1', |
| 81 | + 'lib:SampleTaxonomyOrg1:TL2', |
| 82 | + ])).toEqual(['lib:SampleTaxonomyOrg1:TL2']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should update label to library if one is selected', async () => { |
| 86 | + mockGetContentLibraryV2List.applyMockNoPagination(); |
| 87 | + mockValue = ['lib:SampleTaxonomyOrg1:TL1']; |
| 88 | + renderComponent(); |
| 89 | + |
| 90 | + const dropdownTrigger = await screen.findByRole('button', { name: 'Test Library 1' }); |
| 91 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should update label to n libraries if more than one is selected', async () => { |
| 95 | + mockGetContentLibraryV2List.applyMockNoPagination(); |
| 96 | + mockValue = ['lib:SampleTaxonomyOrg1:TL1', 'lib:SampleTaxonomyOrg1:AL1']; |
| 97 | + renderComponent(); |
| 98 | + |
| 99 | + const dropdownTrigger = await screen.findByRole('button', { name: '2 Libraries' }); |
| 100 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should filter list by search', async () => { |
| 104 | + const user = userEvent.setup(); |
| 105 | + const mockApi = mockGetContentLibraryV2List.applyMockNoPagination(); |
| 106 | + renderComponent(); |
| 107 | + |
| 108 | + const dropdownTrigger = await screen.findByRole('button', { name: 'All libraries' }); |
| 109 | + expect(dropdownTrigger).toBeInTheDocument(); |
| 110 | + await user.click(dropdownTrigger); |
| 111 | + |
| 112 | + const searchInput = await screen.findByPlaceholderText('Search Library Name'); |
| 113 | + await user.type(searchInput, 'Test Library'); |
| 114 | + |
| 115 | + await waitFor(() => expect(mockApi).toHaveBeenLastCalledWith({ |
| 116 | + pagination: false, |
| 117 | + search: 'Test Library', |
| 118 | + }), { timeout: 600 }); |
| 119 | + |
| 120 | + const clearBtn = await screen.findByRole('button', { name: 'clear search' }); |
| 121 | + await user.click(clearBtn); |
| 122 | + |
| 123 | + await waitFor(() => expect(mockApi).toHaveBeenLastCalledWith({ |
| 124 | + pagination: false, |
| 125 | + search: '', |
| 126 | + }), { timeout: 600 }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments