Skip to content

Commit

Permalink
test: fix test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Oct 18, 2024
1 parent 850588e commit d4d7671
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/library-authoring/__mocks__/contentLibrariesListV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
next: null,
previous: null,
count: 2,
num_pages: 1,
num_pages: 2,
current_page: 1,
start: 0,
results: [
Expand Down
39 changes: 36 additions & 3 deletions src/studio-home/tabs-section/TabsSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { getApiBaseUrl, getStudioHomeApiUrl } from '../data/api';
import { executeThunk } from '../../utils';
import { fetchLibraryData, fetchStudioHomeData } from '../data/thunks';
import { getContentLibraryV2ListApiUrl } from '../../library-authoring/data/api';
import { mockGetContentLibraryV2List } from '../../library-authoring/data/api.mocks';
import contentLibrariesListV2 from '../../library-authoring/__mocks__/contentLibrariesListV2';
import {
initializeMocks,
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('<TabsSection />', () => {
const newMocks = initializeMocks({ initialState });
store = newMocks.reduxStore;
axiosMock = newMocks.axiosMock;
axiosMock.onGet(getContentLibraryV2ListApiUrl()).reply(200, contentLibrariesListV2);
mockGetContentLibraryV2List.applyMock();
});

it('should render all tabs correctly', async () => {
Expand Down Expand Up @@ -391,6 +391,7 @@ describe('<TabsSection />', () => {
expect(librariesTab).toHaveClass('active');

expect(screen.getByText('Showing 2 of 2')).toBeVisible();
expect(screen.getByText('Page 1, Current Page, of 2')).toBeVisible();

expect(screen.getByText(contentLibrariesListV2.results[0].title)).toBeVisible();
expect(screen.getByText(
Expand All @@ -403,6 +404,22 @@ describe('<TabsSection />', () => {
)).toBeVisible();
});

it('should show a "not found" message if no v2 libraries were loaded', async () => {
mockGetContentLibraryV2List.applyMockEmpty();
render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
await executeThunk(fetchStudioHomeData(), store.dispatch);

const librariesTab = screen.getByRole('tab', { name: librariesBetaTabTitle });
fireEvent.click(librariesTab);

expect(librariesTab).toHaveClass('active');

expect(await screen.findByText(
tabMessages.librariesV2TabLibraryNotFoundAlertMessage.defaultMessage,
)).toBeVisible();
});

it('should hide Libraries tab when libraries are disabled', async () => {
const data = generateGetStudioHomeDataApiResponse();

Expand All @@ -414,7 +431,7 @@ describe('<TabsSection />', () => {
expect(screen.queryByText(tabMessages.legacyLibrariesTabTitle.defaultMessage)).toBeNull();
});

it('should render libraries fetch failure alert', async () => {
it('should render legacy libraries fetch failure alert', async () => {
render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(libraryApiLink).reply(404);
Expand All @@ -428,5 +445,21 @@ describe('<TabsSection />', () => {

expect(await screen.findByText(tabMessages.librariesTabErrorMessage.defaultMessage)).toBeVisible();
});

it('should render v2 libraries fetch failure alert', async () => {
mockGetContentLibraryV2List.applyMockError();
render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
await executeThunk(fetchStudioHomeData(), store.dispatch);

const librariesTab = screen.getByRole('tab', { name: librariesBetaTabTitle });
fireEvent.click(librariesTab);

expect(librariesTab).toHaveClass('active');

expect(await screen.findByText(
tabMessages.librariesTabErrorMessage.defaultMessage,
)).toBeVisible();
});
});
});
5 changes: 2 additions & 3 deletions src/studio-home/tabs-section/libraries-v2-tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const LibrariesV2Tab: React.FC<Props> = () => {
);
}

const hasV2Libraries = !isLoading && ((data!.results.length || 0) > 0);
const hasV2Libraries = !isLoading && !isError && ((data!.results.length || 0) > 0);

// TODO: update this link when tutorial is ready.
const librariesTutorialLink = (
Expand All @@ -69,7 +69,6 @@ const LibrariesV2Tab: React.FC<Props> = () => {

{isError ? (
<AlertMessage
title={intl.formatMessage(messages.librariesTabErrorMessage)}
variant="danger"
description={(
<Row className="m-0 align-items-center">
Expand All @@ -88,7 +87,7 @@ const LibrariesV2Tab: React.FC<Props> = () => {
setFilterParams={setFilterParams}
setCurrentPage={setCurrentPage}
/>
{ !isLoading
{!isLoading && !isError
&& (
<p>
{intl.formatMessage(messages.coursesPaginationInfo, {
Expand Down

0 comments on commit d4d7671

Please sign in to comment.