fully test text input component

This commit is contained in:
Moriz Wahl 2025-02-03 22:40:06 +01:00
parent 3a84b6db07
commit 22258a62ca
2 changed files with 47 additions and 3 deletions

View File

@ -1,11 +1,32 @@
import { render } from '@testing-library/react'
import { describe, it, expect } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { TextInput } from './TextInput'
describe('<TextInput />', () => {
let wrapper = render(<TextInput />)
beforeEach(() => {
wrapper = render(<TextInput />)
})
it('renders properly', () => {
const wrapper = render(<TextInput />)
expect(wrapper.container.firstChild).toMatchSnapshot()
})
describe('handleChange', () => {
it('calls updateFormValue with new value', () => {
const updateFormValue = vi.fn()
wrapper.rerender(<TextInput updateFormValue={updateFormValue} />)
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test' } })
expect(updateFormValue).toBeCalledWith('test')
})
})
describe('labelTitle', () => {
it('sets label', () => {
wrapper.rerender(<TextInput labelTitle='My Title' />)
expect(wrapper.container.firstChild).toMatchSnapshot()
})
})
})

View File

@ -1,5 +1,28 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`<TextInput /> > labelTitle > sets label 1`] = `
<div
class="tw-form-control undefined"
>
<label
class="tw-label"
>
<span
class="tw-label-text tw-text-base-content undefined"
>
My Title
</span>
</label>
<input
class="tw-input tw-input-bordered tw-w-full "
placeholder=""
required=""
type="text"
value=""
/>
</div>
`;
exports[`<TextInput /> > renders properly 1`] = `
<div
class="tw-form-control undefined"