|
1 | | -// eslint-disable-next-line no-redeclare |
2 | | -/* global jest,test,describe,expect */ |
3 | | -import { Button } from '@edx/paragon'; |
4 | | -import BlockBrowserContainer from 'BlockBrowser/components/BlockBrowser/BlockBrowserContainer'; |
5 | 1 | import { Provider } from 'react-redux'; |
6 | | -import { shallow } from 'enzyme'; |
7 | 2 | import React from 'react'; |
8 | | -import renderer from 'react-test-renderer'; |
| 3 | +import { |
| 4 | + render, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | +} from '@testing-library/react'; |
| 8 | +import userEvent from '@testing-library/user-event'; |
9 | 9 | import store from '../../data/store'; |
10 | | - |
11 | 10 | import Main from './Main'; |
12 | 11 |
|
| 12 | +jest.mock('BlockBrowser/components/BlockBrowser/BlockBrowserContainer', () => { |
| 13 | + function MockedBlockBrowserContainer() { |
| 14 | + return ( |
| 15 | + <div data-testid="mocked-block-browser-container" className="block-browser"> |
| 16 | + Mocked BlockBrowserContainer |
| 17 | + </div> |
| 18 | + ); |
| 19 | + } |
| 20 | + return MockedBlockBrowserContainer; |
| 21 | +}); |
| 22 | + |
13 | 23 | describe('ProblemBrowser Main component', () => { |
14 | 24 | const courseId = 'testcourse'; |
15 | 25 | const problemResponsesEndpoint = '/api/problem_responses/'; |
16 | 26 | const taskStatusEndpoint = '/api/task_status/'; |
17 | 27 | const excludedBlockTypes = []; |
| 28 | + const reportDownloadEndpoint = '/api/report_download'; |
| 29 | + let fetchCourseBlocksMock; |
| 30 | + let createProblemResponsesReportTaskMock; |
| 31 | + let onSelectBlockMock; |
18 | 32 |
|
19 | | - test('render with basic parameters', () => { |
20 | | - const component = renderer.create( |
21 | | - <Provider store={store}> |
22 | | - <Main |
23 | | - courseId={courseId} |
24 | | - createProblemResponsesReportTask={jest.fn()} |
25 | | - excludeBlockTypes={excludedBlockTypes} |
26 | | - fetchCourseBlocks={jest.fn()} |
27 | | - problemResponsesEndpoint={problemResponsesEndpoint} |
28 | | - onSelectBlock={jest.fn()} |
29 | | - selectedBlock={null} |
30 | | - taskStatusEndpoint={taskStatusEndpoint} |
31 | | - /> |
32 | | - </Provider>, |
33 | | - ); |
34 | | - const tree = component.toJSON(); |
35 | | - expect(tree).toMatchSnapshot(); |
36 | | - }); |
37 | | - |
38 | | - test('render with selected block', () => { |
39 | | - const component = renderer.create( |
40 | | - <Provider store={store}> |
41 | | - <Main |
42 | | - courseId={courseId} |
43 | | - createProblemResponsesReportTask={jest.fn()} |
44 | | - excludeBlockTypes={excludedBlockTypes} |
45 | | - fetchCourseBlocks={jest.fn()} |
46 | | - problemResponsesEndpoint={problemResponsesEndpoint} |
47 | | - onSelectBlock={jest.fn()} |
48 | | - selectedBlock="some-selected-block" |
49 | | - taskStatusEndpoint={taskStatusEndpoint} |
50 | | - /> |
51 | | - </Provider>, |
52 | | - ); |
53 | | - const tree = component.toJSON(); |
54 | | - expect(tree).toMatchSnapshot(); |
| 33 | + beforeEach(() => { |
| 34 | + fetchCourseBlocksMock = jest.fn(); |
| 35 | + createProblemResponsesReportTaskMock = jest.fn(); |
| 36 | + onSelectBlockMock = jest.fn(); |
55 | 37 | }); |
56 | 38 |
|
57 | | - test('fetch course block on toggling dropdown', () => { |
58 | | - const fetchCourseBlocksMock = jest.fn(); |
59 | | - const component = renderer.create( |
| 39 | + const renderMainComponent = (props = {}) => ( |
| 40 | + render( |
60 | 41 | <Provider store={store}> |
61 | 42 | <Main |
62 | 43 | courseId={courseId} |
63 | | - createProblemResponsesReportTask={jest.fn()} |
| 44 | + createProblemResponsesReportTask={createProblemResponsesReportTaskMock} |
64 | 45 | excludeBlockTypes={excludedBlockTypes} |
65 | 46 | fetchCourseBlocks={fetchCourseBlocksMock} |
66 | 47 | problemResponsesEndpoint={problemResponsesEndpoint} |
67 | | - onSelectBlock={jest.fn()} |
68 | | - selectedBlock="some-selected-block" |
| 48 | + onSelectBlock={onSelectBlockMock} |
| 49 | + selectedBlock={props.selectedBlock} |
69 | 50 | taskStatusEndpoint={taskStatusEndpoint} |
| 51 | + reportDownloadEndpoint={reportDownloadEndpoint} |
| 52 | + ShowBtnUi="false" |
| 53 | + {...props} |
70 | 54 | /> |
71 | 55 | </Provider>, |
72 | | - ); |
73 | | - // eslint-disable-next-line prefer-destructuring |
74 | | - const instance = component.root.children[0].instance; |
75 | | - instance.handleToggleDropdown(); |
76 | | - expect(fetchCourseBlocksMock.mock.calls.length).toBe(1); |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + describe('Initial rendering', () => { |
| 60 | + test('should match snapshot with basic parameters', () => { |
| 61 | + const { container } = renderMainComponent(); |
| 62 | + expect(container).toMatchSnapshot(); |
| 63 | + }); |
| 64 | + test('should match snapshot with selected block', () => { |
| 65 | + const { container } = renderMainComponent({ selectedBlock: 'some-selected-block' }); |
| 66 | + expect(container).toMatchSnapshot(); |
| 67 | + }); |
77 | 68 | }); |
78 | 69 |
|
79 | | - test('display dropdown on toggling dropdown', () => { |
80 | | - const component = shallow( |
81 | | - <Main |
82 | | - courseId={courseId} |
83 | | - createProblemResponsesReportTask={jest.fn()} |
84 | | - excludeBlockTypes={excludedBlockTypes} |
85 | | - fetchCourseBlocks={jest.fn()} |
86 | | - problemResponsesEndpoint={problemResponsesEndpoint} |
87 | | - onSelectBlock={jest.fn()} |
88 | | - selectedBlock="some-selected-block" |
89 | | - taskStatusEndpoint={taskStatusEndpoint} |
90 | | - />, |
91 | | - ); |
92 | | - expect(component.find(BlockBrowserContainer).length).toBeFalsy(); |
93 | | - component.find(Button).find({ label: 'Select a section or problem' }).simulate('click'); |
94 | | - expect(component.find(BlockBrowserContainer).length).toBeTruthy(); |
| 70 | + describe('Dropdown interactions', () => { |
| 71 | + test('should fetch course blocks when dropdown is toggled', async () => { |
| 72 | + renderMainComponent(); |
| 73 | + await userEvent.click(screen.getByText('Select a section or problem')); |
| 74 | + await waitFor(() => { |
| 75 | + expect(fetchCourseBlocksMock).toHaveBeenCalledTimes(1); |
| 76 | + expect(fetchCourseBlocksMock).toHaveBeenCalledWith(courseId, excludedBlockTypes); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + test('should display dropdown when toggled', async () => { |
| 81 | + renderMainComponent(); |
| 82 | + expect(screen.queryByTestId('mocked-block-browser-container')).toBeNull(); |
| 83 | + await userEvent.click(screen.getByText('Select a section or problem')); |
| 84 | + await waitFor(() => expect( |
| 85 | + screen.getByTestId('mocked-block-browser-container'), |
| 86 | + ).toHaveClass('block-browser')); |
| 87 | + await userEvent.click(screen.getByText('Select a section or problem')); |
| 88 | + await waitFor(() => expect(screen.queryByTestId('mocked-block-browser-container')).toBeNull()); |
| 89 | + }); |
95 | 90 | }); |
96 | 91 | }); |
0 commit comments