-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/form] Add all uncontrolled form tests
- Loading branch information
Showing
13 changed files
with
240 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { FormMode } from '../types'; | ||
import { useForm } from '../use-form'; | ||
|
||
describe('@mantine/form/setErrors', () => { | ||
function tests(mode: FormMode) { | ||
it('filters out errors with undefined and null with setErrors handler', () => { | ||
const hook = renderHook(() => useForm()); | ||
const hook = renderHook(() => useForm({ mode })); | ||
act(() => hook.result.current.setErrors({ a: 1, b: undefined, c: null, d: 2 })); | ||
expect(hook.result.current.errors).toStrictEqual({ a: 1, d: 2 }); | ||
}); | ||
} | ||
|
||
describe('@mantine/form/setErrors-controlled', () => { | ||
tests('controlled'); | ||
}); | ||
|
||
describe('@mantine/form/setErrors-uncontrolled', () => { | ||
tests('uncontrolled'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,66 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { FormMode } from '../types'; | ||
import { useForm } from '../use-form'; | ||
|
||
describe('@mantine/form/setFieldValue', () => { | ||
function tests(mode: FormMode) { | ||
it('sets given value with root path', () => { | ||
const hook = renderHook(() => useForm({ initialValues: { a: 1, b: 2 } })); | ||
const hook = renderHook(() => useForm({ mode, initialValues: { a: 1, b: 2 } })); | ||
|
||
act(() => hook.result.current.setFieldValue('a', 10)); | ||
expect(hook.result.current.values).toStrictEqual({ a: 10, b: 2 }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: 10, b: 2 }); | ||
|
||
act(() => hook.result.current.setFieldValue('b', 20)); | ||
expect(hook.result.current.values).toStrictEqual({ a: 10, b: 20 }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: 10, b: 20 }); | ||
}); | ||
|
||
it('sets given value at composite path', () => { | ||
const hook = renderHook(() => useForm({ initialValues: { a: { d: 2, b: { c: 1 } } } })); | ||
const hook = renderHook(() => useForm({ mode, initialValues: { a: { d: 2, b: { c: 1 } } } })); | ||
|
||
act(() => hook.result.current.setFieldValue('a.b.c', 10)); | ||
expect(hook.result.current.values).toStrictEqual({ a: { d: 2, b: { c: 10 } } }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: { d: 2, b: { c: 10 } } }); | ||
|
||
act(() => hook.result.current.setFieldValue('a.d', 20)); | ||
expect(hook.result.current.values).toStrictEqual({ a: { d: 20, b: { c: 10 } } }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: { d: 20, b: { c: 10 } } }); | ||
}); | ||
|
||
it('sets given value at array path', () => { | ||
const hook = renderHook(() => useForm({ initialValues: { a: [{ b: 1 }, { b: 2 }] } })); | ||
const hook = renderHook(() => useForm({ mode, initialValues: { a: [{ b: 1 }, { b: 2 }] } })); | ||
|
||
act(() => hook.result.current.setFieldValue('a.1.b', 20)); | ||
expect(hook.result.current.values).toStrictEqual({ a: [{ b: 1 }, { b: 20 }] }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: [{ b: 1 }, { b: 20 }] }); | ||
|
||
act(() => hook.result.current.setFieldValue('a.0.b', 10)); | ||
expect(hook.result.current.values).toStrictEqual({ a: [{ b: 10 }, { b: 20 }] }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: [{ b: 10 }, { b: 20 }] }); | ||
}); | ||
|
||
it('sets given value at mixed nested path', () => { | ||
const hook = renderHook(() => | ||
useForm({ initialValues: { a: [{ b: { c: 1 } }, { b: { c: 2 } }] } }) | ||
useForm({ mode, initialValues: { a: [{ b: { c: 1 } }, { b: { c: 2 } }] } }) | ||
); | ||
|
||
act(() => hook.result.current.setFieldValue('a.1.b.c', 20)); | ||
expect(hook.result.current.values).toStrictEqual({ a: [{ b: { c: 1 } }, { b: { c: 20 } }] }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ | ||
a: [{ b: { c: 1 } }, { b: { c: 20 } }], | ||
}); | ||
|
||
act(() => hook.result.current.setFieldValue('a.0.b.c', 10)); | ||
expect(hook.result.current.values).toStrictEqual({ a: [{ b: { c: 10 } }, { b: { c: 20 } }] }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ | ||
a: [{ b: { c: 10 } }, { b: { c: 20 } }], | ||
}); | ||
}); | ||
|
||
it('sets value at path based on previous value', () => { | ||
const hook = renderHook(() => useForm({ initialValues: { a: { d: 2, b: { c: 1 } } } })); | ||
const hook = renderHook(() => useForm({ mode, initialValues: { a: { d: 2, b: { c: 1 } } } })); | ||
|
||
act(() => hook.result.current.setFieldValue('a.b.c', (prev) => prev + 1)); | ||
expect(hook.result.current.values).toStrictEqual({ a: { d: 2, b: { c: 2 } } }); | ||
expect(hook.result.current.getValues()).toStrictEqual({ a: { d: 2, b: { c: 2 } } }); | ||
}); | ||
} | ||
|
||
describe('@mantine/form/setFieldValue-controlled', () => { | ||
tests('controlled'); | ||
}); | ||
|
||
describe('@mantine/form/setFieldValue-uncontrolled', () => { | ||
tests('uncontrolled'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.