-
Notifications
You must be signed in to change notification settings - Fork 227
/
programs-detailed-summary.test.tsx
129 lines (97 loc) · 5.45 KB
/
programs-detailed-summary.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
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, within } from '@testing-library/react';
import { getDefaultsFromConfigSchema, openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib';
import { mockCareProgramsResponse, mockEnrolledInAllProgramsResponse, mockEnrolledProgramsResponse } from '__mocks__';
import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools';
import { type ConfigObject, configSchema } from '../config-schema';
import ProgramsDetailedSummary from './programs-detailed-summary.component';
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockUseConfig = jest.mocked(useConfig<ConfigObject>);
jest.mock('@openmrs/esm-patient-common-lib', () => {
const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib');
return {
...originalModule,
launchPatientWorkspace: jest.fn(),
};
});
describe('ProgramsDetailedSummary', () => {
it('renders an empty state view when the patient is not enrolled into any programs', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } });
renderWithSwr(<ProgramsDetailedSummary patientUuid={mockPatient.id} />);
await waitForLoadingToFinish();
expect(screen.getByText(/Care Programs/i)).toBeInTheDocument();
expect(screen.getByText(/There are no program enrollments to display for this patient/)).toBeInTheDocument();
expect(screen.getByText(/Record program enrollments/)).toBeInTheDocument();
});
it('renders an error state view if there is a problem fetching program enrollments', async () => {
const error = {
message: 'You are not logged in',
response: {
status: 401,
statusText: 'Unauthorized',
},
};
mockOpenmrsFetch.mockRejectedValueOnce(error);
renderWithSwr(<ProgramsDetailedSummary patientUuid={mockPatient.id} />);
await waitForLoadingToFinish();
expect(screen.getByText(/Care Programs/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./,
),
).toBeInTheDocument();
});
it('renders a detailed tabular summary of the patient program enrollments', async () => {
const user = userEvent.setup();
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: mockEnrolledProgramsResponse } });
renderWithSwr(<ProgramsDetailedSummary patientUuid={mockPatient.id} />);
await waitForLoadingToFinish();
expect(screen.getByText(/Care Programs/i)).toBeInTheDocument();
expect(screen.getByRole('table')).toBeInTheDocument();
expect(screen.getByRole('columnheader', { name: /active programs/i })).toBeInTheDocument();
expect(screen.getByRole('columnheader', { name: /date enrolled/i })).toBeInTheDocument();
expect(screen.getByRole('columnheader', { name: /status/i })).toBeInTheDocument();
const addButton = screen.getByRole('button', { name: /Add/ });
expect(addButton).toBeInTheDocument();
const row = screen.getByRole('row', { name: /hiv care and treatment/i });
expect(row).toBeInTheDocument();
expect(within(row).getByRole('cell', { name: /16-Jan-2020/i })).toBeInTheDocument();
expect(within(row).getByRole('cell', { name: /active$/i })).toBeInTheDocument();
const editButton = within(row).getByRole('button', { name: /Edit Program$/i });
expect(editButton).toBeInTheDocument();
// Clicking "Add" launches the programs form in a workspace
expect(addButton).toBeEnabled();
await user.click(addButton);
expect(launchPatientWorkspace).toHaveBeenCalledWith('programs-form-workspace');
// Clicking the edit button launches the edit form in a workspace
await user.click(editButton);
expect(launchPatientWorkspace).toHaveBeenCalledWith('programs-form-workspace', {
programEnrollmentId: mockEnrolledProgramsResponse[0].uuid,
});
});
it('renders a notification when the patient is enrolled in all available programs', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: mockEnrolledInAllProgramsResponse } });
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: mockCareProgramsResponse } });
renderWithSwr(<ProgramsDetailedSummary patientUuid={mockPatient.id} />);
await waitForLoadingToFinish();
expect(screen.getByRole('row', { name: /hiv care and treatment/i })).toBeInTheDocument();
expect(screen.getByRole('row', { name: /hiv differentiated care/i })).toBeInTheDocument();
expect(screen.getByRole('row', { name: /oncology screening and diagnosis/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /add/i })).toBeDisabled();
expect(screen.getByText(/enrolled in all programs/i)).toBeInTheDocument();
expect(screen.getByText(/there are no more programs left to enroll this patient in/i)).toBeInTheDocument();
});
it('renders the programs status field', async () => {
const user = userEvent.setup();
mockOpenmrsFetch.mockReturnValueOnce({ data: { results: mockEnrolledProgramsResponse } });
mockUseConfig.mockReturnValue({
...getDefaultsFromConfigSchema(configSchema),
showProgramStatusField: true,
});
renderWithSwr(<ProgramsDetailedSummary patientUuid={mockPatient.id} />);
await waitForLoadingToFinish();
expect(screen.getByRole('columnheader', { name: /program status/i })).toBeInTheDocument();
});
});