@openmrs/esm-form-engine-lib 2.1.0-pre.1433 → 2.1.0-pre.1435
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/5b7cf2c900238b5f/5b7cf2c900238b5f.gz +0 -0
- package/{2e36cddf3c16560f/2e36cddf3c16560f.gz → 70e17c49e61fa7d9/70e17c49e61fa7d9.gz} +0 -0
- package/package.json +1 -1
- package/src/components/inputs/number/number.test.tsx +94 -0
- package/src/components/inputs/text/text.test.tsx +177 -83
- package/352ae296f62fb1cd/352ae296f62fb1cd.gz +0 -0
- /package/{16563207957244dc/16563207957244dc.gz → 05e6df0fe8b59eb3/05e6df0fe8b59eb3.gz} +0 -0
- /package/{57b838e736d9d290/57b838e736d9d290.gz → 6110c95ec25070ab/6110c95ec25070ab.gz} +0 -0
Binary file
|
Binary file
|
package/package.json
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { act, render, screen, fireEvent } from '@testing-library/react';
|
3
|
+
import { useFormProviderContext } from 'src/provider/form-provider';
|
4
|
+
import NumberField from './number.component';
|
5
|
+
|
6
|
+
jest.mock('src/provider/form-provider', () => ({
|
7
|
+
useFormProviderContext: jest.fn(),
|
8
|
+
}));
|
9
|
+
|
10
|
+
const mockUseFormProviderContext = useFormProviderContext as jest.Mock;
|
11
|
+
|
12
|
+
const numberFieldMock = {
|
13
|
+
label: 'Weight(kg):',
|
14
|
+
type: 'obs',
|
15
|
+
id: 'weight',
|
16
|
+
questionOptions: {
|
17
|
+
rendering: 'number',
|
18
|
+
concept: '5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
19
|
+
},
|
20
|
+
isHidden: false,
|
21
|
+
isDisabled: false,
|
22
|
+
readonly: false,
|
23
|
+
};
|
24
|
+
|
25
|
+
const renderNumberField = async (props) => {
|
26
|
+
await act(() => render(<NumberField {...props} />));
|
27
|
+
};
|
28
|
+
|
29
|
+
const mockProviderValues = {
|
30
|
+
layoutType: 'small-desktop',
|
31
|
+
sessionMode: 'enter',
|
32
|
+
workspaceLayout: 'minimized',
|
33
|
+
formFieldAdapters: {},
|
34
|
+
};
|
35
|
+
|
36
|
+
describe('NumberField Component', () => {
|
37
|
+
beforeEach(() => {
|
38
|
+
mockUseFormProviderContext.mockReturnValue(mockProviderValues);
|
39
|
+
});
|
40
|
+
|
41
|
+
it('renders correctly', async () => {
|
42
|
+
await renderNumberField({
|
43
|
+
field: numberFieldMock,
|
44
|
+
value: '',
|
45
|
+
errors: [],
|
46
|
+
warnings: [],
|
47
|
+
setFieldValue: jest.fn(),
|
48
|
+
});
|
49
|
+
|
50
|
+
expect(screen.getByLabelText('Weight(kg):')).toBeInTheDocument();
|
51
|
+
});
|
52
|
+
|
53
|
+
it('calls setFieldValue on input change', async () => {
|
54
|
+
const mockSetFieldValue = jest.fn();
|
55
|
+
|
56
|
+
await renderNumberField({
|
57
|
+
field: numberFieldMock,
|
58
|
+
value: '',
|
59
|
+
errors: [],
|
60
|
+
warnings: [],
|
61
|
+
setFieldValue: mockSetFieldValue,
|
62
|
+
});
|
63
|
+
|
64
|
+
const inputElement = screen.getByLabelText('Weight(kg):') as HTMLInputElement;
|
65
|
+
fireEvent.change(inputElement, { target: { value: '150' } });
|
66
|
+
|
67
|
+
expect(mockSetFieldValue).toHaveBeenCalledWith(150);
|
68
|
+
});
|
69
|
+
|
70
|
+
it('displays error message when invalid', async () => {
|
71
|
+
await renderNumberField({
|
72
|
+
field: numberFieldMock,
|
73
|
+
value: '',
|
74
|
+
errors: [{ message: 'Invalid value' }],
|
75
|
+
warnings: [],
|
76
|
+
setFieldValue: jest.fn(),
|
77
|
+
});
|
78
|
+
|
79
|
+
expect(screen.getByText('Invalid value')).toBeInTheDocument();
|
80
|
+
});
|
81
|
+
|
82
|
+
it('disables input when field is disabled', async () => {
|
83
|
+
await renderNumberField({
|
84
|
+
field: { ...numberFieldMock, isDisabled: true },
|
85
|
+
value: '',
|
86
|
+
errors: [],
|
87
|
+
warnings: [],
|
88
|
+
setFieldValue: jest.fn(),
|
89
|
+
});
|
90
|
+
|
91
|
+
const inputElement = screen.getByLabelText('Weight(kg):') as HTMLInputElement;
|
92
|
+
expect(inputElement).toBeDisabled();
|
93
|
+
});
|
94
|
+
});
|
@@ -1,104 +1,198 @@
|
|
1
1
|
import React from 'react';
|
2
|
-
import { render,
|
3
|
-
import
|
4
|
-
import { type
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
2
|
+
import { render, screen, act } from '@testing-library/react';
|
3
|
+
import TextField from './text.component';
|
4
|
+
import { type FetchResponse, openmrsFetch, usePatient, useSession } from '@openmrs/esm-framework';
|
5
|
+
import { mockSessionDataResponse } from '__mocks__/session.mock';
|
6
|
+
import { mockPatient } from '__mocks__/patient.mock';
|
7
|
+
import { mockVisit } from '__mocks__/visit.mock';
|
8
|
+
import textFieldFormJson from '__mocks__/forms/rfe-forms/sample_fields.json';
|
9
|
+
import { useFormProviderContext } from 'src/provider/form-provider';
|
10
|
+
import userEvent from '@testing-library/user-event';
|
11
|
+
|
12
|
+
const mockOpenmrsFetch = jest.mocked(openmrsFetch);
|
13
|
+
const mockUseSession = jest.mocked(useSession);
|
14
|
+
const mockUsePatient = jest.mocked(usePatient);
|
15
|
+
const mockSetFieldValue = jest.fn();
|
16
|
+
|
17
|
+
jest.mock('../../../api', () => {
|
18
|
+
const originalModule = jest.requireActual('../../../api');
|
19
|
+
|
20
|
+
return {
|
21
|
+
...originalModule,
|
22
|
+
getPreviousEncounter: jest.fn().mockImplementation(() => Promise.resolve(null)),
|
23
|
+
};
|
24
|
+
});
|
16
25
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
jest.mock('src/provider/form-provider', () => ({
|
27
|
+
useFormProviderContext: jest.fn(),
|
28
|
+
}));
|
29
|
+
|
30
|
+
const mockUseFormProviderContext = useFormProviderContext as jest.Mock;
|
31
|
+
|
32
|
+
const textValues = {
|
33
|
+
field: {
|
34
|
+
label: 'Indicate your notes',
|
35
|
+
type: 'obs',
|
36
|
+
required: false,
|
37
|
+
id: 'indicateNotes',
|
38
|
+
questionOptions: {
|
39
|
+
rendering: 'text',
|
40
|
+
concept: '160632AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
41
|
+
conceptMappings: [
|
42
|
+
{
|
43
|
+
relationship: 'SAME-AS',
|
44
|
+
type: 'CIEL',
|
45
|
+
value: '160632',
|
46
|
+
},
|
47
|
+
{
|
48
|
+
relationship: 'SAME-AS',
|
49
|
+
type: 'AMPATH',
|
50
|
+
value: '1915',
|
51
|
+
},
|
52
|
+
{
|
53
|
+
relationship: 'BROADER-THAN',
|
54
|
+
type: 'LOINC',
|
55
|
+
value: '48767-8',
|
56
|
+
},
|
57
|
+
],
|
58
|
+
answers: [],
|
59
|
+
},
|
60
|
+
meta: {
|
61
|
+
submission: {
|
62
|
+
newValue: null,
|
63
|
+
},
|
64
|
+
concept: {
|
65
|
+
uuid: '160632AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
66
|
+
display: 'Free text general',
|
67
|
+
conceptClass: {
|
68
|
+
uuid: '8d491e50-c2cc-11de-8d13-0010c6dffd0f',
|
69
|
+
display: 'Question',
|
70
|
+
},
|
71
|
+
answers: [],
|
72
|
+
conceptMappings: [
|
73
|
+
{
|
74
|
+
conceptReferenceTerm: {
|
75
|
+
conceptSource: {
|
76
|
+
name: 'CIEL',
|
77
|
+
},
|
78
|
+
code: '160632',
|
79
|
+
},
|
80
|
+
},
|
81
|
+
{
|
82
|
+
conceptReferenceTerm: {
|
83
|
+
conceptSource: {
|
84
|
+
name: 'AMPATH',
|
85
|
+
},
|
86
|
+
code: '1915',
|
87
|
+
},
|
88
|
+
},
|
89
|
+
{
|
90
|
+
conceptReferenceTerm: {
|
91
|
+
conceptSource: {
|
92
|
+
name: 'LOINC',
|
93
|
+
},
|
94
|
+
code: '48767-8',
|
95
|
+
},
|
96
|
+
},
|
97
|
+
],
|
98
|
+
},
|
99
|
+
},
|
100
|
+
validators: [
|
101
|
+
{
|
102
|
+
type: 'form_field',
|
103
|
+
},
|
104
|
+
{
|
105
|
+
type: 'default_value',
|
106
|
+
},
|
107
|
+
],
|
108
|
+
isHidden: false,
|
109
|
+
isRequired: false,
|
110
|
+
isDisabled: false,
|
24
111
|
},
|
25
|
-
|
112
|
+
value: null,
|
113
|
+
errors: [],
|
114
|
+
warnings: [],
|
115
|
+
setFieldValue: mockSetFieldValue,
|
26
116
|
};
|
27
117
|
|
28
|
-
const
|
29
|
-
|
30
|
-
id: '833db896-c1f0-11eb-8529-0242ac130003',
|
31
|
-
},
|
32
|
-
location: {
|
33
|
-
uuid: '41e6e516-c1f0-11eb-8529-0242ac130003',
|
34
|
-
},
|
35
|
-
encounter: {
|
36
|
-
uuid: '873455da-3ec4-453c-b565-7c1fe35426be',
|
37
|
-
obs: [],
|
38
|
-
},
|
39
|
-
sessionMode: 'enter',
|
40
|
-
encounterDate: new Date(2020, 11, 29),
|
41
|
-
setEncounterDate: (value) => {},
|
42
|
-
encounterProvider: '2c95f6f5-788e-4e73-9079-5626911231fa',
|
43
|
-
setEncounterProvider: jest.fn,
|
44
|
-
setEncounterLocation: jest.fn,
|
45
|
-
encounterRole: '8cb3a399-d18b-4b62-aefb-5a0f948a3809',
|
46
|
-
setEncounterRole: jest.fn,
|
118
|
+
const renderForm = async (props) => {
|
119
|
+
await act(() => render(<TextField {...props} />));
|
47
120
|
};
|
48
121
|
|
49
|
-
|
50
|
-
|
122
|
+
let formProcessor;
|
123
|
+
|
124
|
+
const mockProviderValues = {
|
125
|
+
layoutType: 'small-desktop',
|
126
|
+
sessionMode: 'enter',
|
127
|
+
workspaceLayout: 'minimized',
|
128
|
+
formFieldAdapters: {},
|
129
|
+
patient: mockPatient,
|
130
|
+
methods: undefined,
|
131
|
+
formJson: textFieldFormJson as any,
|
132
|
+
visit: mockVisit,
|
133
|
+
sessionDate: new Date(),
|
134
|
+
location: mockVisit.location,
|
135
|
+
currentProvider: mockVisit.encounters[0]?.encounterProvider,
|
136
|
+
processor: formProcessor,
|
51
137
|
};
|
52
138
|
|
53
|
-
describe
|
54
|
-
|
55
|
-
|
139
|
+
describe('Text field input', () => {
|
140
|
+
const user = userEvent.setup();
|
141
|
+
beforeEach(() => {
|
142
|
+
formProcessor = {
|
143
|
+
getInitialValues: jest.fn(),
|
144
|
+
};
|
145
|
+
mockOpenmrsFetch.mockResolvedValue({
|
146
|
+
data: { results: [{ ...textFieldFormJson }] },
|
147
|
+
} as unknown as FetchResponse);
|
148
|
+
mockUseSession.mockReturnValue(mockSessionDataResponse.data);
|
149
|
+
mockUsePatient.mockReturnValue({
|
150
|
+
isLoading: false,
|
151
|
+
patient: mockPatient,
|
152
|
+
patientUuid: mockPatient.id,
|
153
|
+
error: null,
|
154
|
+
});
|
155
|
+
mockUseFormProviderContext.mockReturnValue({
|
156
|
+
...mockProviderValues,
|
157
|
+
setFieldValue: mockSetFieldValue,
|
158
|
+
});
|
56
159
|
});
|
57
160
|
|
58
161
|
it('should record new obs', async () => {
|
59
|
-
await renderForm(
|
60
|
-
const inputField = screen.getByLabelText('
|
162
|
+
await renderForm(textValues);
|
163
|
+
const inputField = screen.getByLabelText('Indicate your notes');
|
61
164
|
|
62
|
-
await
|
63
|
-
expect(question.meta.submission).toBe(undefined);
|
64
|
-
});
|
165
|
+
await user.type(inputField, 'Updated patient notese');
|
65
166
|
|
66
|
-
|
67
|
-
|
167
|
+
expect(mockSetFieldValue).toHaveBeenCalledWith(
|
168
|
+
expect.objectContaining({
|
169
|
+
target: expect.objectContaining({
|
170
|
+
value: 'Updated patient notese',
|
171
|
+
}),
|
172
|
+
}),
|
173
|
+
);
|
174
|
+
});
|
68
175
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
formFieldPath: 'rfe-forms-patient-name',
|
74
|
-
value: 'John Doe',
|
75
|
-
});
|
176
|
+
it('should have value passed in as prop', async () => {
|
177
|
+
await renderForm({
|
178
|
+
...textValues,
|
179
|
+
value: 'Initial patient notes',
|
76
180
|
});
|
181
|
+
const inputField = screen.getByLabelText('Indicate your notes');
|
182
|
+
|
183
|
+
expect(inputField).toHaveValue('Initial patient notes');
|
77
184
|
});
|
78
185
|
|
79
|
-
it('should
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
groupMembers: [],
|
87
|
-
voided: false,
|
88
|
-
value: 'Initial Name',
|
89
|
-
};
|
90
|
-
await renderForm({ 'patient-name': question.meta.previousValue });
|
91
|
-
const inputField = screen.getByLabelText('Patient Name');
|
92
|
-
|
93
|
-
fireEvent.change(inputField, { target: { value: 'Updated Name' } });
|
94
|
-
fireEvent.blur(inputField);
|
95
|
-
|
96
|
-
await act(async () => {
|
97
|
-
expect(question.meta.submission.newValue).toEqual({
|
98
|
-
value: 'Updated Name',
|
99
|
-
formFieldNamespace: 'rfe-forms',
|
100
|
-
formFieldPath: 'rfe-forms-patient-name',
|
101
|
-
});
|
186
|
+
it('should disable field', async () => {
|
187
|
+
await renderForm({
|
188
|
+
...textValues,
|
189
|
+
field: {
|
190
|
+
...textValues.field,
|
191
|
+
isDisabled: true,
|
192
|
+
},
|
102
193
|
});
|
194
|
+
const inputField = screen.getByLabelText('Indicate your notes');
|
195
|
+
|
196
|
+
expect(inputField).toBeDisabled();
|
103
197
|
});
|
104
198
|
});
|
Binary file
|
File without changes
|
File without changes
|