From 34c4b4e18e2c4f739ee98b47997d423de6e1d1e0 Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Sat, 13 Jul 2024 00:00:55 +0930 Subject: [PATCH] test: adds tests for the SearchSortWidget on the LibraryAuthoringPage. --- .../LibraryAuthoringPage.test.tsx | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/library-authoring/LibraryAuthoringPage.test.tsx b/src/library-authoring/LibraryAuthoringPage.test.tsx index ccb1e87c42..3619b1f647 100644 --- a/src/library-authoring/LibraryAuthoringPage.test.tsx +++ b/src/library-authoring/LibraryAuthoringPage.test.tsx @@ -231,4 +231,39 @@ describe('', () => { // This step is necessary to avoid the url change leak to other tests fireEvent.click(getByRole('tab', { name: 'Home' })); }); + it('sort library components', async () => { + mockUseParams.mockReturnValue({ libraryId: libraryData.id }); + axiosMock.onGet(getContentLibraryApiUrl(libraryData.id)).reply(200, libraryData); + fetchMock.post(searchEndpoint, returnEmptyResult, { overwriteRoutes: true }); + + const { findByText, getByText, getByTitle } = render(); + + // Default sorts by relevance + expect(await findByText('Most Relevant')).toBeInTheDocument(); + + const testSortOption = (async (optionText, sortBy) => { + fireEvent.click(getByTitle('Sort search results')); + fireEvent.click(getByText(optionText)); + const bodyText = sortBy ? `"sort":["${sortBy}"]` : '"sort":[]'; + const searchText = sortBy ? `?sort=${encodeURIComponent(sortBy)}` : ''; + await waitFor(() => { + expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, { + body: expect.stringContaining(bodyText), + method: 'POST', + headers: expect.anything(), + }); + }); + expect(window.location.search).toEqual(searchText); + }); + + await testSortOption('Title, A-Z', 'display_name:asc'); + await testSortOption('Title, Z-A', 'display_name:desc'); + await testSortOption('Newest', 'created:desc'); + await testSortOption('Oldest', 'created:asc'); + await testSortOption('Recently Published', 'last_published:desc'); + await testSortOption('Recently Modified', 'modified:desc'); + + // Selecting the default sort option clears the url search param + await testSortOption('Most Relevant', ''); + }); });