-
Notifications
You must be signed in to change notification settings - Fork 207
/
visit-detail-overview.test.tsx
131 lines (102 loc) · 5.67 KB
/
visit-detail-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
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
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { openmrsFetch, getConfig, useConfig } from '@openmrs/esm-framework';
import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools';
import { visitOverviewDetailMockData } from '__mocks__';
import VisitDetailOverview from './visit-detail-overview.component';
const testProps = {
patientUuid: mockPatient.id,
};
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockUseConfig = useConfig as jest.Mock;
const mockGetConfig = getConfig as jest.Mock;
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
getVisitsForPatient: jest.fn(),
createErrorHandler: jest.fn(),
useLayoutType: jest.fn(),
useConfig: jest.fn().mockImplementation(() => ({ numberOfVisitsToLoad: 5 })),
userHasAccess: jest.fn().mockImplementation((privilege, _) => (privilege ? false : true)),
ExtensionSlot: jest.fn().mockImplementation((ext) => ext.name),
useConnectedExtensions: jest.fn(() => []),
};
});
describe('VisitDetailOverview', () => {
it('renders an empty state view if encounters data is unavailable', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } });
mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] });
renderVisitDetailOverview();
await waitForLoadingToFinish();
expect(screen.getByRole('heading', { name: /visits/i })).toBeInTheDocument();
expect(screen.getAllByTitle(/Empty data illustration/i)[0]).toBeInTheDocument();
expect(screen.getAllByText(/There are no visits to display for this patient/i)[0]).toBeInTheDocument();
});
it('renders an error state view if there was a problem fetching encounter data', async () => {
const error = {
message: 'Unauthorized',
response: {
status: 401,
statusText: 'Unauthorized',
},
};
mockOpenmrsFetch.mockRejectedValueOnce(error);
renderVisitDetailOverview();
await waitForLoadingToFinish();
expect(screen.queryByRole('table')).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: /visits/i })).toBeInTheDocument();
expect(screen.getAllByText(/Error 401: Unauthorized/i)[0]).toBeInTheDocument();
expect(screen.getAllByText(/Sorry, there was a problem displaying this information/i)[0]).toBeInTheDocument();
});
it(`renders a summary of the patient's visits and encounters when data is available and showAllEncountersTab is true`, async () => {
const user = userEvent.setup();
mockOpenmrsFetch.mockReturnValueOnce(visitOverviewDetailMockData);
mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] });
mockUseConfig.mockImplementation(() => ({ showAllEncountersTab: true }));
renderVisitDetailOverview();
await waitForLoadingToFinish();
const allVisitsTab = screen.getByRole('tab', { name: /All encounters/i });
const visitSummariesTab = screen.getByRole('tab', { name: /visit summaries/i });
expect(visitSummariesTab).toBeInTheDocument();
expect(allVisitsTab).toBeInTheDocument();
expect(visitSummariesTab).toHaveAttribute('aria-selected', 'true');
expect(allVisitsTab).toHaveAttribute('aria-selected', 'false');
expect(screen.getByRole('tab', { name: /notes/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /tests/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /medications/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /^encounters$/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /ech/i })).toBeInTheDocument();
expect(screen.getByText(/^diagnoses$/i)).toBeInTheDocument();
expect(screen.getByText(/no diagnoses found/i)).toBeInTheDocument();
expect(screen.getByText(/There are no notes to display for this patient/i)).toBeInTheDocument();
expect(screen.getByText(/There are no medications to display for this patient/i)).toBeInTheDocument();
await user.click(allVisitsTab);
expect(allVisitsTab).toHaveAttribute('aria-selected', 'true');
expect(visitSummariesTab).toHaveAttribute('aria-selected', 'false');
});
it('should render only the visit summary tab when showAllEncountersTab is false', async () => {
mockOpenmrsFetch.mockReturnValueOnce(visitOverviewDetailMockData);
mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] });
mockUseConfig.mockImplementation(() => ({ showAllEncountersTab: false }));
renderVisitDetailOverview();
await waitForLoadingToFinish();
const visitSummariesTab = screen.getByRole('tab', { name: /visit summaries/i });
expect(visitSummariesTab).toBeInTheDocument();
expect(visitSummariesTab).toHaveAttribute('aria-selected', 'true');
expect(screen.queryByText('/All encounters/i')).not.toBeInTheDocument();
expect(screen.getByRole('tab', { name: /notes/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /tests/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /medications/i })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /^encounters$/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /ech/i })).toBeInTheDocument();
expect(screen.getByText(/^diagnoses$/i)).toBeInTheDocument();
expect(screen.getByText(/no diagnoses found/i)).toBeInTheDocument();
expect(screen.getByText(/There are no notes to display for this patient/i)).toBeInTheDocument();
expect(screen.getByText(/There are no medications to display for this patient/i)).toBeInTheDocument();
});
});
function renderVisitDetailOverview() {
renderWithSwr(<VisitDetailOverview {...testProps} />);
}