Skip to content

Commit

Permalink
test: usageKey param tests
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 23, 2024
1 parent 7394dea commit b0dd84f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,40 @@ describe('<LibraryAuthoringPage />', () => {
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(<LibraryLayout />, {
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(),
});
});
});
});
10 changes: 7 additions & 3 deletions src/library-authoring/components/LibraryComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect } from 'react';

import { LoadingSpinner } from '../../generic/Loading';
import { useLoadOnScroll } from '../../hooks';
import { useSearchContext } from '../../search-manager';
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit b0dd84f

Please sign in to comment.