|
| 1 | +import React from 'react'; |
| 2 | +import { history } from '@edx/frontend-platform'; |
| 3 | +import { Route } from 'react-router'; |
| 4 | +import { initializeTestStore, render, screen } from '../setupTest'; |
| 5 | +import CourseAccessErrorPage from './CourseAccessErrorPage'; |
| 6 | + |
| 7 | +const mockDispatch = jest.fn(); |
| 8 | +let mockCourseStatus; |
| 9 | +jest.mock('react-redux', () => ({ |
| 10 | + ...jest.requireActual('react-redux'), |
| 11 | + useDispatch: () => mockDispatch, |
| 12 | + useSelector: () => ({ courseStatus: mockCourseStatus }), |
| 13 | +})); |
| 14 | +jest.mock('./PageLoading', () => () => <div data-testid="page-loading" />); |
| 15 | + |
| 16 | +describe('CourseAccessErrorPage', () => { |
| 17 | + let courseId; |
| 18 | + let accessDeniedUrl; |
| 19 | + beforeEach(async () => { |
| 20 | + const store = await initializeTestStore({ excludeFetchSequence: true }); |
| 21 | + courseId = store.getState().courseware.courseId; |
| 22 | + accessDeniedUrl = `/course/${courseId}/access-denied`; |
| 23 | + history.push(accessDeniedUrl); |
| 24 | + }); |
| 25 | + |
| 26 | + it('Displays loading in start on page rendering', () => { |
| 27 | + mockCourseStatus = 'loading'; |
| 28 | + render( |
| 29 | + <Route path="/course/:courseId/access-denied"> |
| 30 | + <CourseAccessErrorPage /> |
| 31 | + </Route>, |
| 32 | + ); |
| 33 | + expect(screen.getByTestId('page-loading')).toBeInTheDocument(); |
| 34 | + expect(history.location.pathname).toBe(accessDeniedUrl); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Redirect user to homepage if user has access', () => { |
| 38 | + mockCourseStatus = 'loaded'; |
| 39 | + render( |
| 40 | + <Route path="/course/:courseId/access-denied"> |
| 41 | + <CourseAccessErrorPage /> |
| 42 | + </Route>, |
| 43 | + ); |
| 44 | + expect(history.location.pathname).toBe('/redirect/home/course-v1:edX+DemoX+Demo_Course'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('For access denied it should render access denied page', () => { |
| 48 | + mockCourseStatus = 'denied'; |
| 49 | + |
| 50 | + render( |
| 51 | + <Route path="/course/:courseId/access-denied"> |
| 52 | + <CourseAccessErrorPage /> |
| 53 | + </Route>, |
| 54 | + ); |
| 55 | + expect(screen.getByTestId('access-denied-main')).toBeInTheDocument(); |
| 56 | + expect(history.location.pathname).toBe(accessDeniedUrl); |
| 57 | + }); |
| 58 | +}); |
0 commit comments