Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulkark committed Sep 13, 2024
1 parent 1039d10 commit 4799d74
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 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 Down Expand Up @@ -260,3 +271,48 @@ describe('<StudioHome />', async () => {
});
});
});

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();
});

it('should only dispatch fetchStudioHomeData once when pagination is enabled', async () => {
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);

render(<RootWrapper />);

expect(axiosMock.history.get.length).toBe(1);
});
});

0 comments on commit 4799d74

Please sign in to comment.