This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom validation messages using the field name/label (#8143)
* Custom validation strings using a new function named getValidityMessageForInput * getValidityMessageForInput tests * Added integration test for error message * Clear value * update test strings
- Loading branch information
1 parent
80c6b26
commit 2ea96ed
Showing
6 changed files
with
146 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { act, render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getValidityMessageForInput } from '../index'; | ||
|
||
describe( 'getValidityMessageForInput', () => { | ||
it( 'Returns nothing if the input is valid', async () => { | ||
render( <input type="text" data-testid="custom-input" /> ); | ||
|
||
const textInputElement = ( await screen.getByTestId( | ||
'custom-input' | ||
) ) as HTMLInputElement; | ||
|
||
const validityMessage = getValidityMessageForInput( | ||
'Test', | ||
textInputElement | ||
); | ||
expect( validityMessage ).toBe( '' ); | ||
} ); | ||
it( 'Returns error message if a required input is empty', async () => { | ||
render( <input type="text" required data-testid="custom-input" /> ); | ||
|
||
const textInputElement = ( await screen.getByTestId( | ||
'custom-input' | ||
) ) as HTMLInputElement; | ||
|
||
const validityMessage = getValidityMessageForInput( | ||
'Test', | ||
textInputElement | ||
); | ||
|
||
expect( validityMessage ).toBe( 'Please enter a valid test' ); | ||
} ); | ||
it( 'Returns a custom error if set, rather than a new message', async () => { | ||
render( | ||
<input | ||
type="text" | ||
required | ||
onChange={ ( event ) => { | ||
event.target.setCustomValidity( 'Custom error' ); | ||
} } | ||
data-testid="custom-input" | ||
/> | ||
); | ||
|
||
const textInputElement = ( await screen.getByTestId( | ||
'custom-input' | ||
) ) as HTMLInputElement; | ||
|
||
await act( async () => { | ||
await userEvent.type( textInputElement, 'Invalid Value' ); | ||
} ); | ||
|
||
const validityMessage = getValidityMessageForInput( | ||
'Test', | ||
textInputElement | ||
); | ||
expect( validityMessage ).toBe( 'Custom error' ); | ||
} ); | ||
} ); |
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