@openmrs/esm-form-engine-lib 2.1.0-pre.1402 → 2.1.0-pre.1404
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/12ea0b64ac1d74d9/12ea0b64ac1d74d9.gz +0 -0
- package/{cf9b433cc012cb49/cf9b433cc012cb49.gz → ff6bbdac1639f6d1/ff6bbdac1639f6d1.gz} +0 -0
- package/package.json +1 -1
- package/src/hooks/useInitialValues.test.ts +76 -0
- package/src/hooks/useInitialValues.ts +1 -0
- package/d9d8dc63b0ddd5c9/d9d8dc63b0ddd5c9.gz +0 -0
- /package/{0a00cb94b7e3c7b1/0a00cb94b7e3c7b1.gz → b39f834950646b97/b39f834950646b97.gz} +0 -0
- /package/{75691be0a762fc30/75691be0a762fc30.gz → f7899165a77a3934/f7899165a77a3934.gz} +0 -0
Binary file
|
Binary file
|
package/package.json
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
import { act, renderHook, waitFor } from '@testing-library/react';
|
2
|
+
|
3
|
+
import useInitialValues from './useInitialValues';
|
4
|
+
import { type FormField } from '../types';
|
5
|
+
|
6
|
+
export const mockFormFields: FormField[] = [
|
7
|
+
{
|
8
|
+
label: 'First Name',
|
9
|
+
type: 'text',
|
10
|
+
id: 'firstName',
|
11
|
+
questionOptions: {
|
12
|
+
rendering: 'text',
|
13
|
+
},
|
14
|
+
value: '',
|
15
|
+
isRequired: true,
|
16
|
+
validators: [{ required: true }],
|
17
|
+
},
|
18
|
+
{
|
19
|
+
label: 'Date of Birth',
|
20
|
+
type: 'date',
|
21
|
+
questionOptions: {
|
22
|
+
rendering: 'date',
|
23
|
+
},
|
24
|
+
datePickerFormat: 'both',
|
25
|
+
id: 'dob',
|
26
|
+
value: null,
|
27
|
+
isRequired: true,
|
28
|
+
validators: [{ required: true }],
|
29
|
+
},
|
30
|
+
];
|
31
|
+
|
32
|
+
describe('useInitialValues', () => {
|
33
|
+
let formProcessor;
|
34
|
+
let context;
|
35
|
+
|
36
|
+
beforeEach(() => {
|
37
|
+
formProcessor = {
|
38
|
+
getInitialValues: jest.fn(),
|
39
|
+
};
|
40
|
+
context = {
|
41
|
+
formFields: mockFormFields,
|
42
|
+
formFieldAdapters: { adapter1: {}, adapter2: {} },
|
43
|
+
};
|
44
|
+
});
|
45
|
+
|
46
|
+
it('should not call getInitialValues if context dependencies are not loaded', () => {
|
47
|
+
const { result } = renderHook(() => useInitialValues(formProcessor, true, context));
|
48
|
+
|
49
|
+
expect(result.current.isLoadingInitialValues).toBe(true);
|
50
|
+
expect(formProcessor.getInitialValues).not.toHaveBeenCalled();
|
51
|
+
});
|
52
|
+
|
53
|
+
it('should set error if getInitialValues rejects', async () => {
|
54
|
+
const mockError = new Error('Failed to fetch initial values');
|
55
|
+
formProcessor.getInitialValues.mockRejectedValue(mockError);
|
56
|
+
|
57
|
+
const { result } = await act(() => renderHook(() => useInitialValues(formProcessor, false, context)));
|
58
|
+
|
59
|
+
expect(result.current.error).toEqual(mockError);
|
60
|
+
expect(result.current.isLoadingInitialValues).toBe(false);
|
61
|
+
});
|
62
|
+
|
63
|
+
it('should set initial values when dependencies are loaded', async () => {
|
64
|
+
const mockInitialValues = { firstName: 'John Doe', dob: '1992-10-10' };
|
65
|
+
formProcessor.getInitialValues.mockResolvedValue(mockInitialValues);
|
66
|
+
|
67
|
+
const { result } = renderHook(() => useInitialValues(formProcessor, false, context));
|
68
|
+
|
69
|
+
await waitFor(() => expect(result.current.isLoadingInitialValues).toBe(true));
|
70
|
+
|
71
|
+
expect(formProcessor.getInitialValues).toHaveBeenCalledWith(context);
|
72
|
+
expect(result.current.initialValues).toEqual(mockInitialValues);
|
73
|
+
expect(result.current.isLoadingInitialValues).toBe(false);
|
74
|
+
expect(result.current.error).toBe(null);
|
75
|
+
});
|
76
|
+
});
|
Binary file
|
File without changes
|
File without changes
|