From cae4f3a481b4ca2c2b066e4ea0d836e1fe4eab93 Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Tue, 13 Aug 2024 20:37:49 +0530 Subject: [PATCH] test: add test cases --- .../MasqueradeWidget.test.jsx | 29 +++++- .../MasqueradeWidgetOption.test.jsx | 97 +++++++++++++++++++ 2 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/instructor-toolbar/masquerade-widget/MasqueradeWidgetOption.test.jsx diff --git a/src/instructor-toolbar/masquerade-widget/MasqueradeWidget.test.jsx b/src/instructor-toolbar/masquerade-widget/MasqueradeWidget.test.jsx index b7604c0991..7349858a37 100644 --- a/src/instructor-toolbar/masquerade-widget/MasqueradeWidget.test.jsx +++ b/src/instructor-toolbar/masquerade-widget/MasqueradeWidget.test.jsx @@ -45,14 +45,13 @@ describe('Masquerade Widget Dropdown', () => { courseware = store.getState().courseware; axiosMock = new MockAdapter(getAuthenticatedHttpClient()); masqueradeUrl = `${getConfig().LMS_BASE_URL}/courses/${courseware.courseId}/masquerade`; - }); - - beforeEach(() => { mockData = { courseId: courseware.courseId, onError: () => {}, }; + }); + beforeEach(() => { mockResponse = { success: true, active: { @@ -111,4 +110,28 @@ describe('Masquerade Widget Dropdown', () => { }); }); }); + + it('handles the clicks with toggle', async () => { + const { container } = render(); + await waitFor(() => expect(axiosMock.history.get).toHaveLength(1)); + + const dropdownToggle = container.querySelector('.dropdown-toggle'); + await act(async () => { + await fireEvent.click(dropdownToggle); + }); + const dropdownMenu = container.querySelector('.dropdown-menu'); + const studentOption = getAllByRole(dropdownMenu, 'button', { hidden: true }).filter( + button => (button.textContent === 'Specific Student...'), + )[0]; + await act(async () => { + await fireEvent.click(studentOption); + }); + getAllByRole(dropdownMenu, 'button', { hidden: true }).forEach(button => { + if (button.textContent === 'Specific Student...') { + expect(button).toHaveClass('active'); + } else { + expect(button).not.toHaveClass('active'); + } + }); + }); }); diff --git a/src/instructor-toolbar/masquerade-widget/MasqueradeWidgetOption.test.jsx b/src/instructor-toolbar/masquerade-widget/MasqueradeWidgetOption.test.jsx new file mode 100644 index 0000000000..4e6dde1476 --- /dev/null +++ b/src/instructor-toolbar/masquerade-widget/MasqueradeWidgetOption.test.jsx @@ -0,0 +1,97 @@ +import React from 'react'; +import { getAllByRole } from '@testing-library/dom'; +import { act } from '@testing-library/react'; +import { getConfig } from '@edx/frontend-platform'; +import MasqueradeWidgetOption from './MasqueradeWidgetOption'; +import { + render, fireEvent, initializeTestStore, +} from '../../setupTest'; + +const originalConfig = jest.requireActual('@edx/frontend-platform').getConfig(); +jest.mock('@edx/frontend-platform', () => ({ + ...jest.requireActual('@edx/frontend-platform'), + getConfig: jest.fn(), +})); +getConfig.mockImplementation(() => originalConfig); + +describe('Masquerade Widget Dropdown', () => { + let courseware; + let mockDataStaff; + let mockDataStudent; + let active; + + beforeAll(async () => { + const store = await initializeTestStore(); + courseware = store.getState().courseware; + active = { + courseKey: courseware.courseId, + groupId: null, + role: 'staff', + userName: null, + userPartitionId: null, + groupName: null, + }; + mockDataStaff = { + groupId: null, + groupName: 'Staff', + key: 'Staff', + role: 'staff', + selected: active, + userName: null, + userPartitionId: null, + userNameInputToggle: () => {}, + onSubmit: () => {}, + }; + mockDataStudent = { + groupId: null, + groupName: 'Specific Student...', + key: 'Specific Student...', + role: 'student', + selected: active, + userName: '', + userPartitionId: null, + userNameInputToggle: () => {}, + onSubmit: () => {}, + }; + Object.defineProperty(global, 'location', { + configurable: true, + value: { reload: jest.fn() }, + }); + }); + + it('renders masquerade active option correctly', async () => { + const { container } = render(); + const button = getAllByRole(container, 'button', { hidden: true })[0]; + expect(button).toHaveTextContent('Staff'); + expect(button).toHaveClass('active'); + }); + + it('renders masquerade inactive option correctly', async () => { + const { container } = render(); + const button = getAllByRole(container, 'button', { hidden: true })[0]; + expect(button).toHaveTextContent('Specific Student...'); + expect(button).not.toHaveClass('active'); + }); + + it('handles the clicks regular option', () => { + const onSubmit = jest.fn().mockImplementation(() => Promise.resolve()); + const { container } = render(); + const button = getAllByRole(container, 'button', { hidden: true })[0]; + act(() => { + fireEvent.click(button); + }); + expect(onSubmit).toHaveBeenCalled(); + }); + + it('handles the clicks student option', () => { + const userNameInputToggle = jest.fn().mockImplementation(() => Promise.resolve()); + const { container } = render( + , + ); + const button = getAllByRole(container, 'button', { hidden: true })[0]; + act(() => { + fireEvent.click(button); + }); + expect(userNameInputToggle).toHaveBeenCalled(); + }); +});