forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificateSignatories.test.jsx
More file actions
114 lines (95 loc) · 3.3 KB
/
CertificateSignatories.test.jsx
File metadata and controls
114 lines (95 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { initializeMockApp } from '@edx/frontend-platform';
import initializeStore from '../../store';
import { MODE_STATES } from '../data/constants';
import { signatoriesMock } from '../__mocks__';
import commonMessages from '../messages';
import messages from './messages';
import useEditSignatory from './hooks/useEditSignatory';
import useCreateSignatory from './hooks/useCreateSignatory';
import CertificateSignatories from './CertificateSignatories';
let store;
const mockArrayHelpers = {
push: jest.fn(),
remove: jest.fn(),
};
jest.mock('./hooks/useEditSignatory');
jest.mock('./hooks/useCreateSignatory');
const renderComponent = (props) => render(
<Provider store={store}>
<IntlProvider locale="en">
<CertificateSignatories {...props} />
</IntlProvider>,
</Provider>,
);
const defaultProps = {
signatories: signatoriesMock,
handleChange: jest.fn(),
handleBlur: jest.fn(),
setFieldValue: jest.fn(),
arrayHelpers: mockArrayHelpers,
isForm: true,
resetForm: jest.fn(),
editModes: {},
setEditModes: jest.fn(),
};
const initialState = {
certificates: {
certificatesData: {
certificates: [],
hasCertificateModes: true,
},
componentMode: MODE_STATES.create,
},
};
describe('CertificateSignatories', () => {
beforeEach(() => {
initializeMockApp({
authenticatedUser: {
userId: 3,
username: 'abc123',
administrator: true,
roles: [],
},
});
store = initializeStore(initialState);
useEditSignatory.mockReturnValue({
toggleEditSignatory: jest.fn(),
handleDeleteSignatory: jest.fn(),
handleCancelUpdateSignatory: jest.fn(),
});
useCreateSignatory.mockReturnValue({
handleAddSignatory: jest.fn(),
});
});
afterEach(() => jest.clearAllMocks());
it('renders signatory components for each signatory', () => {
const { getByText } = renderComponent({ ...defaultProps, isForm: false });
signatoriesMock.forEach(signatory => {
expect(getByText(signatory.name)).toBeInTheDocument();
expect(getByText(signatory.title)).toBeInTheDocument();
expect(getByText(signatory.organization)).toBeInTheDocument();
});
});
it('adds a new signatory when add button is clicked', async () => {
const user = userEvent.setup();
const { getByText } = renderComponent({ ...defaultProps, isForm: true });
await user.click(getByText(messages.addSignatoryButton.defaultMessage));
expect(useCreateSignatory().handleAddSignatory).toHaveBeenCalled();
});
it.skip('calls remove for the correct signatory when delete icon is clicked', async () => {
const user = userEvent.setup();
const { getAllByRole } = renderComponent(defaultProps);
const deleteIcons = getAllByRole('button', { name: commonMessages.deleteTooltip.defaultMessage });
expect(deleteIcons.length).toBe(signatoriesMock.length);
await user.click(deleteIcons[0]);
// FIXME: this isn't called because the whole 'useEditSignatory' hook
// which calls it is mocked out.
await waitFor(() => {
expect(mockArrayHelpers.remove).toHaveBeenCalledWith(0);
});
});
});