-
Notifications
You must be signed in to change notification settings - Fork 227
/
immunizations-overview.test.tsx
102 lines (81 loc) · 3.67 KB
/
immunizations-overview.test.tsx
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
import React from 'react';
import { screen } from '@testing-library/react';
import { openmrsFetch, usePagination } from '@openmrs/esm-framework';
import { mockPatientImmunizationsSearchResponse, mockPaginatedImmunizations } from '__mocks__';
import { mockPatient, patientChartBasePath, renderWithSwr, waitForLoadingToFinish } from 'tools';
import ImmunizationsOverview from './immunizations-overview.component';
const testProps = {
basePath: patientChartBasePath,
patient: mockPatient,
patientUuid: mockPatient.id,
};
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockUsePagination = usePagination as jest.Mock;
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
usePagination: jest.fn().mockImplementation(() => ({
currentPage: 1,
goTo: () => {},
results: [],
})),
};
});
describe('ImmunizationOverview: ', () => {
it('renders an empty state view of immunizations data is unavailable', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: [] });
renderImmunizationsOverview();
await waitForLoadingToFinish();
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: /immunizations/i })).toBeInTheDocument();
expect(screen.getByTitle(/Empty data illustration/i)).toBeInTheDocument();
expect(screen.getByText(/There are no immunizations to display for this patient/i)).toBeInTheDocument();
expect(screen.getByText(/Record immunizations/i)).toBeInTheDocument();
});
it('renders an error state view if there is a problem fetching immunization data', async () => {
const error = {
message: 'You are not logged in',
response: {
status: 401,
statusText: 'Unauthorized',
},
};
mockOpenmrsFetch.mockRejectedValueOnce(error);
renderImmunizationsOverview();
await waitForLoadingToFinish();
expect(screen.queryByRole('table')).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: /immunizations/i })).toBeInTheDocument();
expect(screen.getByText(/Error 401: Unauthorized/i)).toBeInTheDocument();
expect(
screen.getByText(
/Sorry, there was a problem displaying this information. You can try to reload this page, or contact the site administrator and quote the error code above/i,
),
).toBeInTheDocument();
});
it('renders a tabular overview of recently administered immunizations if available', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: mockPatientImmunizationsSearchResponse });
mockUsePagination.mockImplementation(() => ({
currentPage: 1,
goTo: () => {},
results: mockPaginatedImmunizations,
}));
renderImmunizationsOverview();
await waitForLoadingToFinish();
expect(screen.getByRole('heading', { name: /immunizations/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /add/i })).toBeInTheDocument();
const expectedColumnHeaders = [/recent vaccination/, /vaccination date/];
expectedColumnHeaders.forEach((header) => {
expect(screen.getByRole('columnheader', { name: new RegExp(header, 'i') })).toBeInTheDocument();
});
const expectedTableRows = [/rotavirus sept 2018/, /polio nov 2018/, /influenza may 2018/];
expectedTableRows.forEach((row) => {
expect(screen.getByRole('row', { name: new RegExp(row, 'i') })).toBeInTheDocument();
});
expect(screen.getAllByRole('row').length).toEqual(4);
expect(screen.getByText(/1–3 of 3 items/i)).toBeInTheDocument();
});
});
function renderImmunizationsOverview() {
renderWithSwr(<ImmunizationsOverview {...testProps} />);
}