-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: guido <[email protected]>
- Loading branch information
Showing
3 changed files
with
64 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
16 changes: 16 additions & 0 deletions
16
tests/js/unit/components/__snapshots__/search-control.test.tsx.snap
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,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SearchControl renders the input outside the label if the id prop is not passed 1`] = ` | ||
<input | ||
type="search" | ||
value="" | ||
/> | ||
`; | ||
|
||
exports[`SearchControl renders the input within the label if the id prop is passed 1`] = ` | ||
<input | ||
id="search" | ||
type="search" | ||
value="" | ||
/> | ||
`; |
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,45 @@ | ||
// Create tests for SearchControl with the following criteria: | ||
// - The component renders without errors | ||
// - The component renders the input within the label if the id prop is passed | ||
// - The component renders the input outside the label if the id prop is not passed | ||
// - The component calls the onChange prop when the input value changes | ||
import React from 'react'; | ||
|
||
import { describe, it, expect, jest } from '@jest/globals'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { SearchControl } from '../../../../sources/js/src/components/search-control'; | ||
|
||
jest.mock('@wordpress/i18n', () => ({ | ||
__: (text: string) => text, | ||
})); | ||
|
||
describe('SearchControl', () => { | ||
it('renders the input within the label if the id prop is passed', () => { | ||
render(<SearchControl id="search" onChange={() => {}} />); | ||
expect(screen.getByLabelText('Search')).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders the input outside the label if the id prop is not passed', () => { | ||
const { container } = render( | ||
<SearchControl data-testid="search-input" onChange={() => {}} /> | ||
); | ||
expect(container.querySelector('[type="search"]')).toMatchSnapshot(); | ||
}); | ||
|
||
it('calls the onChange prop when the input value changes', async () => { | ||
const onChange = jest.fn(); | ||
|
||
const { container } = render(<SearchControl onChange={onChange} />); | ||
|
||
const input = container.querySelector( | ||
'[type="search"]' | ||
) as HTMLInputElement; | ||
|
||
await userEvent.type(input, 'test'); | ||
|
||
expect(input).toHaveValue('test'); | ||
}); | ||
}); |