|
1 | | -import React from 'react'; |
2 | | -import { shallow } from '@edx/react-unit-test-utils'; |
3 | | - |
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
4 | 3 | import { useDispatch } from 'react-redux'; |
5 | | - |
6 | 4 | import * as hooks from './hooks'; |
7 | 5 | import { StartGradingButton } from '.'; |
| 6 | +import messages from '../messages'; |
| 7 | + |
| 8 | +jest.unmock('@openedx/paragon'); |
| 9 | +jest.unmock('react'); |
| 10 | +jest.unmock('@edx/frontend-platform/i18n'); |
8 | 11 |
|
9 | | -jest.mock('../OverrideGradeConfirmModal', () => 'OverrideGradeConfirmModal'); |
10 | | -jest.mock('../StopGradingConfirmModal', () => 'StopGradingConfirmModal'); |
| 12 | +jest.mock('react-redux', () => ({ |
| 13 | + useDispatch: jest.fn(), |
| 14 | +})); |
11 | 15 |
|
12 | 16 | jest.mock('./hooks', () => ({ |
13 | 17 | buttonHooks: jest.fn(), |
14 | 18 | })); |
15 | 19 |
|
16 | | -let el; |
17 | | -describe('StartGradingButton component', () => { |
18 | | - describe('component', () => { |
19 | | - const dispatch = useDispatch(); |
20 | | - const buttonHooks = { |
| 20 | +describe('StartGradingButton', () => { |
| 21 | + const mockDispatch = jest.fn(); |
| 22 | + |
| 23 | + const renderWithIntl = (component) => render( |
| 24 | + <IntlProvider locale="en" messages={{}}> |
| 25 | + {component} |
| 26 | + </IntlProvider>, |
| 27 | + ); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + useDispatch.mockReturnValue(mockDispatch); |
| 32 | + }); |
| 33 | + |
| 34 | + it('does not render when hide is true', () => { |
| 35 | + hooks.buttonHooks.mockReturnValue({ |
| 36 | + hide: true, |
| 37 | + buttonArgs: {}, |
| 38 | + overrideGradeArgs: {}, |
| 39 | + stopGradingArgs: {}, |
| 40 | + }); |
| 41 | + const { container } = renderWithIntl(<StartGradingButton />); |
| 42 | + expect(container.firstChild).toBeNull(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders primary button when visible', () => { |
| 46 | + hooks.buttonHooks.mockReturnValue({ |
| 47 | + hide: false, |
| 48 | + buttonArgs: { children: 'Start Grading' }, |
| 49 | + overrideGradeArgs: { |
| 50 | + isOpen: false, |
| 51 | + onCancel: jest.fn(), |
| 52 | + onConfirm: jest.fn(), |
| 53 | + }, |
| 54 | + stopGradingArgs: { |
| 55 | + isOpen: false, |
| 56 | + isOverride: false, |
| 57 | + onCancel: jest.fn(), |
| 58 | + onConfirm: jest.fn(), |
| 59 | + }, |
| 60 | + }); |
| 61 | + renderWithIntl(<StartGradingButton />); |
| 62 | + const button = screen.getByRole('button', { name: 'Start Grading' }); |
| 63 | + expect(button).toBeInTheDocument(); |
| 64 | + expect(button).toHaveClass('btn-primary'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('renders override grade modal components', () => { |
| 68 | + hooks.buttonHooks.mockReturnValue({ |
| 69 | + hide: false, |
| 70 | + buttonArgs: { children: 'Start Grading' }, |
| 71 | + overrideGradeArgs: { |
| 72 | + isOpen: true, |
| 73 | + onCancel: jest.fn(), |
| 74 | + onConfirm: jest.fn(), |
| 75 | + }, |
| 76 | + stopGradingArgs: { |
| 77 | + isOpen: false, |
| 78 | + isOverride: false, |
| 79 | + onCancel: jest.fn(), |
| 80 | + onConfirm: jest.fn(), |
| 81 | + }, |
| 82 | + }); |
| 83 | + renderWithIntl(<StartGradingButton />); |
| 84 | + const overrideModalTitle = screen.getByText(messages.overrideConfirmTitle.defaultMessage); |
| 85 | + expect(overrideModalTitle).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders stop grading modal components', () => { |
| 89 | + hooks.buttonHooks.mockReturnValue({ |
| 90 | + hide: false, |
| 91 | + buttonArgs: { children: 'Start Grading' }, |
| 92 | + overrideGradeArgs: { |
| 93 | + isOpen: false, |
| 94 | + onCancel: jest.fn(), |
| 95 | + onConfirm: jest.fn(), |
| 96 | + }, |
| 97 | + stopGradingArgs: { |
| 98 | + isOpen: true, |
| 99 | + isOverride: false, |
| 100 | + onCancel: jest.fn(), |
| 101 | + onConfirm: jest.fn(), |
| 102 | + }, |
| 103 | + }); |
| 104 | + renderWithIntl(<StartGradingButton />); |
| 105 | + const stopGradingModalTitle = screen.getByText(messages.confirmStopGradingTitle.defaultMessage); |
| 106 | + expect(stopGradingModalTitle).toBeInTheDocument(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('calls buttonHooks with dispatch and intl', () => { |
| 110 | + hooks.buttonHooks.mockReturnValue({ |
21 | 111 | hide: false, |
22 | | - buttonArgs: { props: 'hooks.buttonArgs' }, |
23 | | - overrideGradeArgs: { props: 'hooks.overrideGradeArgs' }, |
24 | | - stopGradingArgs: { props: 'hooks.stopGradingArgs' }, |
25 | | - }; |
26 | | - describe('behavior', () => { |
27 | | - it('initializes buttonHooks with dispatch and intl fields', () => { |
28 | | - hooks.buttonHooks.mockReturnValueOnce(buttonHooks); |
29 | | - el = shallow(<StartGradingButton />); |
30 | | - const expected = { dispatch, intl: { formatMessage: expect.any(Function), formatDate: expect.any(Function) } }; |
31 | | - expect(hooks.buttonHooks).toHaveBeenCalledWith(expected); |
32 | | - }); |
| 112 | + buttonArgs: { children: 'Start Grading' }, |
| 113 | + overrideGradeArgs: { |
| 114 | + isOpen: false, |
| 115 | + onCancel: jest.fn(), |
| 116 | + onConfirm: jest.fn(), |
| 117 | + }, |
| 118 | + stopGradingArgs: { |
| 119 | + isOpen: false, |
| 120 | + isOverride: false, |
| 121 | + onCancel: jest.fn(), |
| 122 | + onConfirm: jest.fn(), |
| 123 | + }, |
33 | 124 | }); |
34 | | - describe('snapshots', () => { |
35 | | - test('hide: renders empty component if hook.hide is true', () => { |
36 | | - hooks.buttonHooks.mockReturnValueOnce({ ...buttonHooks, hide: true }); |
37 | | - el = shallow(<StartGradingButton />); |
38 | | - expect(el.snapshot).toMatchSnapshot(); |
39 | | - expect(el.isEmptyRender()).toEqual(true); |
40 | | - }); |
41 | | - test('smoke test: forwards props to components from hooks', () => { |
42 | | - hooks.buttonHooks.mockReturnValueOnce(buttonHooks); |
43 | | - el = shallow(<StartGradingButton />); |
44 | | - expect(el.snapshot).toMatchSnapshot(); |
45 | | - expect(el.isEmptyRender()).toEqual(false); |
46 | | - }); |
| 125 | + renderWithIntl(<StartGradingButton />); |
| 126 | + expect(hooks.buttonHooks).toHaveBeenCalledWith({ |
| 127 | + dispatch: mockDispatch, |
| 128 | + intl: expect.any(Object), |
47 | 129 | }); |
48 | 130 | }); |
49 | 131 | }); |
0 commit comments