|
1 |
| -import { shallow } from '@edx/react-unit-test-utils'; |
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
2 | 3 |
|
3 |
| -import { useExitWithoutSavingAction, useSubmitAssessmentAction } from 'hooks/actions'; |
| 4 | +import { |
| 5 | + useExitWithoutSavingAction, |
| 6 | + useSubmitAssessmentAction, |
| 7 | +} from 'hooks/actions'; |
4 | 8 | import AssessmentActions from './AssessmentActions';
|
5 | 9 |
|
| 10 | +/* eslint-disable react/prop-types */ |
| 11 | + |
| 12 | +jest.unmock('@openedx/paragon'); |
| 13 | +jest.unmock('react'); |
| 14 | +jest.unmock('@edx/frontend-platform/i18n'); |
| 15 | + |
6 | 16 | jest.mock('hooks/actions', () => ({
|
7 | 17 | useExitWithoutSavingAction: jest.fn(),
|
8 | 18 | useSubmitAssessmentAction: jest.fn(),
|
9 | 19 | }));
|
10 | 20 |
|
11 |
| -jest.mock('components/ActionButton', () => 'ActionButton'); |
12 |
| -jest.mock('components/ConfirmDialog', () => 'ConfirmDialog'); |
| 21 | +jest.mock( |
| 22 | + 'components/ActionButton', |
| 23 | + () => ({ |
| 24 | + children, variant, onClick, ...props |
| 25 | + }) => ( |
| 26 | + <button |
| 27 | + type="button" |
| 28 | + data-testid={`action-button-${variant}`} |
| 29 | + onClick={onClick} |
| 30 | + {...props} |
| 31 | + > |
| 32 | + {children} |
| 33 | + </button> |
| 34 | + ), |
| 35 | +); |
| 36 | + |
| 37 | +jest.mock( |
| 38 | + 'components/ConfirmDialog', |
| 39 | + () => ({ title, onConfirm, ...props }) => ( |
| 40 | + <button |
| 41 | + type="button" |
| 42 | + data-testid={`confirm-dialog-${title}`} |
| 43 | + onClick={onConfirm} |
| 44 | + onKeyDown={(e) => e.key === 'Enter' && onConfirm()} |
| 45 | + {...props} |
| 46 | + /> |
| 47 | + ), |
| 48 | +); |
13 | 49 |
|
14 |
| -describe('<AssessmentActions />', () => { |
| 50 | +describe('AssessmentActions', () => { |
15 | 51 | const mockExitWithoutSavingAction = {
|
16 | 52 | action: {
|
17 | 53 | onClick: jest.fn().mockName('useExitWithoutSavingAction.onClick'),
|
| 54 | + children: 'Exit without saving', |
18 | 55 | },
|
19 | 56 | confirmProps: {
|
20 | 57 | onConfirm: jest.fn().mockName('useExitWithoutSavingAction.onConfirm'),
|
| 58 | + title: 'Exit without saving', |
21 | 59 | },
|
22 | 60 | };
|
23 | 61 | const mockSubmitAssessmentAction = {
|
24 | 62 | action: {
|
25 | 63 | onClick: jest.fn().mockName('useSubmitAssessmentAction.onClick'),
|
| 64 | + children: 'Submit assessment', |
26 | 65 | },
|
27 | 66 | confirmProps: {
|
28 | 67 | onConfirm: jest.fn().mockName('useSubmitAssessmentAction.onConfirm'),
|
| 68 | + title: 'Submit assessment', |
29 | 69 | },
|
30 | 70 | };
|
31 | 71 |
|
32 | 72 | beforeEach(() => {
|
| 73 | + jest.clearAllMocks(); |
33 | 74 | useExitWithoutSavingAction.mockReturnValue(mockExitWithoutSavingAction);
|
34 | 75 | useSubmitAssessmentAction.mockReturnValue(mockSubmitAssessmentAction);
|
35 | 76 | });
|
36 | 77 |
|
37 |
| - it('render default', () => { |
38 |
| - const wrapper = shallow(<AssessmentActions />); |
39 |
| - expect(wrapper.snapshot).toMatchSnapshot(); |
| 78 | + it('renders both action buttons and confirm dialogs', () => { |
| 79 | + render(<AssessmentActions />); |
40 | 80 |
|
41 |
| - expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2); |
42 |
| - expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(2); |
| 81 | + expect( |
| 82 | + screen.getByTestId('action-button-outline-primary'), |
| 83 | + ).toBeInTheDocument(); |
| 84 | + expect(screen.getByTestId('action-button-primary')).toBeInTheDocument(); |
| 85 | + expect( |
| 86 | + screen.getByTestId('confirm-dialog-Exit without saving'), |
| 87 | + ).toBeInTheDocument(); |
| 88 | + expect( |
| 89 | + screen.getByTestId('confirm-dialog-Submit assessment'), |
| 90 | + ).toBeInTheDocument(); |
43 | 91 | });
|
44 | 92 |
|
45 |
| - it('render without submitConfirmDialog', () => { |
| 93 | + it('renders without submit confirm dialog when confirmProps is null', () => { |
46 | 94 | useSubmitAssessmentAction.mockReturnValueOnce({
|
47 | 95 | action: mockSubmitAssessmentAction.action,
|
48 | 96 | confirmProps: null,
|
49 | 97 | });
|
50 |
| - const wrapper = shallow(<AssessmentActions />); |
51 |
| - expect(wrapper.snapshot).toMatchSnapshot(); |
| 98 | + render(<AssessmentActions />); |
52 | 99 |
|
53 |
| - expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2); |
54 |
| - expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(1); |
| 100 | + expect( |
| 101 | + screen.getByTestId('action-button-outline-primary'), |
| 102 | + ).toBeInTheDocument(); |
| 103 | + expect(screen.getByTestId('action-button-primary')).toBeInTheDocument(); |
| 104 | + expect( |
| 105 | + screen.getByTestId('confirm-dialog-Exit without saving'), |
| 106 | + ).toBeInTheDocument(); |
| 107 | + expect( |
| 108 | + screen.queryByTestId('confirm-dialog-Submit assessment'), |
| 109 | + ).not.toBeInTheDocument(); |
55 | 110 | });
|
56 | 111 |
|
57 |
| - it('has correct mock value', () => { |
58 |
| - const wrapper = shallow(<AssessmentActions />); |
| 112 | + it('calls the correct handlers when buttons and dialogs are clicked', () => { |
| 113 | + render(<AssessmentActions />); |
59 | 114 |
|
60 |
| - const exitButton = wrapper.instance.findByType('ActionButton')[0]; |
61 |
| - expect(exitButton.props).toMatchObject(mockExitWithoutSavingAction.action); |
| 115 | + const exitButton = screen.getByTestId('action-button-outline-primary'); |
| 116 | + expect(exitButton).toHaveTextContent('Exit without saving'); |
| 117 | + fireEvent.click(exitButton); |
| 118 | + expect(mockExitWithoutSavingAction.action.onClick).toHaveBeenCalled(); |
62 | 119 |
|
63 |
| - const exitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[0]; |
64 |
| - expect(exitConfirmDialog.props).toMatchObject(mockExitWithoutSavingAction.confirmProps); |
| 120 | + const submitButton = screen.getByTestId('action-button-primary'); |
| 121 | + expect(submitButton).toHaveTextContent('Submit assessment'); |
| 122 | + fireEvent.click(submitButton); |
| 123 | + expect(mockSubmitAssessmentAction.action.onClick).toHaveBeenCalled(); |
65 | 124 |
|
66 |
| - const submitButton = wrapper.instance.findByType('ActionButton')[1]; |
67 |
| - expect(submitButton.props).toMatchObject(mockSubmitAssessmentAction.action); |
| 125 | + const exitDialog = screen.getByTestId('confirm-dialog-Exit without saving'); |
| 126 | + fireEvent.click(exitDialog); |
| 127 | + expect( |
| 128 | + mockExitWithoutSavingAction.confirmProps.onConfirm, |
| 129 | + ).toHaveBeenCalled(); |
68 | 130 |
|
69 |
| - const submitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[1]; |
70 |
| - expect(submitConfirmDialog.props).toMatchObject(mockSubmitAssessmentAction.confirmProps); |
| 131 | + const submitDialog = screen.getByTestId('confirm-dialog-Submit assessment'); |
| 132 | + fireEvent.click(submitDialog); |
| 133 | + expect( |
| 134 | + mockSubmitAssessmentAction.confirmProps.onConfirm, |
| 135 | + ).toHaveBeenCalled(); |
71 | 136 | });
|
72 | 137 | });
|
0 commit comments