Skip to content

Commit

Permalink
test: remove component from collection
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 10, 2024
1 parent e6f7377 commit 43707d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
37 changes: 35 additions & 2 deletions src/library-authoring/collections/LibraryCollectionPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fetchMock from 'fetch-mock-jest';
import { cloneDeep } from 'lodash';
import MockAdapter from 'axios-mock-adapter/types';

import {
fireEvent,
initializeMocks,
Expand All @@ -17,6 +19,10 @@ import {
import { mockContentSearchConfig, mockGetBlockTypes } from '../../search-manager/data/api.mock';
import { mockBroadcastChannel, mockClipboardEmpty } from '../../generic/data/api.mock';
import { LibraryLayout } from '..';
import { getLibraryCollectionComponentApiUrl } from '../data/api';

let axiosMock: MockAdapter;
let mockShowToast;

mockClipboardEmpty.applyMock();
mockGetCollectionMetadata.applyMock();
Expand All @@ -40,7 +46,9 @@ const { title } = mockGetCollectionMetadata.collectionData;

describe('<LibraryCollectionPage />', () => {
beforeEach(() => {
initializeMocks();
const mocks = initializeMocks();
axiosMock = mocks.axiosMock;
mockShowToast = mocks.mockShowToast;
fetchMock.mockReset();

// The Meilisearch client-side API uses fetch, not Axios.
Expand Down Expand Up @@ -301,7 +309,6 @@ describe('<LibraryCollectionPage />', () => {
expect(mockResult0.display_name).toStrictEqual(displayName);
await renderLibraryCollectionPage();

// Click on the first component. It should appear twice, in both "Recently Modified" and "Components"
fireEvent.click((await screen.findAllByText(displayName))[0]);

const sidebar = screen.getByTestId('library-sidebar');
Expand All @@ -324,4 +331,30 @@ describe('<LibraryCollectionPage />', () => {

expect(screen.getByText(/no matching components/i)).toBeInTheDocument();
});

it('should remove component from collection and hides sidebar', async () => {
const url = getLibraryCollectionComponentApiUrl(
mockContentLibrary.libraryId,
mockCollection.collectionId,
);
axiosMock.onDelete(url).reply(204);
const displayName = 'Introduction to Testing';
await renderLibraryCollectionPage();

// open sidebar
fireEvent.click(await screen.findByText(displayName));
await waitFor(() => expect(screen.queryByTestId('library-sidebar')).toBeInTheDocument());

const menuBtns = await screen.findAllByRole('button', { name: 'Component actions menu' });
// open menu
fireEvent.click(menuBtns[0]);

fireEvent.click(await screen.findByText('Remove from collection'));
await waitFor(() => {
expect(axiosMock.history.delete.length).toEqual(1);
expect(mockShowToast).toHaveBeenCalledWith('Component successfully removed');
});
// Should close sidebar as component was removed
await waitFor(() => expect(screen.queryByTestId('library-sidebar')).not.toBeInTheDocument());
});
});
10 changes: 6 additions & 4 deletions src/library-authoring/components/ComponentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@openedx/paragon';
import { MoreVert } from '@openedx/paragon/icons';

import { useParams } from 'react-router';
import { updateClipboard } from '../../generic/data/api';
import { ToastContext } from '../../generic/toast-context';
import { type ContentHit } from '../../search-manager';
Expand All @@ -16,7 +17,6 @@ import messages from './messages';
import { STUDIO_CLIPBOARD_CHANNEL } from '../../constants';
import BaseComponentCard from './BaseComponentCard';
import { canEditComponent } from './ComponentEditorModal';
import { useParams } from 'react-router';
import { useRemoveComponentsFromCollection } from '../data/apiHooks';

type ComponentCardProps = {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const ComponentMenu = ({ usageKey }: { usageKey: string }) => {
}).catch(() => {
showToast(intl.formatMessage(messages.removeComponentFailure));

Check warning on line 56 in src/library-authoring/components/ComponentCard.tsx

View check run for this annotation

Codecov / codecov/patch

src/library-authoring/components/ComponentCard.tsx#L55-L56

Added lines #L55 - L56 were not covered by tests
});
}
};

return (
<Dropdown id="component-card-dropdown" onClick={(e) => e.stopPropagation()}>
Expand All @@ -75,9 +75,11 @@ export const ComponentMenu = ({ usageKey }: { usageKey: string }) => {
<Dropdown.Item onClick={updateClipboardClick}>
<FormattedMessage {...messages.menuCopyToClipboard} />
</Dropdown.Item>
{collectionId && <Dropdown.Item onClick={removeFromCollection}>
{collectionId && (
<Dropdown.Item onClick={removeFromCollection}>
<FormattedMessage {...messages.menuRemoveFromCollection} />
</Dropdown.Item>}
</Dropdown.Item>
)}
<Dropdown.Item disabled>
<FormattedMessage {...messages.menuAddToCollection} />
</Dropdown.Item>
Expand Down

0 comments on commit 43707d2

Please sign in to comment.