Skip to content

Commit

Permalink
fix: more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Oct 10, 2024
1 parent 6471d5a commit d5f1438
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ describe('<LibraryAuthoringPage />', () => {
await waitFor(() => { expect(fetchMock).toHaveFetchedTimes(1, searchEndpoint, 'post'); });

expect(await screen.findByText('Content library')).toBeInTheDocument();
screen.logTestingPlaygroundURL();
expect(screen.getByText('You have not added any content to this library yet.')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: /add component/i })).not.toBeInTheDocument();
expect(screen.getByText('Read Only')).toBeInTheDocument();
Expand Down
17 changes: 7 additions & 10 deletions src/library-authoring/LibraryAuthoringPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ const HeaderActions = () => {
);
};

const SubHeaderTitle = ({ title, showReadOnlyBadge }: { title: string, showReadOnlyBadge: boolean }) => {
const SubHeaderTitle = ({ title }: { title: string }) => {
const intl = useIntl();

const { readOnly, componentPickerMode } = useLibraryContext();

const showReadOnlyBadge = readOnly && !componentPickerMode;

return (
<Stack direction="vertical">
{title}
Expand Down Expand Up @@ -202,16 +206,9 @@ const LibraryAuthoringPage = () => {
extraFilter={`context_key = "${libraryId}"`}
>
<SubHeader
title={(
<SubHeaderTitle
title={libraryData.title}
showReadOnlyBadge={!componentPickerMode && !libraryData.canEditLibrary}
/>
)}
title={<SubHeaderTitle title={libraryData.title} />}
subtitle={intl.formatMessage(messages.headingSubtitle)}
headerActions={(
<HeaderActions />
)}
headerActions={<HeaderActions />}
/>
<SearchKeywordsField className="w-50" />
<div className="d-flex mt-3 align-items-center">
Expand Down
49 changes: 24 additions & 25 deletions src/library-authoring/component-info/ComponentAdvancedInfo.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
fireEvent,
initializeMocks,
render,
render as baseRender,
screen,
waitFor,
} from '../../testUtils';
Expand All @@ -21,32 +21,39 @@ mockXBlockAssets.applyMock();
mockXBlockOLX.applyMock();
const setOLXspy = mockSetXBlockOLX.applyMock();

const withLibraryId = (libraryId: string = mockContentLibrary.libraryId) => ({
extraWrapper: ({ children }: { children: React.ReactNode }) => (
<LibraryProvider libraryId={libraryId}>{children}</LibraryProvider>
),
});
const render = (
usageKey: string = mockLibraryBlockMetadata.usageKeyPublished,
libraryId: string = mockContentLibrary.libraryId,
) => baseRender(
<ComponentAdvancedInfo />,
{
extraWrapper: ({ children }: { children: React.ReactNode }) => (
<LibraryProvider libraryId={libraryId} sidebarComponentUsageKey={usageKey}>{children}</LibraryProvider>
),
},
);

describe('<ComponentAdvancedInfo />', () => {
it('should display nothing when collapsed', async () => {
beforeEach(() => {
initializeMocks();
render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId());
});

it('should display nothing when collapsed', async () => {
render();
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
expect(expandButton).toBeInTheDocument();
expect(screen.queryByText(mockLibraryBlockMetadata.usageKeyPublished)).not.toBeInTheDocument();
});

it('should display the usage key of the block (when expanded)', async () => {
initializeMocks();
render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId());
render();
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
expect(await screen.findByText(mockLibraryBlockMetadata.usageKeyPublished)).toBeInTheDocument();
});

it('should display the static assets of the block (when expanded)', async () => {
initializeMocks();
render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId());
render();
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
expect(await screen.findByText(/static\/image1\.png/)).toBeInTheDocument();
Expand All @@ -56,30 +63,24 @@ describe('<ComponentAdvancedInfo />', () => {
});

it('should display the OLX source of the block (when expanded)', async () => {
initializeMocks();
render(<ComponentAdvancedInfo usageKey={mockXBlockOLX.usageKeyHtml} />, withLibraryId());
render(mockXBlockOLX.usageKeyHtml);
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
// Because of syntax highlighting, the OLX will be borken up by many different tags so we need to search for
// just a substring:
const olxPart = /This is a text component which uses/;
expect(await screen.findByText(olxPart)).toBeInTheDocument();
await waitFor(() => expect(screen.getByText(olxPart)).toBeInTheDocument());
});

it('does not display "Edit OLX" button when the library is read-only', async () => {
initializeMocks();
render(
<ComponentAdvancedInfo usageKey={mockXBlockOLX.usageKeyHtml} />,
withLibraryId(mockContentLibrary.libraryIdReadOnly),
);
render(mockXBlockOLX.usageKeyHtml, mockContentLibrary.libraryIdReadOnly);
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
expect(screen.queryByRole('button', { name: /Edit OLX/ })).not.toBeInTheDocument();
});

it('can edit the OLX', async () => {
initializeMocks();
render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId());
render();
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
const editButton = await screen.findByRole('button', { name: /Edit OLX/ });
Expand All @@ -94,13 +95,11 @@ describe('<ComponentAdvancedInfo />', () => {
});

it('displays an error if editing the OLX failed', async () => {
initializeMocks();

setOLXspy.mockImplementation(async () => {
throw new Error('Example error - setting OLX failed');
});

render(<ComponentAdvancedInfo usageKey={mockLibraryBlockMetadata.usageKeyPublished} />, withLibraryId());
render(mockLibraryBlockMetadata.usageKeyPublished);
const expandButton = await screen.findByRole('button', { name: /Advanced details/ });
fireEvent.click(expandButton);
const editButton = await screen.findByRole('button', { name: /Edit OLX/ });
Expand Down
15 changes: 9 additions & 6 deletions src/library-authoring/component-info/ComponentAdvancedInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
} from '../data/apiHooks';
import messages from './messages';

interface Props {
usageKey: string;
}

export const ComponentAdvancedInfo: React.FC<Props> = ({ usageKey }) => {
export const ComponentAdvancedInfo: React.FC<Record<never, never>> = () => {
const intl = useIntl();
const { readOnly } = useLibraryContext();
const { readOnly, sidebarComponentUsageKey: usageKey } = useLibraryContext();

// istanbul ignore if: this should never happen in production
if (!usageKey) {
throw new Error('sidebarComponentUsageKey is required to render ComponentAdvancedInfo');
}

const { data: olx, isLoading: isOLXLoading } = useXBlockOLX(usageKey);
const { data: assets, isLoading: areAssetsLoading } = useXBlockAssets(usageKey);
const editorRef = React.useRef<EditorAccessor | undefined>(undefined);
Expand All @@ -45,6 +47,7 @@ export const ComponentAdvancedInfo: React.FC<Props> = ({ usageKey }) => {
// On error, an <Alert> is shown below. We catch here to avoid the error propagating up.
});
}, [editorRef, olxUpdater, intl]);

return (
<Collapsible
styling="basic"
Expand Down
2 changes: 1 addition & 1 deletion src/library-authoring/component-info/ComponentDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const ComponentDetails = () => {
{...componentMetadata}
/>
</div>
<ComponentAdvancedInfo usageKey={usageKey} />
<ComponentAdvancedInfo />
</Stack>
);
};
Expand Down

0 comments on commit d5f1438

Please sign in to comment.