@openmrs/esm-form-engine-lib 2.1.0-pre.1466 → 2.1.0-pre.1475

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.1466",
3
+ "version": "2.1.0-pre.1475",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
@@ -0,0 +1,415 @@
1
+ import { type FormContextProps } from '../provider/form-provider';
2
+ import { type FormField } from '../types';
3
+ import { ProgramStateAdapter } from './program-state-adapter';
4
+
5
+ const formContext = {
6
+ methods: null,
7
+ workspaceLayout: 'maximized',
8
+ isSubmitting: false,
9
+ patient: {
10
+ id: '833db896-c1f0-11eb-8529-0242ac130003',
11
+ },
12
+ formJson: null,
13
+ visit: null,
14
+ sessionMode: 'enter',
15
+ sessionDate: new Date(),
16
+ location: {
17
+ uuid: '41e6e516-c1f0-11eb-8529-0242ac130003',
18
+ },
19
+ currentProvider: null,
20
+ layoutType: 'small-desktop',
21
+ domainObjectValue: {
22
+ uuid: '873455da-3ec4-453c-b565-7c1fe35426be',
23
+ obs: [],
24
+ },
25
+ previousDomainObjectValue: null,
26
+ processor: null,
27
+ formFields: [],
28
+ formFieldAdapters: null,
29
+ formFieldValidators: null,
30
+ customDependencies: {
31
+ patientPrograms: [],
32
+ },
33
+ getFormField: jest.fn(),
34
+ addFormField: jest.fn(),
35
+ updateFormField: jest.fn(),
36
+ removeFormField: () => {},
37
+ addInvalidField: jest.fn(),
38
+ removeInvalidField: jest.fn(),
39
+ setInvalidFields: jest.fn(),
40
+ setForm: jest.fn(),
41
+ } as FormContextProps;
42
+
43
+ const field = {
44
+ label: 'HIV Enrollment Initial State',
45
+ type: 'programState',
46
+ required: false,
47
+ id: 'hivEnrollmentInitialState',
48
+ questionOptions: {
49
+ rendering: 'select',
50
+ answers: [
51
+ {
52
+ value: '7293cb90-c93f-4386-b32f-e8cfc633dc3e',
53
+ label: 'Example Option 1',
54
+ },
55
+ {
56
+ value: 'c26a8cc7-fb07-4b2f-bdb0-730db9ce0020',
57
+ label: 'Example Option 2',
58
+ },
59
+ {
60
+ value: '29a513f0-2810-4356-98f5-42b12f7013a5',
61
+ label: 'Example Option 3',
62
+ },
63
+ {
64
+ value: '7c0a5653-313f-4564-b9cf-d59adf1173dc',
65
+ label: 'On Antiretrovirals Treatment',
66
+ },
67
+ ],
68
+ programUuid: '64f950e6-1b07-4ac0-8e7e-f3e148f3463f',
69
+ workflowUuid: '70921392-4e3e-5465-978d-45b68b7def5f',
70
+ },
71
+ meta: {
72
+ submission: {},
73
+ },
74
+ } as FormField;
75
+
76
+ const patientPrograms = [
77
+ {
78
+ uuid: 'c0dd89a7-62d5-40ed-850a-d3b1709ea7f2',
79
+ display: 'HIV Care and Treatment',
80
+ program: {
81
+ uuid: '64f950e6-1b07-4ac0-8e7e-f3e148f3463f',
82
+ name: 'HIV Care and Treatment',
83
+ allWorkflows: [
84
+ {
85
+ uuid: '70921392-4e3e-5465-978d-45b68b7def5f',
86
+ concept: {
87
+ uuid: '7dc379f6-1725-11ed-861d-0242ac120002',
88
+ display: 'HIV treatment status',
89
+ links: [
90
+ {
91
+ rel: 'self',
92
+ uri: 'http://dev3.openmrs.org/openmrs/ws/rest/v1/concept/7dc379f6-1725-11ed-861d-0242ac120002',
93
+ resourceAlias: 'concept',
94
+ },
95
+ ],
96
+ },
97
+ description: null,
98
+ retired: false,
99
+ states: [
100
+ {
101
+ uuid: '7293cb90-c93f-4386-b32f-e8cfc633dc3e',
102
+ description: null,
103
+ retired: false,
104
+ concept: {
105
+ uuid: 'acc6f157-c9a5-4690-b814-e653cbf80b4c',
106
+ display: 'Example Option 1',
107
+ name: {
108
+ display: 'Example Option 1',
109
+ uuid: '51d0d6c4-0361-479e-aec4-b40ee82fb23c',
110
+ name: 'Example Option 1',
111
+ locale: 'en',
112
+ localePreferred: true,
113
+ conceptNameType: 'FULLY_SPECIFIED',
114
+ links: [],
115
+ resourceVersion: '1.9',
116
+ },
117
+ datatype: {
118
+ uuid: '8d4a4c94-c2cc-11de-8d13-0010c6dffd0f',
119
+ display: 'N/A',
120
+ links: [],
121
+ },
122
+ conceptClass: {
123
+ uuid: '8d492774-c2cc-11de-8d13-0010c6dffd0f',
124
+ display: 'Misc',
125
+ links: [],
126
+ },
127
+ set: false,
128
+ version: null,
129
+ retired: false,
130
+ names: [
131
+ {
132
+ uuid: '51d0d6c4-0361-479e-aec4-b40ee82fb23c',
133
+ display: 'Example Option 1',
134
+ links: [],
135
+ },
136
+ ],
137
+ descriptions: [],
138
+ mappings: [],
139
+ answers: [],
140
+ setMembers: [],
141
+ attributes: [],
142
+ links: [],
143
+ resourceVersion: '2.0',
144
+ },
145
+ links: [
146
+ {
147
+ rel: 'self',
148
+ uri: 'http://dev3.openmrs.org/openmrs/ws/rest/v1/workflow/70921392-4e3e-5465-978d-45b68b7def5f/state/7293cb90-c93f-4386-b32f-e8cfc633dc3e',
149
+ resourceAlias: 'state',
150
+ },
151
+ {
152
+ rel: 'full',
153
+ uri: 'http://dev3.openmrs.org/openmrs/ws/rest/v1/workflow/70921392-4e3e-5465-978d-45b68b7def5f/state/7293cb90-c93f-4386-b32f-e8cfc633dc3e?v=full',
154
+ resourceAlias: 'state',
155
+ },
156
+ ],
157
+ resourceVersion: '1.8',
158
+ },
159
+ {
160
+ uuid: 'c26a8cc7-fb07-4b2f-bdb0-730db9ce0020',
161
+ description: null,
162
+ retired: false,
163
+ concept: {
164
+ uuid: '4f2f8cd9-e57c-4bcd-822f-0dfabb684bc1',
165
+ display: 'Example Option 2',
166
+ name: {
167
+ display: 'Example Option 2',
168
+ uuid: 'd0934337-68bd-47ff-83f7-075c8d15f31c',
169
+ name: 'Example Option 2',
170
+ locale: 'en',
171
+ localePreferred: true,
172
+ conceptNameType: 'FULLY_SPECIFIED',
173
+ links: [],
174
+ resourceVersion: '1.9',
175
+ },
176
+ datatype: {
177
+ uuid: '8d4a4c94-c2cc-11de-8d13-0010c6dffd0f',
178
+ display: 'N/A',
179
+ links: [],
180
+ },
181
+ conceptClass: {
182
+ uuid: '8d492774-c2cc-11de-8d13-0010c6dffd0f',
183
+ display: 'Misc',
184
+ links: [],
185
+ },
186
+ set: false,
187
+ version: null,
188
+ retired: false,
189
+ names: [
190
+ {
191
+ uuid: 'd0934337-68bd-47ff-83f7-075c8d15f31c',
192
+ display: 'Example Option 2',
193
+ links: [],
194
+ },
195
+ ],
196
+ descriptions: [],
197
+ mappings: [],
198
+ answers: [],
199
+ setMembers: [],
200
+ attributes: [],
201
+ links: [],
202
+ resourceVersion: '2.0',
203
+ },
204
+ links: [],
205
+ resourceVersion: '1.8',
206
+ },
207
+ {
208
+ uuid: '7c0a5653-313f-4564-b9cf-d59adf1173dc',
209
+ description: null,
210
+ retired: false,
211
+ concept: {
212
+ uuid: '7dc37bb8-1725-11ed-861d-0242ac120002',
213
+ display: 'On Antiretrovirals Treatment',
214
+ name: {
215
+ display: 'On Antiretrovirals Treatment',
216
+ uuid: 'c90ed5f3-4c4d-303b-b8bd-d6b5d21c0f81',
217
+ name: 'On Antiretrovirals Treatment',
218
+ locale: 'en',
219
+ localePreferred: true,
220
+ conceptNameType: 'FULLY_SPECIFIED',
221
+ links: [],
222
+ resourceVersion: '1.9',
223
+ },
224
+ datatype: {
225
+ uuid: '8d4a4c94-c2cc-11de-8d13-0010c6dffd0f',
226
+ display: 'N/A',
227
+ links: [],
228
+ },
229
+ conceptClass: {
230
+ uuid: '8d492774-c2cc-11de-8d13-0010c6dffd0f',
231
+ display: 'Misc',
232
+ links: [],
233
+ },
234
+ set: false,
235
+ version: null,
236
+ retired: false,
237
+ names: [
238
+ {
239
+ uuid: 'c90ed5f3-4c4d-303b-b8bd-d6b5d21c0f81',
240
+ display: 'On Antiretrovirals Treatment',
241
+ links: [],
242
+ },
243
+ {
244
+ uuid: '3d61609a-30e1-3dd1-83a0-21310b06c388',
245
+ display: 'On Antiretrovirals',
246
+ links: [],
247
+ },
248
+ ],
249
+ descriptions: [
250
+ {
251
+ uuid: '07fc6455-9ffa-4848-a4bc-e58ffb91503a',
252
+ display: 'On Antiretrovirals Treatment program workflow state',
253
+ links: [],
254
+ },
255
+ ],
256
+ mappings: [
257
+ {
258
+ uuid: 'fffc1802-f832-49ef-bfaa-0ffe4bc7db0f',
259
+ display: 'SNOMED MVP: OATT',
260
+ links: [],
261
+ },
262
+ ],
263
+ answers: [],
264
+ setMembers: [],
265
+ attributes: [],
266
+ links: [],
267
+ resourceVersion: '2.0',
268
+ },
269
+ links: [],
270
+ resourceVersion: '1.8',
271
+ },
272
+ {
273
+ uuid: '29a513f0-2810-4356-98f5-42b12f7013a5',
274
+ description: null,
275
+ retired: false,
276
+ concept: {
277
+ uuid: 'e1ad9977-c106-4116-9f23-45258b7e306b',
278
+ display: 'Example Option 3',
279
+ name: {
280
+ display: 'Example Option 3',
281
+ uuid: '988af00b-6243-4fc2-9d81-718bc9bbe743',
282
+ name: 'Example Option 3',
283
+ locale: 'en',
284
+ localePreferred: true,
285
+ conceptNameType: 'FULLY_SPECIFIED',
286
+ links: [],
287
+ resourceVersion: '1.9',
288
+ },
289
+ datatype: {
290
+ uuid: '8d4a4c94-c2cc-11de-8d13-0010c6dffd0f',
291
+ display: 'N/A',
292
+ links: [],
293
+ },
294
+ conceptClass: {
295
+ uuid: '8d492774-c2cc-11de-8d13-0010c6dffd0f',
296
+ display: 'Misc',
297
+ links: [],
298
+ },
299
+ set: false,
300
+ version: null,
301
+ retired: false,
302
+ names: [
303
+ {
304
+ uuid: '988af00b-6243-4fc2-9d81-718bc9bbe743',
305
+ display: 'Example Option 3',
306
+ links: [
307
+ {
308
+ rel: 'self',
309
+ uri: 'http://dev3.openmrs.org/openmrs/ws/rest/v1/concept/e1ad9977-c106-4116-9f23-45258b7e306b/name/988af00b-6243-4fc2-9d81-718bc9bbe743',
310
+ resourceAlias: 'name',
311
+ },
312
+ ],
313
+ },
314
+ ],
315
+ descriptions: [],
316
+ mappings: [],
317
+ answers: [],
318
+ setMembers: [],
319
+ attributes: [],
320
+ links: [],
321
+ resourceVersion: '2.0',
322
+ },
323
+ links: [],
324
+ resourceVersion: '1.8',
325
+ },
326
+ ],
327
+ links: [],
328
+ resourceVersion: '1.8',
329
+ },
330
+ ],
331
+ },
332
+ dateEnrolled: '2024-09-23T09:31:51.000+0000',
333
+ dateCompleted: null,
334
+ location: null,
335
+ states: [
336
+ {
337
+ startDate: '2024-09-23T00:00:00.000+0000',
338
+ endDate: null,
339
+ state: {
340
+ uuid: '7293cb90-c93f-4386-b32f-e8cfc633dc3e',
341
+ name: null,
342
+ retired: false,
343
+ concept: {
344
+ uuid: 'acc6f157-c9a5-4690-b814-e653cbf80b4c',
345
+ },
346
+ programWorkflow: {
347
+ uuid: '70921392-4e3e-5465-978d-45b68b7def5f',
348
+ },
349
+ },
350
+ },
351
+ ],
352
+ },
353
+ ];
354
+
355
+ describe('ProgramStateAdapter', () => {
356
+ // new submission (enter mode)
357
+ it('should handle submission for a program state', () => {
358
+ const value = '7293cb90-c93f-4386-b32f-e8cfc633dc3e';
359
+ ProgramStateAdapter.transformFieldValue(field, value, formContext);
360
+ expect(field.meta.submission.newValue).toEqual({
361
+ state: value,
362
+ startDate: expect.any(String),
363
+ });
364
+ });
365
+
366
+ it('should return null if the new value is the same as the previous value', () => {
367
+ field.meta.previousValue = { uuid: '7293cb90-c93f-4386-b32f-e8cfc633dc3e' };
368
+ const value = '7293cb90-c93f-4386-b32f-e8cfc633dc3e';
369
+ const result = ProgramStateAdapter.transformFieldValue(field, value, formContext);
370
+ expect(result).toBeNull();
371
+ expect(field.meta.submission.newValue).toBeNull();
372
+ });
373
+
374
+ it('should return null if the new value is empty or null', () => {
375
+ const value = null;
376
+ const result = ProgramStateAdapter.transformFieldValue(field, value, formContext);
377
+ expect(result).toBeNull();
378
+ expect(field.meta.submission.newValue).toBeNull();
379
+ });
380
+
381
+ it('should get initial value for the program state', async () => {
382
+ formContext.customDependencies.patientPrograms.push(...patientPrograms);
383
+ const program = await ProgramStateAdapter.getInitialValue(field, null, formContext);
384
+ expect(program).toEqual('7293cb90-c93f-4386-b32f-e8cfc633dc3e');
385
+ });
386
+
387
+ it('should return null if no active state is found for the patient program', async () => {
388
+ formContext.customDependencies.patientPrograms = [
389
+ {
390
+ ...patientPrograms[0],
391
+ states: [],
392
+ },
393
+ ];
394
+ const p = await ProgramStateAdapter.getInitialValue(field, null, formContext);
395
+ expect(p).toBeNull();
396
+ });
397
+
398
+ it('should return null if no patient program matches the programUuid', async () => {
399
+ const fieldWithDifferentProgramUuid = {
400
+ ...field,
401
+ questionOptions: { ...field.questionOptions, programUuid: 'non-existing-uuid' },
402
+ };
403
+ const program = await ProgramStateAdapter.getInitialValue(fieldWithDifferentProgramUuid, null, formContext);
404
+ expect(program).toBeNull();
405
+ });
406
+
407
+ it('should return null for getPreviousValue', async () => {
408
+ const previousValue = await ProgramStateAdapter.getPreviousValue(field, null, formContext);
409
+ expect(previousValue).toBeNull();
410
+ });
411
+
412
+ it('should execute tearDown without issues', () => {
413
+ expect(() => ProgramStateAdapter.tearDown()).not.toThrow();
414
+ });
415
+ });