From b0dd84f72bf78f2fb79e14176f9e31ac9a742cee Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Thu, 17 Oct 2024 15:41:44 +0530 Subject: [PATCH] test: usageKey param tests --- .../LibraryAuthoringPage.test.tsx | 36 +++++++++++++++++++ .../components/LibraryComponents.tsx | 10 ++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/library-authoring/LibraryAuthoringPage.test.tsx b/src/library-authoring/LibraryAuthoringPage.test.tsx index 70853f18b6..ce7091dad8 100644 --- a/src/library-authoring/LibraryAuthoringPage.test.tsx +++ b/src/library-authoring/LibraryAuthoringPage.test.tsx @@ -745,4 +745,40 @@ describe('', () => { expect(container.queryAllByText('Text').length).toBeGreaterThan(0); expect(container.queryAllByText('Collection').length).toBeGreaterThan(0); }); + + it('shows a single block when usageKey query param is set', async () => { + render(, { + path, + routerProps: { + initialEntries: [ + `/library/${mockContentLibrary.libraryId}/components?usageKey=${mockXBlockFields.usageKeyHtml}`, + ], + }, + }); + await waitFor(() => { + expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, { + body: expect.stringContaining(mockXBlockFields.usageKeyHtml), + headers: expect.anything(), + method: 'POST', + }); + }); + expect(screen.queryByPlaceholderText('Displaying single block, clear filters to search')).toBeInTheDocument(); + const { displayName } = mockXBlockFields.dataHtml; + const sidebar = screen.getByTestId('library-sidebar'); + + const { getByText } = within(sidebar); + + // should display the component with passed param: usageKey in the sidebar + await waitFor(() => expect(getByText(displayName)).toBeInTheDocument()); + // clear usageKey filter + const clearFitlersButton = screen.getByRole('button', { name: /clear filters/i }); + fireEvent.click(clearFitlersButton); + await waitFor(() => { + expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, { + body: expect.not.stringContaining(mockXBlockFields.usageKeyHtml), + method: 'POST', + headers: expect.anything(), + }); + }); + }); }); diff --git a/src/library-authoring/components/LibraryComponents.tsx b/src/library-authoring/components/LibraryComponents.tsx index c3a7758ec0..772dd76313 100644 --- a/src/library-authoring/components/LibraryComponents.tsx +++ b/src/library-authoring/components/LibraryComponents.tsx @@ -1,3 +1,5 @@ +import { useEffect } from 'react'; + import { LoadingSpinner } from '../../generic/Loading'; import { useLoadOnScroll } from '../../hooks'; import { useSearchContext } from '../../search-manager'; @@ -30,9 +32,11 @@ const LibraryComponents = ({ variant }: LibraryComponentsProps) => { } = useSearchContext(); const { openAddContentSidebar, openComponentInfoSidebar } = useLibraryContext(); - if (usageKey) { - openComponentInfoSidebar(usageKey); - } + useEffect(() => { + if (usageKey) { + openComponentInfoSidebar(usageKey); + } + }, [usageKey]); const componentList = variant === 'preview' ? hits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT) : hits;