-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SearchControl: Add unit tests (#58693)
Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: ciampo <mciampini@git.wordpress.org>
- Loading branch information
1 parent
6d97038
commit 7d75052
Showing
1 changed file
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen } from '@testing-library/react'; | ||
import { click, type } from '@ariakit/test'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SearchControl from '..'; | ||
|
||
function ControlledSearchControl( { | ||
onChange, | ||
...restProps | ||
}: React.ComponentProps< typeof SearchControl > ) { | ||
const [ value, setValue ] = useState( '' ); | ||
|
||
return ( | ||
<SearchControl | ||
{ ...restProps } | ||
value={ value } | ||
onChange={ ( ...args ) => { | ||
setValue( ...args ); | ||
onChange( ...args ); | ||
} } | ||
/> | ||
); | ||
} | ||
|
||
describe( 'SearchControl', () => { | ||
describe.each( [ | ||
// TODO: Uncontrolled mode is not supported yet. | ||
// [ 'Uncontrolled', SearchControl ], | ||
[ 'Controlled mode', ControlledSearchControl ], | ||
] )( '%s', ( ...modeAndComponent ) => { | ||
const [ , Component ] = modeAndComponent; | ||
|
||
it( 'should call onChange with input value when value is changed', async () => { | ||
const onChangeSpy = jest.fn(); | ||
render( <Component onChange={ onChangeSpy } /> ); | ||
|
||
const searchInput = screen.getByRole( 'searchbox' ); | ||
await type( 'test', searchInput ); | ||
expect( searchInput ).toHaveValue( 'test' ); | ||
expect( onChangeSpy ).toHaveBeenLastCalledWith( 'test' ); | ||
} ); | ||
|
||
it( 'should render a Reset search button if no onClose function is provided', async () => { | ||
const onChangeSpy = jest.fn(); | ||
render( <Component onChange={ onChangeSpy } /> ); | ||
|
||
const searchInput = screen.getByRole( 'searchbox' ); | ||
|
||
expect( | ||
screen.queryByRole( 'button', { name: 'Reset search' } ) | ||
).not.toBeInTheDocument(); | ||
|
||
await type( 'test', searchInput ); | ||
const resetButton = screen.getByRole( 'button', { | ||
name: 'Reset search', | ||
} ); | ||
expect( resetButton ).toBeVisible(); | ||
|
||
await click( resetButton ); | ||
expect( searchInput ).toHaveValue( '' ); | ||
expect( onChangeSpy ).toHaveBeenLastCalledWith( '' ); | ||
} ); | ||
|
||
it( 'should should render a Close button (instead of Reset) when onClose function is provided', async () => { | ||
const onChangeSpy = jest.fn(); | ||
const onCloseSpy = jest.fn(); | ||
render( | ||
<Component onChange={ onChangeSpy } onClose={ onCloseSpy } /> | ||
); | ||
|
||
expect( | ||
screen.queryByRole( 'button', { name: 'Close search' } ) | ||
).toBeVisible(); | ||
expect( | ||
screen.queryByRole( 'button', { name: 'Reset search' } ) | ||
).not.toBeInTheDocument(); | ||
|
||
const searchInput = screen.getByRole( 'searchbox' ); | ||
await type( 'test', searchInput ); | ||
|
||
expect( | ||
screen.queryByRole( 'button', { name: 'Close search' } ) | ||
).toBeVisible(); | ||
expect( | ||
screen.queryByRole( 'button', { name: 'Reset search' } ) | ||
).not.toBeInTheDocument(); | ||
expect( onChangeSpy ).toHaveBeenCalledTimes( 'test'.length ); | ||
|
||
await click( | ||
screen.getByRole( 'button', { name: 'Close search' } ) | ||
); | ||
expect( onCloseSpy ).toHaveBeenCalledTimes( 1 ); | ||
expect( searchInput ).toHaveValue( 'test' ); // no change | ||
expect( onChangeSpy ).toHaveBeenCalledTimes( 'test'.length ); // no change | ||
} ); | ||
} ); | ||
} ); |
7d75052
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 7d75052.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7800400564
📝 Reported issues:
/test/e2e/specs/interactivity/directive-bind.spec.ts