|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import { CourseAuthoringProvider } from '@src/CourseAuthoringContext'; |
| 3 | +import { |
| 4 | + initializeMocks, |
| 5 | + render, |
| 6 | + within, |
| 7 | +} from '../testUtils'; |
| 8 | +import { getContentStoreApiUrl } from './data/api'; |
| 9 | +import { groupConfigurationResponseMock } from './__mocks__'; |
| 10 | +import messages from './messages'; |
| 11 | +import experimentMessages from './experiment-configurations-section/messages'; |
| 12 | +import contentGroupsMessages from './content-groups-section/messages'; |
| 13 | +import GroupConfigurations from '.'; |
| 14 | + |
| 15 | +let axiosMock; |
| 16 | +const courseId = 'course-v1:org+101+101'; |
| 17 | +const enrollmentTrackGroups = groupConfigurationResponseMock.allGroupConfigurations[0]; |
| 18 | +const contentGroups = groupConfigurationResponseMock.allGroupConfigurations[1]; |
| 19 | +const teamGroups = groupConfigurationResponseMock.allGroupConfigurations[2]; |
| 20 | + |
| 21 | +const renderComponent = () => |
| 22 | + render( |
| 23 | + <CourseAuthoringProvider courseId={courseId}> |
| 24 | + <GroupConfigurations /> |
| 25 | + </CourseAuthoringProvider>, |
| 26 | + ); |
| 27 | + |
| 28 | +describe('<GroupConfigurations />', () => { |
| 29 | + beforeEach(async () => { |
| 30 | + const mocks = initializeMocks(); |
| 31 | + axiosMock = mocks.axiosMock; |
| 32 | + axiosMock |
| 33 | + .onGet(getContentStoreApiUrl(courseId)) |
| 34 | + .reply(200, groupConfigurationResponseMock); |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders component correctly', async () => { |
| 38 | + renderComponent(); |
| 39 | + |
| 40 | + const mainContent = await screen.findByTestId('group-configurations-main-content-wrapper'); |
| 41 | + const groupConfigurationsTitle = screen.getAllByText(messages.headingTitle.defaultMessage)[0]; |
| 42 | + |
| 43 | + expect(groupConfigurationsTitle).toBeInTheDocument(); |
| 44 | + expect( |
| 45 | + screen.getByText(messages.headingSubtitle.defaultMessage), |
| 46 | + ).toBeInTheDocument(); |
| 47 | + expect( |
| 48 | + within(mainContent).getByText(contentGroupsMessages.addNewGroup.defaultMessage), |
| 49 | + ).toBeInTheDocument(); |
| 50 | + expect( |
| 51 | + within(mainContent).getByText(experimentMessages.addNewGroup.defaultMessage), |
| 52 | + ).toBeInTheDocument(); |
| 53 | + expect( |
| 54 | + within(mainContent).getByText(experimentMessages.title.defaultMessage), |
| 55 | + ).toBeInTheDocument(); |
| 56 | + expect(screen.getByText(contentGroups.name)).toBeInTheDocument(); |
| 57 | + expect(screen.getByText(enrollmentTrackGroups.name)).toBeInTheDocument(); |
| 58 | + expect(screen.getByText(teamGroups.name)).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not render an empty section for enrollment track groups if it is empty', async () => { |
| 62 | + const shouldNotShowEnrollmentTrackResponse = { |
| 63 | + ...groupConfigurationResponseMock, |
| 64 | + shouldShowEnrollmentTrack: false, |
| 65 | + }; |
| 66 | + axiosMock |
| 67 | + .onGet(getContentStoreApiUrl(courseId)) |
| 68 | + .reply(200, shouldNotShowEnrollmentTrackResponse); |
| 69 | + |
| 70 | + renderComponent(); |
| 71 | + |
| 72 | + await screen.findByTestId('group-configurations-main-content-wrapper'); |
| 73 | + expect( |
| 74 | + screen.queryByTestId('group-configurations-empty-placeholder'), |
| 75 | + ).not.toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not show a connection error when the request fails with a non-403 status', async () => { |
| 79 | + axiosMock |
| 80 | + .onGet(getContentStoreApiUrl(courseId)) |
| 81 | + .reply(404); |
| 82 | + |
| 83 | + renderComponent(); |
| 84 | + |
| 85 | + await screen.findByTestId('group-configurations-main-content-wrapper'); |
| 86 | + expect(screen.queryByTestId('connectionErrorAlert')).not.toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('displays a connection error alert when API responds with 403', async () => { |
| 90 | + axiosMock |
| 91 | + .onGet(getContentStoreApiUrl(courseId)) |
| 92 | + .reply(403); |
| 93 | + |
| 94 | + renderComponent(); |
| 95 | + |
| 96 | + expect(await screen.findByTestId('connectionErrorAlert')).toBeInTheDocument(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments