|
| 1 | +import * as reactRedux from 'react-redux'; |
| 2 | +import { getConfig } from '@edx/frontend-platform'; |
| 3 | + |
| 4 | +import { studioHomeMock } from '../__mocks__'; |
| 5 | +import messages from '../messages'; |
| 6 | +import { trimSlashes } from './utils'; |
| 7 | +import { |
| 8 | + fireEvent, |
| 9 | + initializeMocks, |
| 10 | + render, |
| 11 | + screen, |
| 12 | +} from '../../testUtils'; |
| 13 | +import CardItem from '.'; |
| 14 | + |
| 15 | +jest.spyOn(reactRedux, 'useSelector').mockImplementation(() => studioHomeMock); |
| 16 | + |
| 17 | +describe('<CardItem />', () => { |
| 18 | + beforeEach(() => { |
| 19 | + initializeMocks(); |
| 20 | + }); |
| 21 | + it('should render course details for non-library course', () => { |
| 22 | + const props = studioHomeMock.archivedCourses[0]; |
| 23 | + render(<CardItem {...props} />); |
| 24 | + expect(screen.getByText(`${props.org} / ${props.number} / ${props.run}`)).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should render correct links for non-library course', () => { |
| 28 | + const props = studioHomeMock.archivedCourses[0]; |
| 29 | + render(<CardItem {...props} />); |
| 30 | + const courseTitleLink = screen.getByText(props.displayName); |
| 31 | + expect(courseTitleLink).toHaveAttribute('href', `${getConfig().STUDIO_BASE_URL}${props.url}`); |
| 32 | + const btnReRunCourse = screen.getByText(messages.btnReRunText.defaultMessage); |
| 33 | + expect(btnReRunCourse).toHaveAttribute('href', trimSlashes(props.rerunLink)); |
| 34 | + const viewLiveLink = screen.getByText(messages.viewLiveBtnText.defaultMessage); |
| 35 | + expect(viewLiveLink).toHaveAttribute('href', props.lmsLink); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should render correct links for non-library course pagination', () => { |
| 39 | + const props = studioHomeMock.archivedCourses[0]; |
| 40 | + render(<CardItem {...props} isPaginated />); |
| 41 | + const courseTitleLink = screen.getByText(props.displayName); |
| 42 | + expect(courseTitleLink).toHaveAttribute('href', `${getConfig().STUDIO_BASE_URL}${props.url}`); |
| 43 | + const dropDownMenu = screen.getByTestId('toggle-dropdown'); |
| 44 | + fireEvent.click(dropDownMenu); |
| 45 | + const btnReRunCourse = screen.getByText(messages.btnReRunText.defaultMessage); |
| 46 | + expect(btnReRunCourse).toHaveAttribute('href', trimSlashes(props.rerunLink)); |
| 47 | + const viewLiveLink = screen.getByText(messages.viewLiveBtnText.defaultMessage); |
| 48 | + expect(viewLiveLink).toHaveAttribute('href', props.lmsLink); |
| 49 | + }); |
| 50 | + it('should render course details for library course', () => { |
| 51 | + const props = { ...studioHomeMock.archivedCourses[0], isLibraries: true }; |
| 52 | + render(<CardItem {...props} />); |
| 53 | + const courseTitleLink = screen.getByText(props.displayName); |
| 54 | + expect(courseTitleLink).toHaveAttribute('href', `${getConfig().STUDIO_BASE_URL}${props.url}`); |
| 55 | + expect(screen.getByText(`${props.org} / ${props.number}`)).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + it('should hide rerun button if disallowed', () => { |
| 58 | + const props = studioHomeMock.archivedCourses[0]; |
| 59 | + // Update our mocked redux data: |
| 60 | + jest.spyOn(reactRedux, 'useSelector').mockImplementation(() => ( |
| 61 | + { ...studioHomeMock, allowCourseReruns: false } |
| 62 | + )); |
| 63 | + const { queryByText } = render(<CardItem {...props} />); |
| 64 | + expect(queryByText(messages.btnReRunText.defaultMessage)).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + it('should be read only course if old mongo course', () => { |
| 67 | + const props = studioHomeMock.courses[1]; |
| 68 | + render(<CardItem {...props} />); |
| 69 | + expect(screen.queryByText(props.displayName)).not.toHaveAttribute('href'); |
| 70 | + expect(screen.queryByText(messages.btnReRunText.defaultMessage)).not.toBeInTheDocument(); |
| 71 | + expect(screen.queryByText(messages.viewLiveBtnText.defaultMessage)).not.toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should render course key if displayname is empty', () => { |
| 75 | + const props = studioHomeMock.courses[1]; |
| 76 | + const courseKeyTest = 'course-key'; |
| 77 | + render( |
| 78 | + <CardItem |
| 79 | + {...props} |
| 80 | + displayName="" |
| 81 | + courseKey={courseKeyTest} |
| 82 | + lmsLink="lmsLink" |
| 83 | + rerunLink="returnLink" |
| 84 | + url="url" |
| 85 | + />, |
| 86 | + ); |
| 87 | + expect(screen.getByText(courseKeyTest)).toBeInTheDocument(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments