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

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.1464",
3
+ "version": "2.1.0-pre.1466",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
package/src/api/index.ts CHANGED
@@ -106,9 +106,13 @@ export async function fetchOpenMRSForm(nameOrUUID: string): Promise<OpenmrsForm
106
106
  return openmrsFormResponse;
107
107
  }
108
108
 
109
- return openmrsFormResponse.results?.length
110
- ? openmrsFormResponse.results.find((form) => form.retired === false)
111
- : new Error(`Form with ${nameOrUUID} was not found`);
109
+ if (openmrsFormResponse.results?.length) {
110
+ const form = openmrsFormResponse.results.find((form) => form.retired === false);
111
+ if (form) {
112
+ return form;
113
+ }
114
+ }
115
+ throw new Error(`Form with ID "${nameOrUUID}" was not found`);
112
116
  }
113
117
 
114
118
  /**
@@ -121,7 +125,7 @@ export async function fetchClobData(form: OpenmrsForm): Promise<any | null> {
121
125
  return null;
122
126
  }
123
127
 
124
- const jsonSchemaResource = form.resources.find(({ name }) => name === 'JSON schema');
128
+ const jsonSchemaResource = form.resources?.find(({ name }) => name === 'JSON schema');
125
129
  if (!jsonSchemaResource) {
126
130
  return null;
127
131
  }
@@ -36,6 +36,7 @@ const COMPONENT_ART_SCHEMA_VALUE_REF = '74d06044-850f-11ee-b9d1-0242ac120003';
36
36
  const COMPONENT_PRECLINIC_REVIEW = 'component_preclinic-review';
37
37
  const COMPONENT_PRECLINIC_REVIEW_UUID = '2f063f32-7f8a-11ee-b962-0242ac120004';
38
38
  const COMPONENT_PRECLINIC_REVIEW_SCHEMA_VALUE_REF = '74d06044-850f-11ee-b9d1-0242ac120004';
39
+ const NON_EXISTENT_FORM_NAME = 'non-existent-form';
39
40
 
40
41
  // Base setup
41
42
  const mockOpenmrsFetch = openmrsFetch as jest.Mock;
@@ -74,6 +75,7 @@ when(mockOpenmrsFetch)
74
75
  when(mockOpenmrsFetch)
75
76
  .calledWith(buildPath(COMPONENT_ART))
76
77
  .mockResolvedValue({ data: { results: [artComponentSkeleton] } });
78
+
77
79
  when(mockOpenmrsFetch).calledWith(buildPath(COMPONENT_ART_UUID)).mockResolvedValue({ data: artComponentSkeleton });
78
80
  when(mockOpenmrsFetch)
79
81
  .calledWith(buildPath(COMPONENT_ART_SCHEMA_VALUE_REF))
@@ -82,6 +84,7 @@ when(mockOpenmrsFetch)
82
84
  when(mockOpenmrsFetch)
83
85
  .calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW))
84
86
  .mockResolvedValue({ data: { results: [preclinicReviewComponentSkeleton] } });
87
+
85
88
  when(mockOpenmrsFetch)
86
89
  .calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW_UUID))
87
90
  .mockResolvedValue({ data: preclinicReviewComponentSkeleton });
@@ -89,6 +92,10 @@ when(mockOpenmrsFetch)
89
92
  .calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW_SCHEMA_VALUE_REF))
90
93
  .mockResolvedValue({ data: preclinicReviewComponentBody });
91
94
 
95
+ when(mockOpenmrsFetch)
96
+ .calledWith(buildPath(NON_EXISTENT_FORM_NAME))
97
+ .mockResolvedValue({ data: { results: [] } });
98
+
92
99
  describe('useFormJson', () => {
93
100
  it('should fetch basic form by name', async () => {
94
101
  let hook = null;
@@ -97,7 +104,7 @@ describe('useFormJson', () => {
97
104
  });
98
105
 
99
106
  expect(hook.result.current.isLoading).toBe(false);
100
- expect(hook.result.current.error).toBe(undefined);
107
+ expect(hook.result.current.formError).toBe(undefined);
101
108
  expect(hook.result.current.formJson.name).toBe(MINI_FORM_NAME);
102
109
  });
103
110
 
@@ -108,7 +115,7 @@ describe('useFormJson', () => {
108
115
  });
109
116
 
110
117
  expect(hook.result.current.isLoading).toBe(false);
111
- expect(hook.result.current.error).toBe(undefined);
118
+ expect(hook.result.current.formError).toBe(undefined);
112
119
  expect(hook.result.current.formJson.name).toBe(MINI_FORM_NAME);
113
120
  });
114
121
 
@@ -119,7 +126,7 @@ describe('useFormJson', () => {
119
126
  });
120
127
 
121
128
  expect(hook.result.current.isLoading).toBe(false);
122
- expect(hook.result.current.error).toBe(undefined);
129
+ expect(hook.result.current.formError).toBe(undefined);
123
130
  expect(hook.result.current.formJson.name).toBe(PARENT_FORM_NAME);
124
131
 
125
132
  // verify subforms
@@ -133,7 +140,7 @@ describe('useFormJson', () => {
133
140
  });
134
141
 
135
142
  expect(hook.result.current.isLoading).toBe(false);
136
- expect(hook.result.current.error).toBe(undefined);
143
+ expect(hook.result.current.formError).toBe(undefined);
137
144
  expect(hook.result.current.formJson.name).toBe(PARENT_FORM_NAME);
138
145
 
139
146
  // verify subforms
@@ -146,12 +153,26 @@ describe('useFormJson', () => {
146
153
  hook = renderHook(() => useFormJson(null, formComponentBody, null, null));
147
154
  });
148
155
  expect(hook.result.current.isLoading).toBe(false);
149
- expect(hook.result.current.error).toBe(undefined);
156
+ expect(hook.result.current.formError).toBe(undefined);
150
157
  expect(hook.result.current.formJson.name).toBe(COMPONENT_FORM_NAME);
151
158
 
152
159
  // verify form components have been loaded
153
160
  verifyFormComponents(hook.result.current.formJson);
154
161
  });
162
+
163
+ it('should return an error when the form is not found', async () => {
164
+ // setup and execute
165
+ let hook = null;
166
+ await act(async () => {
167
+ hook = renderHook(() => useFormJson(NON_EXISTENT_FORM_NAME, null, null, null));
168
+ });
169
+ // verify
170
+ expect(hook.result.current.isLoading).toBe(false);
171
+ expect(hook.result.current.formError.message).toBe(
172
+ 'Error loading form JSON: Form with ID "non-existent-form" was not found',
173
+ );
174
+ expect(hook.result.current.formJson).toBe(null);
175
+ });
155
176
  });
156
177
 
157
178
  function buildPath(path: string) {
@@ -29,7 +29,7 @@ export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: s
29
29
 
30
30
  return {
31
31
  formJson,
32
- isLoading: !formJson,
32
+ isLoading: !formJson && !error,
33
33
  formError: error,
34
34
  };
35
35
  }