-
Notifications
You must be signed in to change notification settings - Fork 9
/
AgreementEditRoute.test.js
132 lines (111 loc) · 3.4 KB
/
AgreementEditRoute.test.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import PropTypes from 'prop-types';
import { renderWithIntl, Button as ButtonInteractor } from '@folio/stripes-erm-testing';
import { MemoryRouter } from 'react-router-dom';
import { useQuery } from 'react-query';
import { useStripes } from '@folio/stripes/core';
import { Button } from '@folio/stripes/components';
import translationsProperties from '../../../test/helpers';
import AgreementEditRoute from './AgreementEditRoute';
import {
match,
location,
agreement,
basket,
} from './testResources';
import mockRefdata from '../../../test/jest/refdata';
const CloseButton = (props) => {
return <Button onClick={props.handlers.onClose}>CloseButton</Button>;
};
CloseButton.propTypes = {
handlers: PropTypes.shape({
onClose: PropTypes.func,
}),
};
const historyPushMock = jest.fn();
const onSubmitMock = jest.fn();
jest.mock('../../hooks', () => ({
...jest.requireActual('../../hooks'),
useAgreementsRefdata: () => mockRefdata,
}));
jest.mock('../../components/views/AgreementForm', () => {
return (props) => (
<div>
<div>AgreementForm</div>
<CloseButton {...props} />
</div>
);
});
const data = {
history: {
push: historyPushMock
},
location,
match,
resources: {
basket,
}
};
// Default mock implementation
useQuery.mockImplementation(() => ({ data: agreement, isLoading: false }));
describe('AgreementEditRoute', () => {
describe('rendering the route with permissions', () => {
let renderComponent;
beforeEach(() => {
renderComponent = renderWithIntl(
<MemoryRouter>
<AgreementEditRoute {...data} onSubmit={onSubmitMock} />
</MemoryRouter>,
translationsProperties
);
});
test('renders the agreementForm component', () => {
const { getByText } = renderComponent;
expect(getByText('AgreementForm')).toBeInTheDocument();
});
test('calls the CloseButton', async () => {
await ButtonInteractor('CloseButton').click();
expect(historyPushMock).toHaveBeenCalled();
});
});
describe('rendering loading view', () => {
let renderComponent;
beforeEach(() => {
// We have multiple useQuery calls, so make sure they're all loading for this test
useQuery.mockImplementation(() => ({ data: {}, isLoading: true }));
renderComponent = renderWithIntl(
<MemoryRouter>
<AgreementEditRoute {...data} />
</MemoryRouter>,
translationsProperties
);
});
afterEach(() => {
// Reset to the default implementation after this test
useQuery.mockImplementation(() => ({ data: agreement, isLoading: false }));
});
test('renders loadingView', () => {
const { getByText } = renderComponent;
expect(getByText('LoadingView')).toBeInTheDocument();
});
});
describe('rendering with no permissions', () => {
let renderComponent;
beforeEach(() => {
const { hasPerm } = useStripes();
hasPerm.mockImplementation(() => false);
renderComponent = renderWithIntl(
<MemoryRouter>
<AgreementEditRoute
{...data}
stripes={{ hasPerm: () => false }}
/>
</MemoryRouter>,
translationsProperties
);
});
test('displays the permission error', () => {
const { getByText } = renderComponent;
expect(getByText('Sorry - your permissions do not allow access to this page.')).toBeInTheDocument();
});
});
});