-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: create unit test for UI input components
1 parent
fe84d9b
commit 8d21645
Showing
3 changed files
with
81 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,20 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import { MaskedInput } from './input-masked' | ||
|
||
describe('MaskedInput', () => { | ||
it('renders without errors', () => { | ||
render(<MaskedInput />) | ||
expect(screen.getByRole('textbox')).toBeInTheDocument() | ||
}) | ||
it('renders with custom class name', () => { | ||
render(<MaskedInput className="custom-input" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('custom-input') | ||
}) | ||
it('forwards ref to the input element', () => { | ||
const ref = React.createRef<HTMLInputElement>() | ||
render(<MaskedInput ref={ref} />) | ||
expect(ref.current).toBeInstanceOf(HTMLInputElement) | ||
}) | ||
}) |
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,30 @@ | ||
import '@testing-library/jest-dom' | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import { Input } from './input' | ||
|
||
describe('Input', () => { | ||
test('renders without errors', () => { | ||
render(<Input />) | ||
expect(screen.getByRole('textbox')).toBeInTheDocument() | ||
}) | ||
|
||
test('renders with custom class name', () => { | ||
render(<Input className="custom-input" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('custom-input') | ||
}) | ||
|
||
test('forwards ref to the input element', () => { | ||
const ref = React.createRef<HTMLInputElement>() | ||
render(<Input ref={ref} />) | ||
expect(ref.current).toBeInstanceOf(HTMLInputElement) | ||
}) | ||
|
||
test('triggers onChange event', () => { | ||
const handleChange = jest.fn() | ||
render(<Input onChange={handleChange} />) | ||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test' } }) | ||
expect(handleChange).toHaveBeenCalledTimes(1) | ||
expect(handleChange).toHaveBeenCalledWith(expect.any(Object)) | ||
}) | ||
}) |
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,31 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { Textarea } from './textarea' | ||
|
||
describe('Textarea', () => { | ||
test('renders without errors', () => { | ||
render(<Textarea />) | ||
const textareaElement = screen.getByRole('textbox') | ||
expect(textareaElement).toBeInTheDocument() | ||
}) | ||
|
||
test('renders with initial value', () => { | ||
const initialValue = 'Hello World' | ||
render(<Textarea value={initialValue} />) | ||
const textareaElement = screen.getByRole('textbox') as HTMLTextAreaElement | ||
expect(textareaElement.value).toBe(initialValue) | ||
}) | ||
test('renders with placeholder', () => { | ||
const placeholder = 'Enter your text' | ||
render(<Textarea placeholder={placeholder} />) | ||
const textareaElement = screen.getByRole('textbox') as HTMLTextAreaElement | ||
expect(textareaElement.placeholder).toBe(placeholder) | ||
}) | ||
|
||
test('renders with custom class', () => { | ||
const customClass = 'custom-textarea' | ||
render(<Textarea className={customClass} />) | ||
const textareaElement = screen.getByRole('textbox') | ||
expect(textareaElement).toHaveClass(customClass) | ||
}) | ||
}) |