generated from nimblehq/git-template
-
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.
[#7] Add some tests for Input componenet and clean up
- Loading branch information
1 parent
4b9fe58
commit b150416
Showing
3 changed files
with
47 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import Input from '.'; | ||
|
||
const handleInputChange = jest.fn(); | ||
|
||
describe('Input', () => { | ||
test('renders an Input of type Text on the page with the correct class', () => { | ||
render(<Input name="email" label="email" type="text" className="input-class" onInputChange={handleInputChange} />); | ||
|
||
const inputComponent = screen.getByRole('textbox', { name: 'email' }); | ||
|
||
expect(inputComponent).toBeInTheDocument(); | ||
expect(inputComponent).toHaveAttribute('type', 'text'); | ||
expect(inputComponent).toHaveClass('input-class'); | ||
}); | ||
|
||
test('renders an Input of type Text on the page with a label element', () => { | ||
render(<Input name="email" label="email-label" type="text" onInputChange={handleInputChange} />); | ||
|
||
const inputComponent = screen.getByRole('textbox', { name: 'email-label' }); | ||
const labelElement = screen.getByLabelText('email-label'); | ||
|
||
expect(inputComponent).toBeInTheDocument(); | ||
expect(labelElement).toBeInTheDocument(); | ||
expect(inputComponent).toHaveAttribute('type', 'text'); | ||
}); | ||
|
||
test('when text has changed, calls onInputChange handler', async () => { | ||
render(<Input name="email" label="email-label" type="text" className="input-class" onInputChange={handleInputChange} />); | ||
|
||
const user = userEvent.setup(); | ||
|
||
const inputComponent = screen.getByRole('textbox', { name: 'email-label' }); | ||
|
||
await user.type(inputComponent, 'test'); | ||
|
||
expect(handleInputChange).toBeCalledTimes(4); | ||
}); | ||
}); |
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