From 4799d74e9866f07fa89a0a886c16c424df300dff Mon Sep 17 00:00:00 2001 From: Pooja Kulkarni Date: Fri, 13 Sep 2024 10:05:54 -0400 Subject: [PATCH] fix: update tests --- src/studio-home/StudioHome.test.jsx | 58 ++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/studio-home/StudioHome.test.jsx b/src/studio-home/StudioHome.test.jsx index ad02f4373f..134756dfc7 100644 --- a/src/studio-home/StudioHome.test.jsx +++ b/src/studio-home/StudioHome.test.jsx @@ -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'; @@ -41,6 +41,11 @@ jest.mock('react-router-dom', () => ({ }), })); +jest.mock('@edx/frontend-platform', () => ({ + ...jest.requireActual('@edx/frontend-platform'), + getConfig: jest.fn(), +})); + const RootWrapper = () => ( @@ -52,6 +57,9 @@ const RootWrapper = () => ( describe('', async () => { describe('api fetch fails', () => { beforeEach(async () => { + getConfig.mockImplementation(() => ({ + ...jest.requireActual('@edx/frontend-platform').getConfig(), + })); initializeMockApp({ authenticatedUser: { userId: 3, @@ -80,6 +88,9 @@ describe('', async () => { describe('api fetch succeeds', () => { beforeEach(async () => { + getConfig.mockImplementation(() => ({ + ...jest.requireActual('@edx/frontend-platform').getConfig(), + })); initializeMockApp({ authenticatedUser: { userId: 3, @@ -260,3 +271,48 @@ describe('', 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(); + + expect(getByText(`${studioShortName} home`)).toBeInTheDocument(); + }); + + it('should only dispatch fetchStudioHomeData once when pagination is enabled', async () => { + axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock); + + render(); + + expect(axiosMock.history.get.length).toBe(1); + }); +});