|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 4 | +import { IntlProvider } from 'react-intl'; |
| 5 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 6 | +import { setSiteConfig, getSiteConfig } from '@openedx/frontend-base'; |
| 7 | + |
| 8 | +import MasqueradeBar from './MasqueradeBar'; |
| 9 | +import * as api from './data/api'; |
| 10 | + |
| 11 | +jest.mock('./data/api'); |
| 12 | + |
| 13 | +const mockGetMasqueradeOptions = api.getMasqueradeOptions as jest.MockedFunction<typeof api.getMasqueradeOptions>; |
| 14 | + |
| 15 | +const COURSE_ID = 'course-v1:edX+DemoX+Demo'; |
| 16 | +const UNIT_ID = 'block-v1:edX+DemoX+Demo+type@vertical+block@abc123'; |
| 17 | + |
| 18 | +const defaultMasqueradeResponse: api.MasqueradeStatus = { |
| 19 | + success: true, |
| 20 | + active: { |
| 21 | + groupId: null, |
| 22 | + role: 'staff', |
| 23 | + userName: null, |
| 24 | + userPartitionId: null, |
| 25 | + groupName: null, |
| 26 | + }, |
| 27 | + available: [ |
| 28 | + { name: 'Staff', role: 'staff' }, |
| 29 | + { name: 'Specific Student...', role: 'student', userName: '' }, |
| 30 | + ], |
| 31 | +}; |
| 32 | + |
| 33 | +function createTestQueryClient() { |
| 34 | + return new QueryClient({ |
| 35 | + defaultOptions: { |
| 36 | + queries: { retry: false }, |
| 37 | + mutations: { retry: false }, |
| 38 | + }, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function renderMasqueradeBar( |
| 43 | + path = `/course/${COURSE_ID}/unit/${UNIT_ID}`, |
| 44 | + cmsBaseUrl = '', |
| 45 | +) { |
| 46 | + mockGetMasqueradeOptions.mockResolvedValue(defaultMasqueradeResponse); |
| 47 | + setSiteConfig({ ...getSiteConfig(), cmsBaseUrl }); |
| 48 | + |
| 49 | + const queryClient = createTestQueryClient(); |
| 50 | + return render( |
| 51 | + <QueryClientProvider client={queryClient}> |
| 52 | + <IntlProvider locale="en"> |
| 53 | + <MemoryRouter initialEntries={[path]}> |
| 54 | + <Routes> |
| 55 | + <Route path="/course/:courseId/unit/:unitId" element={<MasqueradeBar />} /> |
| 56 | + <Route path="/course/:courseId" element={<MasqueradeBar />} /> |
| 57 | + </Routes> |
| 58 | + </MemoryRouter> |
| 59 | + </IntlProvider> |
| 60 | + </QueryClientProvider>, |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +describe('MasqueradeBar', () => { |
| 65 | + beforeEach(() => { |
| 66 | + jest.resetAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('renders the masquerade widget', async () => { |
| 70 | + renderMasqueradeBar(); |
| 71 | + |
| 72 | + expect(await screen.findByRole('region', { name: /masquerade bar/i })).toBeInTheDocument(); |
| 73 | + expect(screen.getByText('View this course as:')).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('displays Studio link when cmsBaseUrl is configured', async () => { |
| 77 | + renderMasqueradeBar(`/course/${COURSE_ID}/unit/${UNIT_ID}`, 'http://localhost:18010'); |
| 78 | + |
| 79 | + const studioLink = await screen.findByRole('link', { name: 'Studio' }); |
| 80 | + expect(screen.getByText('View course in:')).toBeInTheDocument(); |
| 81 | + expect(studioLink).toHaveAttribute('href', `http://localhost:18010/container/${UNIT_ID}`); |
| 82 | + }); |
| 83 | + |
| 84 | + it('builds Studio URL with courseId when unitId is not in the route', async () => { |
| 85 | + renderMasqueradeBar(`/course/${COURSE_ID}`, 'http://localhost:18010'); |
| 86 | + |
| 87 | + const studioLink = await screen.findByRole('link', { name: 'Studio' }); |
| 88 | + expect(studioLink).toHaveAttribute('href', `http://localhost:18010/course/${COURSE_ID}`); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not display Studio link when cmsBaseUrl is not configured', async () => { |
| 92 | + renderMasqueradeBar(`/course/${COURSE_ID}/unit/${UNIT_ID}`, ''); |
| 93 | + |
| 94 | + /* Wait until the bar is visible before asserting the link's absence. */ |
| 95 | + await screen.findByRole('region', { name: /masquerade bar/i }); |
| 96 | + expect(screen.queryByText('View course in:')).not.toBeInTheDocument(); |
| 97 | + expect(screen.queryByRole('link', { name: 'Studio' })).not.toBeInTheDocument(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments