-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathNationalExams.unit.spec.jsx
273 lines (249 loc) · 7.31 KB
/
NationalExams.unit.spec.jsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React from 'react';
import { expect } from 'chai';
import { cleanup } from '@testing-library/react';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { VaPagination } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import NationalExamsList from '../../containers/NationalExamsList';
import { formatNationalExamName } from '../../utils/helpers';
const mockExams = [
{
enrichedId: '1@acce9',
name: 'AP-ADVANCED PLACEMENT EXAMS',
},
{
enrichedId: '2@5bf2b',
name: 'CLEP-COLLEGE LEVEL EXAMINATION PROGRAM',
},
{
enrichedId: '3@48003',
name: 'DANTES SPONSORED CLEP EXAMS',
},
{
enrichedId: '4@a359f',
name: 'DAT-DENTAL ADMISSION TEST',
},
{
enrichedId: '5@8527d',
name: 'GMAT-GRADUATE MGMT ADMISSION TEST',
},
{
enrichedId: '6@a4d71',
name: 'GRE-GRADUATE RECORD EXAM',
},
{
enrichedId: '7@5073b',
name: 'TOEFL',
},
{
enrichedId: '8@2eef3',
name: 'MCAT',
},
{
enrichedId: '9@f683b',
name: 'OAT-OPTOMETRY ADMISSION TEST',
},
{
enrichedId: '10@b4bfb',
name: 'SAT-SCHOLASTIC ASSESSMENT TEST',
},
{
enrichedId: '11@fc1dd',
name: 'CAS',
},
{
enrichedId: '12@53d2a',
name: 'LSAT-LAW SCHOOL ADMISSION TEST',
},
{
enrichedId: '13@8eca8',
name: 'ACT',
},
{
enrichedId: '14@2db24',
name: 'DSST-DANTES',
},
{
enrichedId: '15@8fd2a',
name: 'MAT-MILLER ANALOGIES TEST',
},
{
enrichedId: '16@e477d',
name: 'PCAT-PHARMACY COLLEGE ADMISSON TEST',
},
{
enrichedId: '17@8479c',
name: 'ECE (4 hours)',
},
{
enrichedId: '18@07aaf',
name: 'ECE (6 hours)',
},
{
enrichedId: '19@a36f3',
name: 'ECE 8 HOURS NURSING',
},
];
const mockStore = configureStore([thunk]);
describe('NationalExamsList', () => {
let store;
const initialState = {
nationalExams: {
nationalExams: mockExams,
loading: false,
error: null,
},
};
beforeEach(() => {
store = mockStore(initialState);
});
afterEach(() => {
cleanup();
});
const mountComponent = () => {
store = mockStore({
nationalExams: {
...initialState.nationalExams,
},
});
return mount(
<Provider store={store}>
<NationalExamsList />
</Provider>,
);
};
it('should render National Exams when not loading', () => {
const wrapper = mountComponent();
expect(wrapper.exists()).to.be.true;
expect(wrapper.find('h1').text()).to.equal('National Exams');
expect(wrapper.find('p').exists()).to.be.true;
expect(wrapper.find(VaPagination).length).to.equal(1);
expect(wrapper.find(VaPagination).props().page).to.equal(1);
wrapper.unmount();
});
it('should render the GI Bill reimbursement link correctly', () => {
const wrapper = mountComponent();
const link = wrapper.find('va-link').at(0);
expect(link.prop('href')).to.equal(
'https://www.va.gov/education/about-gi-bill-benefits/how-to-use-benefits/national-tests/',
);
expect(link.prop('text')).to.equal(
'Find out how to get reimbursed for national tests',
);
wrapper.unmount();
});
it('should render the correct number of exams for the first page', () => {
const wrapper = mountComponent();
const examItems = wrapper.find('va-card');
expect(examItems.length).to.equal(10);
});
it('should calculate the correct total number of pages', () => {
const wrapper = mountComponent();
const totalPages = wrapper.find(VaPagination).props().pages;
expect(totalPages).to.equal(2);
});
it('calculates the total number of pages correctly in VaPagination', () => {
const wrapper = mountComponent();
const itemsPerPage = 10;
const totalItems = store.getState().nationalExams.nationalExams.length;
const totalPages = Math.ceil(totalItems / itemsPerPage);
const paginationComponent = wrapper.find('VaPagination');
expect(paginationComponent.prop('pages')).to.equal(totalPages);
wrapper.unmount();
});
it('simulates page change in VaPagination to page 2 and verifies displayed items', async () => {
const wrapper = mountComponent();
const newPage = 2;
const itemsPerPage = 10;
wrapper.find('VaPagination').prop('onPageSelect')({
detail: { page: newPage },
});
wrapper.update();
await new Promise(resolve => setTimeout(resolve, 0));
const expectedItems = initialState.nationalExams.nationalExams
.slice((newPage - 1) * itemsPerPage, newPage * itemsPerPage)
.map(exam => exam.name);
const expectedItemsFormatted = expectedItems.map(name =>
formatNationalExamName(name),
);
const displayedItems = wrapper.find('li h3').map(node => node.text());
expect(displayedItems).to.deep.equal(expectedItemsFormatted);
wrapper.unmount();
});
it('displays the loading indicator when loading is true', () => {
// Mount the component with loading state set to true
store = mockStore({
nationalExams: {
...initialState.nationalExams,
loading: true,
error: null,
},
});
const wrapper = mount(
<Provider store={store}>
<NationalExamsList />
</Provider>,
);
// Check if the loading indicator is rendered
const loadingIndicator = wrapper.find('va-loading-indicator');
expect(loadingIndicator.exists()).to.be.true;
expect(loadingIndicator.prop('label')).to.equal('Loading');
expect(loadingIndicator.prop('message')).to.equal(
'Loading your national exams...',
);
wrapper.unmount();
});
it('should hide the loading indicator once loading is complete', () => {
store = mockStore({
nationalExams: { nationalExams: mockExams, loading: true, error: null },
});
const wrapper = mount(
<Provider store={store}>
<NationalExamsList />
</Provider>,
);
// Initially, loading indicator is visible
expect(wrapper.find('va-loading-indicator').exists()).to.be.true;
// Update store to set loading to false
store = mockStore({
nationalExams: {
nationalExams: mockExams,
loading: false,
error: null,
},
});
wrapper.setProps({ store });
// Check if loading indicator is no longer present
expect(wrapper.find('va-loading-indicator').exists()).to.be.false;
wrapper.unmount();
});
it('displays an error message when there is an error in the state', () => {
store = mockStore({
nationalExams: {
...initialState.nationalExams,
loading: false,
error: 'Server error occurred',
},
});
const wrapper = mount(
<Provider store={store}>
<NationalExamsList />
</Provider>,
);
// Check that the error alert is displayed
const alert = wrapper.find('va-alert');
expect(alert.exists()).to.be.true;
expect(alert.prop('status')).to.equal('error');
expect(alert.find('h2[slot="headline"]').text()).to.equal(
'We can’t load the national exams list right now',
);
expect(alert.find('p').text()).to.include(
'We’re sorry. There’s a problem with our system. Try again later.',
);
// Check institution name and program type are displayed
expect(wrapper.find('h1').text()).to.equal('National Exams');
wrapper.unmount();
});
});