-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathunspecified.test.tsx
More file actions
174 lines (148 loc) · 5.31 KB
/
unspecified.test.tsx
File metadata and controls
174 lines (148 loc) · 5.31 KB
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
import React from 'react';
import { act, render, screen } from '@testing-library/react';
import { usePatient, useSession } from '@openmrs/esm-framework';
import { type FormSchema, type SessionMode } from '../../../types';
import { findNumberInput } from '../../../utils/test-utils';
import unspecifiedForm from '../../../../__mocks__/forms/rfe-forms/sample_unspecified-form.json';
import { FormEngine } from '../../..';
import { mockPatient } from '../../../../__mocks__/patient.mock';
import { mockSessionDataResponse } from '../../../../__mocks__/session.mock';
import userEvent from '@testing-library/user-event';
import * as api from '../../../api';
const mockUsePatient = jest.mocked(usePatient);
const mockUseSession = jest.mocked(useSession);
global.ResizeObserver = require('resize-observer-polyfill');
jest.mock('../../../api', () => {
const originalModule = jest.requireActual('../../../api');
return {
...originalModule,
getPreviousEncounter: jest.fn().mockImplementation(() => Promise.resolve(null)),
getConcept: jest.fn().mockImplementation(() => Promise.resolve(null)),
saveEncounter: jest.fn(),
};
});
jest.mock('../../../hooks/useConcepts', () => ({
useConcepts: jest.fn().mockImplementation((references: Set<string>) => {
return {
isLoading: false,
concepts: [],
error: undefined,
};
}),
}));
jest.mock('../../../hooks/useEncounterRole', () => ({
useEncounterRole: jest.fn().mockReturnValue({
isLoading: false,
encounterRole: { name: 'Clinician', uuid: 'clinician-uuid' },
error: undefined,
}),
}));
jest.mock('../../../hooks/useEncounter', () => ({
useEncounter: jest.fn().mockImplementation((formJson: FormSchema) => {
return {
encounter: formJson.encounter
? {
uuid: 'encounter-uuid',
obs: [],
}
: null,
isLoading: false,
error: undefined,
};
}),
}));
jest.mock('../../../hooks/usePersonAttributes', () => ({
usePersonAttributes: jest.fn().mockReturnValue({
personAttributes: [],
error: null,
isLoading: false,
}),
}));
const renderForm = async (mode: SessionMode = 'enter') => {
await act(async () => {
render(
<FormEngine
formJson={unspecifiedForm as FormSchema}
patientUUID="8673ee4f-e2ab-4077-ba55-4980f408773e"
mode={mode}
encounterUUID={mode === 'edit' ? 'encounter-uuid' : null}
/>,
);
});
};
describe('Unspecified', () => {
const user = userEvent.setup();
beforeEach(() => {
Object.defineProperty(window, 'i18next', {
writable: true,
configurable: true,
value: {
language: 'en',
t: jest.fn(),
},
});
mockUsePatient.mockImplementation(() => ({
patient: mockPatient,
isLoading: false,
error: undefined,
patientUuid: mockPatient.id,
}));
mockUseSession.mockImplementation(() => mockSessionDataResponse.data);
});
it('Should clear field value when the "Unspecified" checkbox is clicked', async () => {
//setup
await renderForm();
const unspecifiedCheckbox = screen.getByRole('checkbox', { name: /Unspecified/ });
const bodyWeightField = await findNumberInput(screen, 'Body Weight *');
// assert initial state
expect(unspecifiedCheckbox).not.toBeChecked();
expect(bodyWeightField.value).toBe('');
await user.type(bodyWeightField, '55');
// assert new value
expect(bodyWeightField.value).toBe('55');
// mark as unspecified
await user.click(unspecifiedCheckbox);
expect(unspecifiedCheckbox).toBeChecked();
expect(bodyWeightField.value).toBe('');
});
it('Should bypass form validation when the "Unspecified" checkbox is clicked', async () => {
//setup
const mockSaveEncounter = jest.spyOn(api, 'saveEncounter');
await renderForm();
const unspecifiedCheckbox = screen.getByRole('checkbox', { name: /Unspecified/ });
const bodyWeightField = await findNumberInput(screen, 'Body Weight *');
// assert initial state
expect(unspecifiedCheckbox).not.toBeChecked();
expect(bodyWeightField.value).toBe('');
// attempt to submit the form
await user.click(screen.getByRole('button', { name: /Save/ }));
expect(screen.getByText(/Field is mandatory/)).toBeInTheDocument();
expect(mockSaveEncounter).not.toHaveBeenCalled();
// mark as unspecified
await user.click(unspecifiedCheckbox);
expect(unspecifiedCheckbox).toBeChecked();
expect(bodyWeightField.value).toBe('');
// submit the form again
await user.click(screen.getByRole('button', { name: /Save/ }));
expect(mockSaveEncounter).toHaveBeenCalled();
});
it('Should mark fields with null values as unspecified when in edit mode', async () => {
// setup
await renderForm('edit');
const unspecifiedCheckbox = screen.getByRole('checkbox', { name: /Unspecified/ });
const bodyWeightField = await findNumberInput(screen, 'Body Weight *');
// assert initial state
expect(unspecifiedCheckbox).toBeChecked();
expect(bodyWeightField.value).toBe('');
});
it('Should not display the unspecified checkbox in view mode', async () => {
// setup
await renderForm('view');
try {
screen.getByRole('checkbox', { name: /Unspecified/ });
fail('Unspecified checkbox should not be displayed');
} catch (error) {
expect(error).toBeDefined();
}
});
});