-
Notifications
You must be signed in to change notification settings - Fork 207
/
visit-notes-form.test.tsx
206 lines (165 loc) · 7.68 KB
/
visit-notes-form.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
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
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, render } from '@testing-library/react';
import { of } from 'rxjs/internal/observable/of';
import { showSnackbar, useConfig, useSession } from '@openmrs/esm-framework';
import { fetchConceptDiagnosisByName, saveVisitNote } from './visit-notes.resource';
import {
ConfigMock,
diagnosisSearchResponse,
mockFetchLocationByUuidResponse,
mockFetchProviderByUuidResponse,
mockSessionDataResponse,
} from '__mocks__';
import { mockPatient, getByTextWithMarkup } from 'tools';
import VisitNotesForm from './visit-notes-form.component';
jest.setTimeout(10000);
const testProps = {
patientUuid: mockPatient.id,
closeWorkspace: jest.fn(),
promptBeforeClosing: jest.fn(),
};
const mockFetchConceptDiagnosisByName = fetchConceptDiagnosisByName as jest.Mock;
const mockSaveVisitNote = saveVisitNote as jest.Mock;
const mockedShowSnackbar = jest.mocked(showSnackbar);
const mockUseConfig = useConfig as jest.Mock;
const mockUseSession = useSession as jest.Mock;
jest.mock('lodash-es/debounce', () => jest.fn((fn) => fn));
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
createErrorHandler: jest.fn(),
showSnackbar: jest.fn(),
useConfig: jest.fn().mockImplementation(() => ConfigMock),
useSession: jest.fn().mockImplementation(() => mockSessionDataResponse),
};
});
jest.mock('./visit-notes.resource', () => ({
fetchConceptDiagnosisByName: jest.fn(),
useLocationUuid: jest.fn().mockImplementation(() => ({
data: mockFetchLocationByUuidResponse.data.uuid,
})),
useProviderUuid: jest.fn().mockImplementation(() => ({
data: mockFetchProviderByUuidResponse.data.uuid,
})),
saveVisitNote: jest.fn(),
useVisitNotes: jest.fn().mockImplementation(() => ({
mutateVisitNotes: jest.fn(),
})),
}));
test('renders the visit notes form with all the relevant fields and values', () => {
mockFetchConceptDiagnosisByName.mockReturnValue(of([]));
renderVisitNotesForm();
expect(screen.getByRole('textbox', { name: /Visit date/i })).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /Write your notes/i })).toBeInTheDocument();
expect(screen.getByRole('searchbox', { name: /Enter Primary diagnoses/i })).toBeInTheDocument();
expect(screen.getByRole('searchbox', { name: /Enter Secondary diagnoses/i })).toBeInTheDocument();
expect(screen.getByRole('group', { name: /Add an image to this visit/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Add image/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Discard/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Save and close/i })).toBeInTheDocument();
});
test.only('typing in the diagnosis search input triggers a search', async () => {
mockFetchConceptDiagnosisByName.mockReturnValue(of(diagnosisSearchResponse.results));
renderVisitNotesForm();
const searchBox = screen.getByPlaceholderText('Choose a primary diagnosis');
await userEvent.type(searchBox, 'Diabetes Mellitus');
const targetSearchResult = screen.getByText('Diabetes Mellitus');
expect(targetSearchResult).toBeInTheDocument();
expect(screen.getByText('Diabetes Mellitus, Type II')).toBeInTheDocument();
// clicking on a search result displays the selected diagnosis as a tag
await userEvent.click(targetSearchResult);
expect(screen.getByTitle('Diabetes Mellitus')).toBeInTheDocument();
const diabetesMellitusTag = screen.getByTitle(/^Diabetes Mellitus$/i);
expect(diabetesMellitusTag).toBeInTheDocument();
const closeTagButton = screen.getByRole('button', { name: /clear filter/i });
// Clicking the close button on the tag removes the selected diagnosis
await userEvent.click(closeTagButton);
// no selected diagnoses left
expect(screen.getByText(/No diagnosis selected — Enter a diagnosis below/i)).toBeInTheDocument();
});
test('renders an error message when no matching diagnoses are found', async () => {
const user = userEvent.setup();
mockFetchConceptDiagnosisByName.mockReturnValue(of([]));
renderVisitNotesForm();
const searchBox = screen.getByPlaceholderText('Choose a primary diagnosis');
await user.type(searchBox, 'COVID-21');
expect(getByTextWithMarkup('No diagnoses found matching "COVID-21"')).toBeInTheDocument();
});
test('closes the form and the workspace when the cancel button is clicked', async () => {
renderVisitNotesForm();
const cancelButton = screen.getByRole('button', { name: /Discard/i });
await userEvent.click(cancelButton);
expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1);
});
test('renders a success snackbar upon successfully recording a visit note', async () => {
const successPayload = {
encounterProviders: expect.arrayContaining([
{
encounterRole: ConfigMock.visitNoteConfig.clinicianEncounterRole,
provider: undefined,
},
]),
encounterType: ConfigMock.visitNoteConfig.encounterTypeUuid,
form: ConfigMock.visitNoteConfig.formConceptUuid,
location: undefined,
obs: expect.arrayContaining([
{
concept: { display: '', uuid: '162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' },
value: 'Sample clinical note',
},
]),
patient: mockPatient.id,
};
mockSaveVisitNote.mockResolvedValueOnce({ status: 201, body: 'Condition created' });
mockFetchConceptDiagnosisByName.mockReturnValue(of(diagnosisSearchResponse.results));
renderVisitNotesForm();
const searchBox = screen.getByPlaceholderText('Choose a primary diagnosis');
await userEvent.type(searchBox, 'Diabetes Mellitus');
const targetSearchResult = screen.getByText('Diabetes Mellitus');
expect(targetSearchResult).toBeInTheDocument();
await userEvent.click(targetSearchResult);
const clinicalNote = screen.getByRole('textbox', { name: /Write your notes/i });
await userEvent.clear(clinicalNote);
await userEvent.type(clinicalNote, 'Sample clinical note');
expect(clinicalNote).toHaveValue('Sample clinical note');
const submitButton = screen.getByRole('button', { name: /Save and close/i });
await userEvent.click(submitButton);
expect(mockSaveVisitNote).toHaveBeenCalledTimes(1);
expect(mockSaveVisitNote).toHaveBeenCalledWith(new AbortController(), expect.objectContaining(successPayload));
});
test('renders an error snackbar if there was a problem recording a condition', async () => {
const error = {
message: 'Internal Server Error',
response: {
status: 500,
statusText: 'Internal Server Error',
},
};
mockSaveVisitNote.mockRejectedValueOnce(error);
mockFetchConceptDiagnosisByName.mockReturnValue(of(diagnosisSearchResponse.results));
renderVisitNotesForm();
const searchBox = screen.getByPlaceholderText('Choose a primary diagnosis');
await userEvent.type(searchBox, 'Diabetes Mellitus');
const targetSearchResult = screen.getByText('Diabetes Mellitus');
expect(targetSearchResult).toBeInTheDocument();
await userEvent.click(targetSearchResult);
const clinicalNote = screen.getByRole('textbox', { name: /Write your notes/i });
await userEvent.clear(clinicalNote);
await userEvent.type(clinicalNote, 'Sample clinical note');
expect(clinicalNote).toHaveValue('Sample clinical note');
const submitButton = screen.getByRole('button', { name: /Save and close/i });
await userEvent.click(submitButton);
expect(mockedShowSnackbar).toHaveBeenCalledWith({
isLowContrast: false,
kind: 'error',
subtitle: 'Internal Server Error',
title: 'Error saving visit note',
});
});
function renderVisitNotesForm() {
mockUseConfig.mockReturnValue(ConfigMock);
mockUseSession.mockReturnValue(mockSessionDataResponse);
render(<VisitNotesForm {...testProps} />);
}