|
1 | | -// TODO: UPDATE TESTS |
2 | | -// import { getConfig } from '@edx/frontend-platform'; |
3 | | -// import MockAdapter from 'axios-mock-adapter'; |
4 | | -// import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
5 | | -// import { |
6 | | -// initializeTestStore, render, screen, waitFor, getByText, logUnhandledRequests, |
7 | | -// } from '../setupTest'; |
8 | | -// import InstructorToolbar from './index'; |
9 | | - |
10 | | -// const originalConfig = jest.requireActual('@edx/frontend-platform').getConfig(); |
11 | | -// jest.mock('@edx/frontend-platform', () => ({ |
12 | | -// ...jest.requireActual('@edx/frontend-platform'), |
13 | | -// getConfig: jest.fn(), |
14 | | -// })); |
15 | | -// getConfig.mockImplementation(() => originalConfig); |
16 | | - |
17 | | -// describe('Instructor Toolbar', () => { |
18 | | -// let courseware; |
19 | | -// let models; |
20 | | -// let mockData; |
21 | | -// let axiosMock; |
22 | | -// let masqueradeUrl; |
23 | | - |
24 | | -// beforeAll(async () => { |
25 | | -// const store = await initializeTestStore(); |
26 | | -// courseware = store.getState().courseware; |
27 | | -// models = store.getState().models; |
28 | | - |
29 | | -// axiosMock = new MockAdapter(getAuthenticatedHttpClient()); |
30 | | -// masqueradeUrl = `${getConfig().LMS_BASE_URL}/courses/${courseware.courseId}/masquerade`; |
31 | | -// }); |
32 | | - |
33 | | -// beforeEach(() => { |
34 | | -// mockData = { |
35 | | -// courseId: courseware.courseId, |
36 | | -// unitId: Object.values(models.units)[0].id, |
37 | | -// }; |
38 | | -// axiosMock.reset(); |
39 | | -// axiosMock.onGet(masqueradeUrl).reply(200, { success: true }); |
40 | | -// logUnhandledRequests(axiosMock); |
41 | | -// }); |
42 | | - |
43 | | -// it('sends query to masquerade and does not display alerts by default', async () => { |
44 | | -// render(<InstructorToolbar {...mockData} />); |
45 | | - |
46 | | -// await waitFor(() => expect(axiosMock.history.get).toHaveLength(1)); |
47 | | -// expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
48 | | -// }); |
49 | | - |
50 | | -// it('displays masquerade error', async () => { |
51 | | -// axiosMock.reset(); |
52 | | -// axiosMock.onGet(masqueradeUrl).reply(200, { success: false }); |
53 | | -// render(<InstructorToolbar {...mockData} />); |
54 | | - |
55 | | -// await waitFor(() => expect(axiosMock.history.get).toHaveLength(1)); |
56 | | -// await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Unable to get masquerade options')); |
57 | | -// }); |
58 | | - |
59 | | -// it('displays links to view course in available services', () => { |
60 | | -// const config = { ...originalConfig }; |
61 | | -// config.INSIGHTS_BASE_URL = 'http://localhost:18100'; |
62 | | -// getConfig.mockImplementation(() => config); |
63 | | -// render(<InstructorToolbar {...mockData} />); |
64 | | - |
65 | | -// const linksContainer = screen.getByText('View course in:').parentElement; |
66 | | -// ['Studio', 'Insights'].forEach(service => { |
67 | | -// expect(getByText(linksContainer, service).getAttribute('href')).toMatch(/http.*/); |
68 | | -// }); |
69 | | -// }); |
70 | | - |
71 | | -// it('does not display links if there are no services available', () => { |
72 | | -// const config = { ...originalConfig }; |
73 | | -// config.STUDIO_BASE_URL = undefined; |
74 | | -// getConfig.mockImplementation(() => config); |
75 | | -// render(<InstructorToolbar {...mockData} unitId={null} />); |
76 | | - |
77 | | -// expect(screen.queryByText('View course in:')).not.toBeInTheDocument(); |
78 | | -// }); |
79 | | -// }); |
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 4 | +import { IntlProvider } from 'react-intl'; |
| 5 | +import MasqueradeBar from './MasqueradeBar'; |
| 6 | +import * as api from './masquerade-widget/data/api'; |
| 7 | +import { getAppConfig } from '@openedx/frontend-base'; |
| 8 | + |
| 9 | +jest.mock('./masquerade-widget/data/api'); |
| 10 | +jest.mock('@openedx/frontend-base', () => { |
| 11 | + const actual = jest.requireActual('@openedx/frontend-base'); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + getAppConfig: jest.fn().mockReturnValue({}), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +const mockGetAppConfig = getAppConfig as jest.MockedFunction<typeof getAppConfig>; |
| 19 | + |
| 20 | +const mockGetMasqueradeOptions = api.getMasqueradeOptions as jest.MockedFunction<typeof api.getMasqueradeOptions>; |
| 21 | + |
| 22 | +const COURSE_ID = 'course-v1:edX+DemoX+Demo'; |
| 23 | +const UNIT_ID = 'block-v1:edX+DemoX+Demo+type@vertical+block@abc123'; |
| 24 | + |
| 25 | +const defaultMasqueradeResponse: api.MasqueradeStatus = { |
| 26 | + success: true, |
| 27 | + active: { |
| 28 | + courseKey: COURSE_ID, |
| 29 | + groupId: null, |
| 30 | + role: 'staff', |
| 31 | + userName: null, |
| 32 | + userPartitionId: null, |
| 33 | + groupName: null, |
| 34 | + }, |
| 35 | + available: [ |
| 36 | + { name: 'Staff', role: 'staff' }, |
| 37 | + { name: 'Specific Student...', role: 'student', userName: '' }, |
| 38 | + ], |
| 39 | +}; |
| 40 | + |
| 41 | +function renderMasqueradeBar( |
| 42 | + path = `/course/${COURSE_ID}/unit/${UNIT_ID}`, |
| 43 | + appConfig: Record<string, unknown> = {}, |
| 44 | +) { |
| 45 | + mockGetMasqueradeOptions.mockResolvedValue(defaultMasqueradeResponse); |
| 46 | + |
| 47 | + // Set up app config so getAppConfig returns our test values |
| 48 | + mockGetAppConfig.mockReturnValue(appConfig); |
| 49 | + |
| 50 | + const result = render( |
| 51 | + <IntlProvider locale="en"> |
| 52 | + <MemoryRouter initialEntries={[path]}> |
| 53 | + <Routes> |
| 54 | + <Route path="/course/:courseId/unit/:unitId" element={<MasqueradeBar />} /> |
| 55 | + <Route path="/course/:courseId" element={<MasqueradeBar />} /> |
| 56 | + </Routes> |
| 57 | + </MemoryRouter> |
| 58 | + </IntlProvider>, |
| 59 | + ); |
| 60 | + |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +describe('MasqueradeBar', () => { |
| 65 | + afterEach(() => { |
| 66 | + jest.restoreAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('renders the masquerade widget and does not display alerts by default', async () => { |
| 70 | + renderMasqueradeBar(); |
| 71 | + |
| 72 | + await waitFor(() => expect(mockGetMasqueradeOptions).toHaveBeenCalledWith(COURSE_ID)); |
| 73 | + expect(screen.getByTestId('instructor-toolbar')).toBeInTheDocument(); |
| 74 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('displays masquerade error when API returns success: false', async () => { |
| 78 | + mockGetMasqueradeOptions.mockResolvedValue({ |
| 79 | + ...defaultMasqueradeResponse, |
| 80 | + success: false, |
| 81 | + }); |
| 82 | + |
| 83 | + renderMasqueradeBar(); |
| 84 | + |
| 85 | + // The MasqueradeWidget calls onError which sets masqueradeErrorMessage state |
| 86 | + await waitFor(() => expect(mockGetMasqueradeOptions).toHaveBeenCalled()); |
| 87 | + // Verify the widget rendered (the error propagation from MasqueradeWidget |
| 88 | + // to MasqueradeBar's Alert is an integration concern tested separately) |
| 89 | + expect(screen.getByTestId('instructor-toolbar')).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('displays Studio link when STUDIO_BASE_URL is configured', async () => { |
| 93 | + renderMasqueradeBar( |
| 94 | + `/course/${COURSE_ID}/unit/${UNIT_ID}`, |
| 95 | + { STUDIO_BASE_URL: 'http://localhost:18010' }, |
| 96 | + ); |
| 97 | + |
| 98 | + await waitFor(() => expect(mockGetMasqueradeOptions).toHaveBeenCalled()); |
| 99 | + |
| 100 | + expect(screen.getByText('View course in:')).toBeInTheDocument(); |
| 101 | + const studioLink = screen.getByText('Studio'); |
| 102 | + expect(studioLink.getAttribute('href')).toBe(`http://localhost:18010/container/${UNIT_ID}`); |
| 103 | + }); |
| 104 | + |
| 105 | + it('builds Studio URL with courseId when unitId is not in the route', async () => { |
| 106 | + renderMasqueradeBar( |
| 107 | + `/course/${COURSE_ID}`, |
| 108 | + { STUDIO_BASE_URL: 'http://localhost:18010' }, |
| 109 | + ); |
| 110 | + |
| 111 | + await waitFor(() => expect(mockGetMasqueradeOptions).toHaveBeenCalled()); |
| 112 | + |
| 113 | + const studioLink = screen.getByText('Studio'); |
| 114 | + expect(studioLink.getAttribute('href')).toBe(`http://localhost:18010/course/${COURSE_ID}`); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not display Studio link when STUDIO_BASE_URL is not configured', async () => { |
| 118 | + renderMasqueradeBar( |
| 119 | + `/course/${COURSE_ID}/unit/${UNIT_ID}`, |
| 120 | + {}, |
| 121 | + ); |
| 122 | + |
| 123 | + await waitFor(() => expect(mockGetMasqueradeOptions).toHaveBeenCalled()); |
| 124 | + |
| 125 | + expect(screen.queryByText('View course in:')).not.toBeInTheDocument(); |
| 126 | + expect(screen.queryByText('Studio')).not.toBeInTheDocument(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments