Skip to content

Commit

Permalink
useForm - partially refactored to switch from useRef to useState. Len…
Browse files Browse the repository at this point in the history
…ses are still an issue.
  • Loading branch information
Yakov Zhmurov committed Dec 6, 2024
1 parent ccc9033 commit eb3970f
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 133 deletions.
53 changes: 53 additions & 0 deletions uui-core/src/data/forms/__tests__/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,59 @@ describe('useForm', () => {
expect(result.current.isInvalid).toBe(false);
});

it('Should update form value with lens.update (2 immediate updates)', async () => {
const { result } = await renderHookWithContextAsync<UseFormProps<number>, IFormApi<number>>(() =>
useForm({
onSave: () => Promise.resolve(),
onError: () => Promise.resolve(),
value: 1,
}));

act(() => {
result.current.lens.update((x) => x + 1);
result.current.lens.update((x) => x + 1);
});
expect(result.current.value).toBe(3);
expect(result.current.isChanged).toBe(true);
expect(result.current.isInvalid).toBe(false);
});

it('Should get actual form value form value w/o re-render.', async () => {
const { result } = await renderHookWithContextAsync<UseFormProps<number>, IFormApi<number>>(() =>
useForm({
onSave: () => Promise.resolve(),
onError: () => Promise.resolve(),
value: 1,
}));

act(() => {
result.current.lens.set(2);
expect(result.current.lens.get()).toBe(2);
result.current.lens.set(3);
});
expect(result.current.value).toBe(3);
expect(result.current.isChanged).toBe(true);
expect(result.current.isInvalid).toBe(false);
});

it('Should update via lens.toProps()', async () => {
const { result } = await renderHookWithContextAsync<UseFormProps<number>, IFormApi<number>>(() =>
useForm({
onSave: () => Promise.resolve(),
onError: () => Promise.resolve(),
value: 1,
}));

act(() => {
const props = result.current.lens.toProps();
props.onValueChange(2);
props.onValueChange(3);
});
expect(result.current.value).toBe(3);
expect(result.current.isChanged).toBe(true);
expect(result.current.isInvalid).toBe(false);
});

it('should update form value by external props.value change', async () => {
const { result, rerender } = await renderHookWithContextAsync<UseFormProps<number>, IFormApi<number>>((props) => useForm(props), {
onSave: () => Promise.resolve(),
Expand Down
Loading

0 comments on commit eb3970f

Please sign in to comment.