-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(forms): Handle error display when no focus and blur events is cal…
…led (#2737) Handle error display when no focus and blur events is called
- Loading branch information
Showing
3 changed files
with
105 additions
and
42 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
73 changes: 73 additions & 0 deletions
73
packages/dnb-eufemia/src/extensions/forms/hooks/__tests__/useDataValue.test.ts
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import useDataValue from '../useDataValue' | ||
|
||
describe('useDataValue', () => { | ||
it('should call external onChange based change callbacks', () => { | ||
const onChange = jest.fn() | ||
const { result } = renderHook(() => useDataValue({ onChange })) | ||
|
||
const { handleChange } = result.current | ||
|
||
act(() => { | ||
handleChange('new-value') | ||
}) | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
expect(onChange).toHaveBeenNthCalledWith(1, 'new-value') | ||
}) | ||
|
||
describe('using focus callbacks', () => { | ||
it('should return the error only when the value is invalid AND it is not in focus', () => { | ||
const { result } = renderHook(() => | ||
useDataValue({ | ||
value: 'foo', | ||
emptyValue: '', | ||
required: true, | ||
}) | ||
) | ||
|
||
const { handleFocus, handleBlur, handleChange } = result.current | ||
|
||
act(() => { | ||
handleFocus() | ||
handleChange('') | ||
}) | ||
expect(result.current.error).toBeUndefined() | ||
|
||
act(() => { | ||
handleBlur() | ||
}) | ||
expect(result.current.error).toBeInstanceOf(Error) | ||
|
||
act(() => { | ||
handleFocus() | ||
handleChange('a') | ||
handleBlur() | ||
}) | ||
expect(result.current.error).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('without using focus callbacks', () => { | ||
it('should return the error as long as the value is invalud', () => { | ||
const { result } = renderHook(() => | ||
useDataValue({ | ||
value: 'foo', | ||
emptyValue: '', | ||
required: true, | ||
}) | ||
) | ||
|
||
const { handleChange } = result.current | ||
|
||
act(() => { | ||
handleChange('') | ||
}) | ||
expect(result.current.error).toBeInstanceOf(Error) | ||
|
||
act(() => { | ||
handleChange('abc') | ||
}) | ||
expect(result.current.error).toBeUndefined() | ||
}) | ||
}) | ||
}) |
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