@openmrs/esm-form-engine-lib 2.1.0-pre.1433 → 2.1.0-pre.1434

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-engine-lib",
3
- "version": "2.1.0-pre.1433",
3
+ "version": "2.1.0-pre.1434",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
@@ -1,104 +1,198 @@
1
1
  import React from 'react';
2
- import { render, fireEvent, screen, act } from '@testing-library/react';
3
- import { type EncounterContext } from '../../../form-context';
4
- import { type FormField } from '../../../types';
5
-
6
- const question: FormField = {
7
- label: 'Patient Name',
8
- id: 'patient-name',
9
- type: 'obs',
10
- questionOptions: {
11
- rendering: 'text',
12
- concept: 'your_concept_uuid_here',
13
- },
14
- meta: {},
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
- const question2: FormField = {
18
- label: 'Patient Name',
19
- id: 'patient-name',
20
- type: 'obs',
21
- questionOptions: {
22
- rendering: 'text',
23
- concept: 'your_concept_uuid_here',
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
- meta: {},
112
+ value: null,
113
+ errors: [],
114
+ warnings: [],
115
+ setFieldValue: mockSetFieldValue,
26
116
  };
27
117
 
28
- const encounterContext: EncounterContext = {
29
- patient: {
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
- const renderForm = (intialValues) => {
50
- render(<></>);
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.skip('Text field input', () => {
54
- afterEach(() => {
55
- question.meta = {};
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('Patient Name');
162
+ await renderForm(textValues);
163
+ const inputField = screen.getByLabelText('Indicate your notes');
61
164
 
62
- await act(async () => {
63
- expect(question.meta.submission).toBe(undefined);
64
- });
165
+ await user.type(inputField, 'Updated patient notese');
65
166
 
66
- fireEvent.change(inputField, { target: { value: 'John Doe' } });
67
- fireEvent.blur(inputField);
167
+ expect(mockSetFieldValue).toHaveBeenCalledWith(
168
+ expect.objectContaining({
169
+ target: expect.objectContaining({
170
+ value: 'Updated patient notese',
171
+ }),
172
+ }),
173
+ );
174
+ });
68
175
 
69
- await act(async () => {
70
- expect(question.meta.submission.newValue).toEqual({
71
- concept: 'your_concept_uuid_here',
72
- formFieldNamespace: 'rfe-forms',
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 edit obs', async () => {
80
- question.meta.previousValue = {
81
- person: '833db896-c1f0-11eb-8529-0242ac130003',
82
- obsDatetime: encounterContext.encounterDate,
83
- concept: 'your_concept_uuid_here',
84
- location: { uuid: '41e6e516-c1f0-11eb-8529-0242ac130003' },
85
- order: null,
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
  });