Skip to content

Commit

Permalink
test: adapt tests to the latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bra-i-am committed Jan 30, 2025
1 parent f7625e8 commit eb6203a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 152 deletions.
6 changes: 4 additions & 2 deletions src/studio-home/card-item/CardItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ describe('<CardItem />', () => {
render(<CardItem {...props} />);
const courseTitleLink = screen.getByText(props.displayName);
expect(courseTitleLink).toHaveAttribute('href', `${getConfig().STUDIO_BASE_URL}${props.url}`);
const dropDownMenu = screen.getByTestId('toggle-dropdown');
fireEvent.click(dropDownMenu);
const btnReRunCourse = screen.getByText(messages.btnReRunText.defaultMessage);
expect(btnReRunCourse).toHaveAttribute('href', trimSlashes(props.rerunLink));
expect(btnReRunCourse).toHaveAttribute('href', props.rerunLink);
const viewLiveLink = screen.getByText(messages.viewLiveBtnText.defaultMessage);
expect(viewLiveLink).toHaveAttribute('href', props.lmsLink);
});

it('should render correct links for non-library course pagination', () => {
const props = studioHomeMock.archivedCourses[0];
render(<CardItem {...props} isPaginated />);
render(<CardItem {...props} />);
const courseTitleLink = screen.getByText(props.displayName);
expect(courseTitleLink).toHaveAttribute('href', `${getConfig().STUDIO_BASE_URL}${props.url}`);
const dropDownMenu = screen.getByTestId('toggle-dropdown');
Expand Down
3 changes: 0 additions & 3 deletions src/studio-home/card-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import React from 'react';
import { useSelector } from 'react-redux';
import {
Card,
Hyperlink,
Dropdown,
IconButton,
ActionRow,
} from '@openedx/paragon';
import { MoreHoriz } from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
Expand All @@ -16,7 +14,6 @@ import { getWaffleFlags } from '../../data/selectors';
import { COURSE_CREATOR_STATES } from '../../constants';
import { getStudioHomeData } from '../data/selectors';
import messages from '../messages';
import { trimSlashes } from './utils';

interface BaseProps {
displayName: string;
Expand Down
10 changes: 5 additions & 5 deletions src/studio-home/data/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getStudioHomeLibraries,
} from './api';
import {
generateGetStudioCoursesApiResponse,
generateGetStudioCoursesApiResponseV2,
generateGetStudioHomeDataApiResponse,
generateGetStudioHomeLibrariesApiResponse,
} from '../factories/mockApiResponses';
Expand Down Expand Up @@ -50,19 +50,19 @@ describe('studio-home api calls', () => {

it('should get studio courses data', async () => {
const apiLink = `${getApiBaseUrl()}/api/contentstore/v1/home/courses`;
axiosMock.onGet(apiLink).reply(200, generateGetStudioCoursesApiResponse());
axiosMock.onGet(apiLink).reply(200, generateGetStudioCoursesApiResponseV2());
const result = await getStudioHomeCourses('');
const expected = generateGetStudioCoursesApiResponse();
const expected = generateGetStudioCoursesApiResponseV2();

expect(axiosMock.history.get[0].url).toEqual(apiLink);
expect(result).toEqual(expected);
});

it('should get studio courses data v2', async () => {
const apiLink = `${getApiBaseUrl()}/api/contentstore/v2/home/courses`;
axiosMock.onGet(apiLink).reply(200, generateGetStudioCoursesApiResponse());
axiosMock.onGet(apiLink).reply(200, generateGetStudioCoursesApiResponseV2());
const result = await getStudioHomeCoursesV2('');
const expected = generateGetStudioCoursesApiResponse();
const expected = generateGetStudioCoursesApiResponseV2();

expect(axiosMock.history.get[0].url).toEqual(apiLink);
expect(result).toEqual(expected);
Expand Down
2 changes: 1 addition & 1 deletion src/studio-home/data/slice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('updateStudioHomeCoursesCustomParams action', () => {
studioHomeData: {},
studioHomeCoursesRequestParams: {
currentPage: 1,
search: undefined,
search: '',
order: 'display_name',
archivedOnly: undefined,
activeOnly: undefined,
Expand Down
28 changes: 0 additions & 28 deletions src/studio-home/factories/mockApiResponses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,6 @@ export const generateGetStudioHomeDataApiResponse = () => ({
allowToCreateNewOrg: false,
});

/** Mock for the deprecated /api/contentstore/v1/home/courses endpoint. Note this endpoint is NOT paginated. */
export const generateGetStudioCoursesApiResponse = () => ({
archivedCourses: /** @type {any[]} */([]),
courses: [
{
courseKey: 'course-v1:HarvardX+123+2023',
displayName: 'Managing Risk in the Information Age',
lmsLink: '//localhost:18000/courses/course-v1:HarvardX+123+2023/jump_to/block-v1:HarvardX+123+2023+type@course+block@course',
number: '123',
org: 'HarvardX',
rerunLink: '/course_rerun/course-v1:HarvardX+123+2023',
run: '2023',
url: '/course/course-v1:HarvardX+123+2023',
},
{
courseKey: 'org.0/course_0/Run_0',
displayName: 'Run 0',
lmsLink: null,
number: 'course_0',
org: 'org.0',
rerunLink: null,
run: 'Run_0',
url: null,
},
],
inProcessCourseActions: [],
});

export const generateGetStudioCoursesApiResponseV2 = () => ({
count: 5,
next: null,
Expand Down
71 changes: 9 additions & 62 deletions src/studio-home/tabs-section/TabsSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import TabsSection from '.';
import {
initialState,
generateGetStudioHomeDataApiResponse,
generateGetStudioCoursesApiResponse,
generateGetStudioCoursesApiResponseV2,
generateGetStudioHomeLibrariesApiResponse,
} from '../factories/mockApiResponses';
Expand All @@ -29,7 +28,6 @@ const { studioShortName } = studioHomeMock;

let axiosMock;
let store;
const courseApiLink = `${getApiBaseUrl()}/api/contentstore/v1/home/courses`;
const courseApiLinkV2 = `${getApiBaseUrl()}/api/contentstore/v2/home/courses`;
const libraryApiLink = `${getApiBaseUrl()}/api/contentstore/v1/home/libraries`;

Expand All @@ -38,7 +36,6 @@ const librariesBetaTabTitle = /Libraries Beta/;

const tabSectionComponent = (overrideProps) => (
<TabsSection
isPaginationCoursesEnabled={false}
showNewCourseContainer={false}
onClickNewCourse={() => {}}
isShowProcessing
Expand Down Expand Up @@ -76,16 +73,6 @@ describe('<TabsSection />', () => {

it('should render all tabs correctly', async () => {
const data: any = generateGetStudioHomeDataApiResponse();
data.archivedCourses = [{
courseKey: 'course-v1:MachineLearning+123+2023',
displayName: 'Machine Learning',
lmsLink: '//localhost:18000/courses/course-v1:MachineLearning+123+2023/jump_to/block-v1:MachineLearning+123+2023+type@course+block@course',
number: '123',
org: 'LSE',
rerunLink: '/course_rerun/course-v1:MachineLearning+123+2023',
run: '2023',
url: '/course/course-v1:MachineLearning+123+2023',
}];

render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, data);
Expand All @@ -96,8 +83,6 @@ describe('<TabsSection />', () => {
expect(screen.getByRole('tab', { name: librariesBetaTabTitle })).toBeInTheDocument();

expect(screen.getByRole('tab', { name: tabMessages.legacyLibrariesTabTitle.defaultMessage })).toBeInTheDocument();

expect(screen.getByRole('tab', { name: tabMessages.archivedTabTitle.defaultMessage })).toBeInTheDocument();
});

it('should render only 1 library tab when libraries-v2 disabled', async () => {
Expand Down Expand Up @@ -135,9 +120,9 @@ describe('<TabsSection />', () => {
describe('course tab', () => {
it('should render specific course details', async () => {
render();
const data = generateGetStudioCoursesApiResponse();
const data = generateGetStudioCoursesApiResponseV2();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLink).reply(200, data);
axiosMock.onGet(courseApiLinkV2).reply(200, data);
await executeThunk(fetchStudioHomeData(), store.dispatch);

expect(screen.getByText(studioHomeMock.courses[0].displayName)).toBeVisible();
Expand All @@ -148,12 +133,12 @@ describe('<TabsSection />', () => {
});

it('should render default sections when courses are empty', async () => {
const data = generateGetStudioCoursesApiResponse();
data.courses = [];
const data = generateGetStudioCoursesApiResponseV2();
data.results.courses = [];

render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLink).reply(200, data);
axiosMock.onGet(courseApiLinkV2).reply(200, data);
await executeThunk(fetchStudioHomeData(), store.dispatch);

expect(screen.getByText(`Are you staff on an existing ${studioShortName} course?`)).toBeInTheDocument();
Expand All @@ -168,7 +153,7 @@ describe('<TabsSection />', () => {
it('should render course fetch failure alert', async () => {
render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLink).reply(404);
axiosMock.onGet(courseApiLinkV2).reply(404);
await executeThunk(fetchStudioHomeData(), store.dispatch);

expect(screen.getByText(tabMessages.courseTabErrorMessage.defaultMessage)).toBeVisible();
Expand All @@ -178,7 +163,7 @@ describe('<TabsSection />', () => {
render({ isPaginationCoursesEnabled: true });
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLinkV2).reply(200, generateGetStudioCoursesApiResponseV2());
await executeThunk(fetchStudioHomeData('', true, {}, true), store.dispatch);
await executeThunk(fetchStudioHomeData('', true, {}), store.dispatch);
const data = generateGetStudioCoursesApiResponseV2();
const coursesLength = data.results.courses.length;
const totalItems = data.count;
Expand Down Expand Up @@ -275,47 +260,9 @@ describe('<TabsSection />', () => {
});
});

describe('archived tab', () => {
it('should switch to Archived tab and render specific archived course details', async () => {
render();
const data = generateGetStudioCoursesApiResponse();
data.archivedCourses = studioHomeMock.archivedCourses;
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLink).reply(200, data);
await executeThunk(fetchStudioHomeData(), store.dispatch);

const archivedTab = screen.getByText(tabMessages.archivedTabTitle.defaultMessage);
fireEvent.click(archivedTab);

expect(screen.getByText(studioHomeMock.archivedCourses[0].displayName)).toBeVisible();

expect(screen.getByText(
`${studioHomeMock.archivedCourses[0].org} / ${studioHomeMock.archivedCourses[0].number} / ${studioHomeMock.archivedCourses[0].run}`,
)).toBeVisible();
});

it('should hide Archived tab when archived courses are empty', async () => {
const data = generateGetStudioCoursesApiResponse();
data.archivedCourses = [];

render();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, generateGetStudioHomeDataApiResponse());
axiosMock.onGet(courseApiLink).reply(200, data);
await executeThunk(fetchStudioHomeData(), store.dispatch);

expect(screen.getByRole('tab', { name: tabMessages.coursesTabTitle.defaultMessage })).toBeInTheDocument();

expect(screen.getByRole('tab', { name: librariesBetaTabTitle })).toBeInTheDocument();

expect(screen.getByRole('tab', { name: tabMessages.legacyLibrariesTabTitle.defaultMessage })).toBeInTheDocument();

expect(screen.queryByRole('tab', { name: tabMessages.archivedTabTitle.defaultMessage })).toBeNull();
});
});

describe('library tab', () => {
beforeEach(() => {
axiosMock.onGet(courseApiLink).reply(200, generateGetStudioCoursesApiResponse());
axiosMock.onGet(courseApiLinkV2).reply(200, generateGetStudioCoursesApiResponseV2());
});
it('should switch to Legacy Libraries tab and render specific v1 library details', async () => {
render();
Expand Down Expand Up @@ -391,7 +338,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.getAllByText('Page 1, Current Page, of 2')[0]).toBeVisible();

expect(screen.getByText(contentLibrariesListV2.results[0].title)).toBeVisible();
expect(screen.getByText(
Expand Down
32 changes: 0 additions & 32 deletions src/studio-home/tabs-section/courses-tab/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import { initialState } from '../../factories/mockApiResponses';
import CoursesTab from '.';

const onClickNewCourse = jest.fn();
const isShowProcessing = false;
const isLoading = false;
const isFailed = false;
const numPages = 1;
const coursesCount = studioHomeMock.courses.length;
const isEnabledPagination = true;
const showNewCourseContainer = true;

const renderComponent = (overrideProps = {}, studioHomeState = {}) => {
Expand All @@ -40,12 +38,10 @@ const renderComponent = (overrideProps = {}, studioHomeState = {}) => {
coursesDataItems={studioHomeMock.courses}
showNewCourseContainer={showNewCourseContainer}
onClickNewCourse={onClickNewCourse}
isShowProcessing={isShowProcessing}
isLoading={isLoading}
isFailed={isFailed}
numPages={numPages}
coursesCount={coursesCount}
isEnabledPagination={isEnabledPagination}
{...overrideProps}
/>
</IntlProvider>
Expand Down Expand Up @@ -77,18 +73,6 @@ describe('<CoursesTab />', () => {
expect(coursesFilterSearchInput).toBeInTheDocument();
});

it('should not render pagination and filter elements when isEnabledPagination is false', () => {
renderComponent({ isEnabledPagination: false });
const coursesPaginationInfo = screen.queryByTestId('pagination-info');
const coursesTypesMenu = screen.queryByTestId('dropdown-toggle-course-type-menu');
const coursesOrderMenu = screen.queryByTestId('dropdown-toggle-courses-order-menu');
const coursesFilterSearchInput = screen.queryByTestId('input-filter-courses-search');
expect(coursesPaginationInfo).not.toBeInTheDocument();
expect(coursesTypesMenu).not.toBeInTheDocument();
expect(coursesOrderMenu).not.toBeInTheDocument();
expect(coursesFilterSearchInput).not.toBeInTheDocument();
});

it('should render loading spinner when isLoading is true and isFiltered is false', () => {
const props = { isLoading: true, coursesDataItems: [] };
const customStoreData = { studioHomeCoursesRequestParams: { currentPage: 1, isFiltered: true } };
Expand All @@ -113,22 +97,6 @@ describe('<CoursesTab />', () => {
expect(alertCoursesNotFound).toBeInTheDocument();
});

it('should render processing courses component when isEnabledPagination is false and isShowProcessing is true', () => {
const props = { isShowProcessing: true, isEnabledPagination: false };
const customStoreData = {
studioHomeData: {
inProcessCourseActions: [],
},
studioHomeCoursesRequestParams: {
currentPage: 1,
isFiltered: true,
},
};
renderComponent(props, customStoreData);
const alertCoursesNotFound = screen.queryByTestId('processing-courses-title');
expect(alertCoursesNotFound).toBeInTheDocument();
});

it('should render CollapsibleStateWithAction when courseCreatorStatus is true', () => {
const props = { isShowProcessing: true, isEnabledPagination: false };
const customStoreData = {
Expand Down
21 changes: 2 additions & 19 deletions src/studio-home/tabs-section/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { getLoadingStatuses, getStudioHomeData } from '../data/selectors';
import messages from './messages';
import LibrariesTab from './libraries-tab';
import LibrariesV2Tab from './libraries-v2-tab/index';
import ArchivedTab from './archived-tab';
import CoursesTab from './courses-tab';
import { RequestStatus } from '../../data/constants';
import { fetchLibraryData } from '../data/thunks';
Expand Down Expand Up @@ -66,7 +65,7 @@ const TabsSection = ({
}, [pathname]);

const {
courses, libraries, archivedCourses,
courses, libraries,
numPages, coursesCount,
} = useSelector(getStudioHomeData);
const {
Expand Down Expand Up @@ -100,22 +99,6 @@ const TabsSection = ({
</Tab>,
);

if (archivedCourses?.length) {
tabs.push(
<Tab
key={TABS_LIST.archived}
eventKey={TABS_LIST.archived}
title={intl.formatMessage(messages.archivedTabTitle)}
>
<ArchivedTab
archivedCoursesData={archivedCourses}
isLoading={isLoadingCourses}
isFailed={isFailedCoursesPage}
/>
</Tab>,
);
}

if (librariesV2Enabled) {
tabs.push(
<Tab
Expand Down Expand Up @@ -164,7 +147,7 @@ const TabsSection = ({
}

return tabs;
}, [archivedCourses, showNewCourseContainer, isLoadingCourses, isLoadingLibraries]);
}, [showNewCourseContainer, isLoadingCourses, isLoadingLibraries]);

const handleSelectTab = (tab) => {
if (tab === TABS_LIST.courses) {
Expand Down

0 comments on commit eb6203a

Please sign in to comment.