Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/library-authoring/components/CollectionCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import userEvent from '@testing-library/user-event';
import type MockAdapter from 'axios-mock-adapter';

import { mockContentLibrary } from '@src/library-authoring/data/api.mocks';
import {
initializeMocks, render as baseRender, screen, waitFor, within, fireEvent,
} from '../../testUtils';
Expand Down Expand Up @@ -43,14 +44,18 @@ const collectionHitSample: CollectionHit = {
let axiosMock: MockAdapter;
let mockShowToast;

const libraryId = 'lib:org1:Demo_Course';
const { libraryId } = mockContentLibrary;

const render = (ui: React.ReactElement, showOnlyPublished: boolean = false) => baseRender(ui, {
const render = (
ui: React.ReactElement,
showOnlyPublished: boolean = false,
libId: string = libraryId,
) => baseRender(ui, {
path: '/library/:libraryId',
params: { libraryId },
params: { libraryId: libId },
extraWrapper: ({ children }) => (
<LibraryProvider
libraryId="lib:Axim:TEST"
libraryId={libId}
showOnlyPublished={showOnlyPublished}
>
{children}
Expand All @@ -63,6 +68,7 @@ describe('<CollectionCard />', () => {
const mocks = initializeMocks();
axiosMock = mocks.axiosMock;
mockShowToast = mocks.mockShowToast;
mockContentLibrary.applyMock();
});

it('should render the card with title and description', () => {
Expand Down Expand Up @@ -193,4 +199,24 @@ describe('<CollectionCard />', () => {
expect(mockShowToast).toHaveBeenCalledWith('Failed to delete collection');
});
});

it('should not show delete button when library is read-only', async () => {
const user = userEvent.setup();

// Render with read-only library
render(<CollectionCard hit={collectionHitSample} />, false, mockContentLibrary.libraryIdReadOnly);

// Open menu
const menu = await screen.findByTestId('collection-card-menu-toggle');
expect(menu).toBeInTheDocument();
await user.click(menu);

// Delete button should not be visible in readonly mode
const deleteOption = screen.queryByRole('button', { name: 'Delete' });
expect(deleteOption).not.toBeInTheDocument();

// Open button should still be visible
const openOption = screen.queryByRole('button', { name: 'Open' });
expect(openOption).toBeInTheDocument();
});
});
9 changes: 6 additions & 3 deletions src/library-authoring/components/CollectionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const CollectionMenu = ({ hit } : CollectionMenuProps) => {
const intl = useIntl();
const { showToast } = useContext(ToastContext);
const { navigateTo } = useLibraryRoutes();
const { readOnly } = useLibraryContext();
const [isDeleteModalOpen, openDeleteModal, closeDeleteModal] = useToggle(false);
const { closeLibrarySidebar, sidebarItemInfo } = useSidebarContext();
const {
Expand Down Expand Up @@ -90,9 +91,11 @@ const CollectionMenu = ({ hit } : CollectionMenuProps) => {
<Dropdown.Item onClick={openCollection}>
<FormattedMessage {...messages.menuOpen} />
</Dropdown.Item>
<Dropdown.Item onClick={openDeleteModal}>
<FormattedMessage {...messages.deleteCollection} />
</Dropdown.Item>
{!readOnly && (
<Dropdown.Item onClick={openDeleteModal}>
<FormattedMessage {...messages.deleteCollection} />
</Dropdown.Item>
)}
</Dropdown.Menu>
</Dropdown>
<DeleteModal
Expand Down
28 changes: 23 additions & 5 deletions src/library-authoring/components/ComponentCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mockContentLibrary } from '@src/library-authoring/data/api.mocks';
import {
fireEvent,
render as baseRender,
Expand Down Expand Up @@ -48,12 +49,12 @@ const contentHit: ContentHit = {
publishStatus: PublishStatus.Published,
};

const libraryId = 'lib:org1:Demo_Course';
const render = () => baseRender(<ComponentCard hit={contentHit} />, {
const { libraryId } = mockContentLibrary;
const render = (libId: string = libraryId) => baseRender(<ComponentCard hit={contentHit} />, {
path: '/library/:libraryId',
params: { libraryId },
params: { libraryId: libId },
extraWrapper: ({ children }) => (
<LibraryProvider libraryId={libraryId}>
<LibraryProvider libraryId={libId}>
<SidebarProvider>
{ children }
</SidebarProvider>
Expand All @@ -62,6 +63,9 @@ const render = () => baseRender(<ComponentCard hit={contentHit} />, {
});

describe('<ComponentCard />', () => {
beforeEach(() => {
mockContentLibrary.applyMock();
});
it('should render the card with title and description', () => {
initializeMocks();
render();
Expand Down Expand Up @@ -127,7 +131,7 @@ describe('<ComponentCard />', () => {
expect(menu).toBeInTheDocument();
fireEvent.click(menu);

// Click copy to clipboard
// Click edit option
const editOption = await screen.findByRole('button', { name: 'Edit' });
expect(editOption).toBeInTheDocument();
fireEvent.click(editOption);
Expand All @@ -137,4 +141,18 @@ describe('<ComponentCard />', () => {
search: '',
});
});

it('should not show edit button when library is read-only', async () => {
initializeMocks();
render(mockContentLibrary.libraryIdReadOnly);

// Open menu
const menu = await screen.findByTestId('component-card-menu-toggle');
expect(menu).toBeInTheDocument();
fireEvent.click(menu);

// Edit button should not be visible in readonly mode
const editOption = screen.queryByRole('button', { name: 'Edit' });
expect(editOption).not.toBeInTheDocument();
});
});
27 changes: 17 additions & 10 deletions src/library-authoring/components/ComponentMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const ComponentMenu = ({ usageKey, index }: Props) => {
collectionId,
containerId,
openComponentEditor,
readOnly,
} = useLibraryContext();

const {
Expand Down Expand Up @@ -103,9 +104,11 @@ export const ComponentMenu = ({ usageKey, index }: Props) => {
data-testid="component-card-menu-toggle"
/>
<Dropdown.Menu>
<Dropdown.Item {...(canEdit ? { onClick: handleEdit } : { disabled: true })}>
<FormattedMessage {...messages.menuEdit} />
</Dropdown.Item>
{!readOnly && (
<Dropdown.Item {...(canEdit ? { onClick: handleEdit } : { disabled: true })}>
<FormattedMessage {...messages.menuEdit} />
</Dropdown.Item>
)}
<Dropdown.Item onClick={updateClipboardClick}>
<FormattedMessage {...messages.menuCopyToClipboard} />
</Dropdown.Item>
Expand All @@ -114,10 +117,12 @@ export const ComponentMenu = ({ usageKey, index }: Props) => {
<FormattedMessage {...messages.removeComponentFromUnitMenu} />
</Dropdown.Item>
)}
<Dropdown.Item onClick={openDeleteModal}>
<FormattedMessage {...messages.menuDelete} />
</Dropdown.Item>
{insideCollection && (
{!readOnly && (
<Dropdown.Item onClick={openDeleteModal}>
<FormattedMessage {...messages.menuDelete} />
</Dropdown.Item>
)}
{insideCollection && !readOnly && (
<Dropdown.Item onClick={removeFromCollection}>
<FormattedMessage
{...containerMessages.menuRemoveFromContainer}
Expand All @@ -127,9 +132,11 @@ export const ComponentMenu = ({ usageKey, index }: Props) => {
/>
</Dropdown.Item>
)}
<Dropdown.Item onClick={showManageCollections}>
<FormattedMessage {...containerMessages.menuAddToCollection} />
</Dropdown.Item>
{!readOnly && (
<Dropdown.Item onClick={showManageCollections}>
<FormattedMessage {...containerMessages.menuAddToCollection} />
</Dropdown.Item>
)}
</Dropdown.Menu>
{isDeleteModalOpen && (
<ComponentDeleter
Expand Down
36 changes: 36 additions & 0 deletions src/library-authoring/containers/ContainerCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,40 @@ describe('<ContainerCard />', () => {
});
expect(axiosMock.history.post[0].url).toEqual(url);
});

test.each([
ContainerType.Unit,
ContainerType.Subsection,
ContainerType.Section,
])('should not show delete and add to collection buttons when library is read-only for %s', async (containerType) => {
const containerHit = getContainerHitSample(containerType);
const user = userEvent.setup();

// Render with read-only library
baseRender(<ContainerCard hit={containerHit} />, {
path: '/library/:libraryId',
params: { libraryId: mockContentLibrary.libraryIdReadOnly },
extraWrapper: ({ children }) => (
<LibraryProvider libraryId={mockContentLibrary.libraryIdReadOnly}>
{children}
</LibraryProvider>
),
});

// Open menu
const menu = await screen.findByTestId('container-card-menu-toggle');
expect(menu).toBeInTheDocument();
await user.click(menu);

// Delete and Add to collection buttons should not be visible in readonly mode
const deleteOption = screen.queryByRole('button', { name: 'Delete' });
expect(deleteOption).not.toBeInTheDocument();

const addToCollectionOption = screen.queryByRole('button', { name: 'Add to collection' });
expect(addToCollectionOption).not.toBeInTheDocument();

// Copy button should still be visible
const copyOption = screen.queryByRole('button', { name: 'Copy to clipboard' });
expect(copyOption).toBeInTheDocument();
});
});
20 changes: 13 additions & 7 deletions src/library-authoring/containers/ContainerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type ContainerMenuProps = {

export const ContainerMenu = ({ containerKey, displayName, index } : ContainerMenuProps) => {
const intl = useIntl();
const { libraryId, collectionId, containerId } = useLibraryContext();
const {
libraryId, collectionId, containerId, readOnly,
} = useLibraryContext();
const {
sidebarItemInfo,
closeLibrarySidebar,
Expand Down Expand Up @@ -116,9 +118,11 @@ export const ContainerMenu = ({ containerKey, displayName, index } : ContainerMe
<Dropdown.Item onClick={handleCopy}>
<FormattedMessage {...messages.menuCopyContainer} />
</Dropdown.Item>
<Dropdown.Item onClick={confirmDelete}>
<FormattedMessage {...messages.menuDeleteContainer} />
</Dropdown.Item>
{!readOnly && (
<Dropdown.Item onClick={confirmDelete}>
<FormattedMessage {...messages.menuDeleteContainer} />
</Dropdown.Item>
)}
{(insideCollection || insideSection || insideSubsection) && (
<Dropdown.Item onClick={handleRemove}>
<FormattedMessage
Expand All @@ -129,9 +133,11 @@ export const ContainerMenu = ({ containerKey, displayName, index } : ContainerMe
/>
</Dropdown.Item>
)}
<Dropdown.Item onClick={showManageCollections}>
<FormattedMessage {...messages.menuAddToCollection} />
</Dropdown.Item>
{!readOnly && (
<Dropdown.Item onClick={showManageCollections}>
<FormattedMessage {...messages.menuAddToCollection} />
</Dropdown.Item>
)}
</Dropdown.Menu>
</Dropdown>
{isConfirmingDelete && (
Expand Down