-
Notifications
You must be signed in to change notification settings - Fork 209
/
vitals-biometrics-form.test.tsx
245 lines (208 loc) · 9.77 KB
/
vitals-biometrics-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
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
import React from 'react';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { type FetchResponse, showSnackbar } from '@openmrs/esm-framework';
import { mockConceptMetadata, mockVitalsConfig, mockVitalsSignsConcept } from '../__mocks__/vitals.mock';
import { mockPatient } from '../../../../tools/test-helpers';
import { saveVitalsAndBiometrics } from '../common';
import VitalsAndBiometricsForm from './vitals-biometrics-form.component';
const testProps = {
closeWorkspace: () => {},
patientUuid: mockPatient.id,
promptBeforeClosing: () => {},
};
const heightValue = 180;
const muacValue = 23;
const oxygenSaturationValue = 100;
const pulseValue = 80;
const respiratoryRateValue = 16;
const weightValue = 62;
const systolicBloodPressureValue = 120;
const temperatureValue = 37;
const mockedShowSnackbar = jest.mocked(showSnackbar);
const mockedSavePatientVitals = jest.mocked(saveVitalsAndBiometrics);
const mockConceptUnits = new Map<string, string>(
mockVitalsSignsConcept.data.results[0].setMembers.map((concept) => [concept.uuid, concept.units]),
);
const mockConceptRanges = new Map<string, { lowAbsolute: number | null; highAbsolute: number | null }>(
mockVitalsSignsConcept.data.results[0].setMembers.map((concept) => [
concept.uuid,
{ lowAbsolute: concept.lowAbsolute ?? null, highAbsolute: concept.hiAbsolute ?? null },
]),
);
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
useConfig: jest.fn().mockImplementation(() => mockVitalsConfig),
};
});
jest.mock('@openmrs/esm-patient-common-lib', () => {
const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib');
return {
...originalModule,
useVitalsConceptMetadata: jest.fn().mockImplementation(() => ({
data: mockConceptUnits,
conceptMetadata: mockConceptMetadata,
conceptRanges: mockConceptRanges,
})),
};
});
jest.mock('../common', () => ({
assessValue: jest.fn(),
getReferenceRangesForConcept: jest.fn(),
generatePlaceholder: jest.fn(),
interpretBloodPressure: jest.fn(),
invalidateCachedVitalsAndBiometrics: jest.fn(),
saveVitalsAndBiometrics: jest.fn(),
useVitalsAndBiometrics: jest.fn(),
}));
describe('VitalsBiometricsForm', () => {
beforeEach(() => {
mockedShowSnackbar.mockClear();
});
it('renders the vitals and biometrics form', async () => {
renderForm();
expect(screen.getByText(/vitals/i)).toBeInTheDocument();
expect(screen.getByText(/biometrics/i)).toBeInTheDocument();
expect(screen.getByText(/blood pressure/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /systolic/i })).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /diastolic/i })).toBeInTheDocument();
expect(screen.getByText(/mmHg/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /pulse/i })).toBeInTheDocument();
expect(screen.getByText(/beats\/min/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /oxygen saturation/i })).toBeInTheDocument();
expect(screen.getByText(/spO2/i)).toBeInTheDocument();
expect(screen.getByText(/%/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /respiration rate/i })).toBeInTheDocument();
expect(screen.getByText(/breaths\/min/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /temperature/i })).toBeInTheDocument();
expect(screen.getByText(/temp/i)).toBeInTheDocument();
expect(screen.getByText(/DEG C/i)).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /notes/i })).toBeInTheDocument();
expect(screen.getByPlaceholderText(/type any additional notes here/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /weight/i })).toBeInTheDocument();
expect(screen.getByText(/^kg$/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /height/i })).toBeInTheDocument();
expect(screen.getByText(/bmi \(calc.\)/i)).toBeInTheDocument();
expect(screen.getByText(/kg \/ m²/i)).toBeInTheDocument();
expect(screen.getByRole('spinbutton', { name: /muac/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /discard/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /save and close/i })).toBeInTheDocument();
});
it("computes a patient's BMI from the given height and weight values", async () => {
const user = userEvent.setup();
renderForm();
const heightInput = screen.getByRole('spinbutton', { name: /height/i });
const weightInput = screen.getByRole('spinbutton', { name: /weight/i });
const bmiInput = screen.getByRole('spinbutton', { name: /bmi/i });
await user.type(heightInput, '180');
await user.type(weightInput, '62');
expect(bmiInput).toHaveValue(19.1);
});
it('renders a success snackbar upon clicking the save button', async () => {
const user = userEvent.setup();
const response: Partial<FetchResponse> = {
statusText: 'created',
status: 201,
data: [],
};
mockedSavePatientVitals.mockReturnValue(Promise.resolve(response) as ReturnType<typeof saveVitalsAndBiometrics>);
renderForm();
const heightInput = screen.getByRole('spinbutton', { name: /height/i });
const weightInput = screen.getByRole('spinbutton', { name: /weight/i });
const bmiInput = screen.getByRole('spinbutton', { name: /bmi/i });
const systolic = screen.getByRole('spinbutton', { name: /systolic/i });
const pulse = screen.getByRole('spinbutton', { name: /pulse/i });
const oxygenSaturation = screen.getByRole('spinbutton', { name: /oxygen saturation/i });
const respirationRate = screen.getByRole('spinbutton', { name: /respiration rate/i });
const temperature = screen.getByRole('spinbutton', { name: /temperature/i });
const muac = screen.getByRole('spinbutton', { name: /muac/i });
const saveButton = screen.getByRole('button', { name: /Save and close/i });
await user.type(heightInput, heightValue.toString());
await user.type(weightInput, weightValue.toString());
await user.type(systolic, systolicBloodPressureValue.toString());
await user.type(pulse, pulseValue.toString());
await user.type(oxygenSaturation, oxygenSaturationValue.toString());
await user.type(respirationRate, respiratoryRateValue.toString());
await user.type(temperature, temperatureValue.toString());
await user.type(muac, muacValue.toString());
expect(bmiInput).toHaveValue(19.1);
expect(systolic).toHaveValue(120);
expect(pulse).toHaveValue(80);
expect(oxygenSaturation).toHaveValue(100);
expect(respirationRate).toHaveValue(16);
expect(temperature).toHaveValue(37);
expect(muac).toHaveValue(23);
await user.click(saveButton);
expect(mockedSavePatientVitals).toHaveBeenCalledTimes(1);
expect(mockedSavePatientVitals).toHaveBeenCalledWith(
mockVitalsConfig.vitals.encounterTypeUuid,
mockVitalsConfig.vitals.formUuid,
mockVitalsConfig.concepts,
mockPatient.id,
expect.objectContaining({
height: heightValue,
midUpperArmCircumference: muacValue,
oxygenSaturation: oxygenSaturationValue,
pulse: pulseValue,
respiratoryRate: respiratoryRateValue,
systolicBloodPressure: systolicBloodPressureValue,
temperature: temperatureValue,
weight: weightValue,
}),
expect.anything(),
new AbortController(),
undefined,
);
expect(mockedShowSnackbar).toHaveBeenCalledTimes(1);
expect(mockedShowSnackbar).toHaveBeenCalledWith(
expect.objectContaining({
isLowContrast: true,
kind: 'success',
subtitle: 'They are now visible on the Vitals and Biometrics page',
title: 'Vitals and Biometrics saved',
}),
);
});
it('renders an error snackbar if there was a problem saving vitals and biometrics', async () => {
const user = userEvent.setup();
const error = {
message: 'Some of the values entered are invalid',
response: {
status: 500,
statusText: 'Internal Server Error',
},
};
mockedSavePatientVitals.mockRejectedValueOnce(error);
renderForm();
const heightInput = screen.getByRole('spinbutton', { name: /height/i });
const weightInput = screen.getByRole('spinbutton', { name: /weight/i });
const systolic = screen.getByRole('spinbutton', { name: /systolic/i });
const pulse = screen.getByRole('spinbutton', { name: /pulse/i });
const oxygenSaturation = screen.getByRole('spinbutton', { name: /oxygen saturation/i });
const respirationRate = screen.getByRole('spinbutton', { name: /respiration rate/i });
const temperature = screen.getByRole('spinbutton', { name: /temperature/i });
const muac = screen.getByRole('spinbutton', { name: /muac/i });
await user.type(heightInput, heightValue.toString());
await user.type(weightInput, weightValue.toString());
await user.type(systolic, systolicBloodPressureValue.toString());
await user.type(pulse, pulseValue.toString());
await user.type(oxygenSaturation, oxygenSaturationValue.toString());
await user.type(respirationRate, respiratoryRateValue.toString());
await user.type(temperature, temperatureValue.toString());
await user.type(muac, muacValue.toString());
const saveButton = screen.getByRole('button', { name: /save and close/i });
await user.click(saveButton);
expect(mockedShowSnackbar).toHaveBeenCalledTimes(1);
expect(mockedShowSnackbar).toHaveBeenCalledWith({
isLowContrast: false,
kind: 'error',
subtitle: 'Some of the values entered are invalid',
title: 'Error saving vitals and biometrics',
});
});
});
function renderForm() {
render(<VitalsAndBiometricsForm {...testProps} />);
}