Skip to content

Commit f40926a

Browse files
committed
Add test for exposed "format()" handler
1 parent 917a7a1 commit f40926a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_form_data.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@ describe('useFormData() hook', () => {
9292
});
9393
});
9494

95+
describe('format form data', () => {
96+
let testBed: TestBed;
97+
let onChangeSpy: jest.Mock;
98+
99+
const getLastMockValue = () => {
100+
return onChangeSpy.mock.calls[onChangeSpy.mock.calls.length - 1][0] as HookReturn;
101+
};
102+
103+
const TestComp = (props: Props) => {
104+
const { form } = useForm();
105+
106+
return (
107+
<Form form={form}>
108+
<UseField path="user.firstName" defaultValue="John" />
109+
<UseField path="user.lastName" defaultValue="Snow" />
110+
<HookListenerComp {...props} />
111+
</Form>
112+
);
113+
};
114+
115+
const setup = registerTestBed(TestComp, {
116+
memoryRouter: { wrapComponent: false },
117+
});
118+
119+
beforeEach(() => {
120+
onChangeSpy = jest.fn();
121+
testBed = setup({ onChange: onChangeSpy }) as TestBed;
122+
});
123+
124+
test('should expose a handler to build the form data', () => {
125+
const [_, format] = getLastMockValue();
126+
expect(format()).toEqual({
127+
user: {
128+
firstName: 'John',
129+
lastName: 'Snow',
130+
},
131+
});
132+
});
133+
});
134+
95135
describe('options', () => {
96136
describe('watch', () => {
97137
let testBed: TestBed;

0 commit comments

Comments
 (0)