Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BB-9101] Alternative fix for default filter #66

Merged
merged 2 commits into from
Sep 13, 2024
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
70 changes: 69 additions & 1 deletion src/studio-home/StudioHome.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { initializeMockApp } from '@edx/frontend-platform';
import { initializeMockApp, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { IntlProvider, injectIntl } from '@edx/frontend-platform/i18n';
import { AppProvider } from '@edx/frontend-platform/react';
Expand Down Expand Up @@ -41,6 +41,11 @@ jest.mock('react-router-dom', () => ({
}),
}));

jest.mock('@edx/frontend-platform', () => ({
...jest.requireActual('@edx/frontend-platform'),
getConfig: jest.fn(),
}));

const RootWrapper = () => (
<AppProvider store={store}>
<IntlProvider locale="en" messages={{}}>
Expand All @@ -52,6 +57,9 @@ const RootWrapper = () => (
describe('<StudioHome />', async () => {
describe('api fetch fails', () => {
beforeEach(async () => {
getConfig.mockImplementation(() => ({
...jest.requireActual('@edx/frontend-platform').getConfig(),
}));
initializeMockApp({
authenticatedUser: {
userId: 3,
Expand Down Expand Up @@ -80,6 +88,9 @@ describe('<StudioHome />', async () => {

describe('api fetch succeeds', () => {
beforeEach(async () => {
getConfig.mockImplementation(() => ({
...jest.requireActual('@edx/frontend-platform').getConfig(),
}));
initializeMockApp({
authenticatedUser: {
userId: 3,
Expand All @@ -95,6 +106,10 @@ describe('<StudioHome />', async () => {
useSelector.mockReturnValue(studioHomeMock);
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should render page and page title correctly', () => {
const { getByText } = render(<RootWrapper />);
expect(getByText(`${studioShortName} home`)).toBeInTheDocument();
Expand Down Expand Up @@ -258,5 +273,58 @@ describe('<StudioHome />', async () => {
expect(getByText('Looking for help with Studio?')).toBeInTheDocument();
expect(getByText('LMS')).toHaveAttribute('href', process.env.LMS_BASE_URL);
});

it('should dispatch updateSavingStatuses and fetchStudioHomeData when courseCreatorSavingStatus is SUCCESSFUL', () => {
useSelector.mockReturnValue({
courseCreatorSavingStatus: RequestStatus.SUCCESSFUL,
});
const { getByText } = render(<RootWrapper />);
expect(getByText(`${studioShortName} home`)).toBeInTheDocument();
});

it('should dispatch updateSavingStatuses and fetchStudioHomeData when deleteNotificationSavingStatus is SUCCESSFUL', () => {
useSelector.mockReturnValue({
deleteNotificationSavingStatus: RequestStatus.SUCCESSFUL,
});
const { getByText } = render(<RootWrapper />);
expect(getByText(`${studioShortName} home`)).toBeInTheDocument();
});
});
});

describe('Enable pagination', () => {
beforeEach(async () => {
getConfig.mockImplementation(() => ({
...jest.requireActual('@edx/frontend-platform').getConfig(),
ENABLE_HOME_PAGE_COURSE_API_V2: true,
}));
initializeMockApp({
authenticatedUser: {
userId: 3,
username: 'abc123',
administrator: true,
roles: [],
},
});
store = initializeStore();
axiosMock = new MockAdapter(getAuthenticatedHttpClient());
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
search: '?search=test',
},
});
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should dispatch fetchStudioHomeData with paginated parameters on component mount', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);

const { getByText } = render(<RootWrapper />);

expect(getByText(`${studioShortName} home`)).toBeInTheDocument();
});
});
8 changes: 4 additions & 4 deletions src/studio-home/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ const useStudioHome = (isPaginated = false) => {

useEffect(() => {
if (!isPaginated) {
dispatch(fetchStudioHomeData(location.search ?? ''));
dispatch(fetchStudioHomeData(location.search ?? '', false, { active_only: true }));
setShowNewCourseContainer(false);
}
}, [location.search]);

useEffect(() => {
if (isPaginated) {
const firstPage = 1;
dispatch(fetchStudioHomeData(location.search ?? '', false, { page: firstPage }, true));
dispatch(fetchStudioHomeData(location.search ?? '', false, { page: firstPage, active_only: true }, true));
}
}, []);

useEffect(() => {
if (courseCreatorSavingStatus === RequestStatus.SUCCESSFUL) {
dispatch(updateSavingStatuses({ courseCreatorSavingStatus: '' }));
dispatch(fetchStudioHomeData());
dispatch(fetchStudioHomeData('', false, { active_only: true }));
}
}, [courseCreatorSavingStatus]);

useEffect(() => {
if (deleteNotificationSavingStatus === RequestStatus.SUCCESSFUL) {
dispatch(updateSavingStatuses({ courseCreatorSavingStatus: '' }));
dispatch(fetchStudioHomeData());
dispatch(fetchStudioHomeData('', false, { active_only: true }));
} else if (deleteNotificationSavingStatus === RequestStatus.FAILED) {
dispatch(updateSavingStatuses({ deleteNotificationSavingStatus: '' }));
}
Expand Down
Loading