Skip to content

Commit

Permalink
test: adds tests for the SearchSortWidget
Browse files Browse the repository at this point in the history
on the LibraryAuthoringPage.
  • Loading branch information
pomegranited committed Jul 12, 2024
1 parent e96194c commit 34c4b4e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,39 @@ describe('<LibraryAuthoringPage />', () => {
// 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(<RootWrapper />);

// 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', '');
});
});

0 comments on commit 34c4b4e

Please sign in to comment.