import { h } from 'preact'; import TextField from '../TextField'; import { render, screen, fireEvent } from 'testing-library'; describe('TextField', () => { test('can render a leading icon', async () => { render(); expect(screen.getByTestId('icon-leading')).toBeInTheDocument(); }); test('can render a trailing icon', async () => { render(); expect(screen.getByTestId('icon-trailing')).toBeInTheDocument(); }); test('can renders icons in correct positions', async () => { render(); const icons = screen.queryAllByTestId(/icon-.+/); expect(icons[0]).toHaveAttribute('data-testid', 'icon-leading'); expect(icons[1]).toHaveAttribute('data-testid', 'icon-trailing'); }); test('onChange updates the value', async () => { const handleChangeText = jest.fn(); render(); const input = screen.getByRole('textbox'); fireEvent.input(input, { target: { value: 'i like tacos' } }); expect(handleChangeText).toHaveBeenCalledWith('i like tacos'); expect(input.value).toEqual('i like tacos'); }); test('still updates the value if an original value was given', async () => { render(); const input = screen.getByRole('textbox'); fireEvent.input(input, { target: { value: 'i like tacos' } }); expect(input.value).toEqual('i like tacos'); }); test('changes the value if the prop value changes', async () => { const { rerender } = render(); const input = screen.getByRole('textbox'); fireEvent.input(input, { target: { value: 'i like tacos' } }); expect(input.value).toEqual('i like tacos'); rerender(); expect(input.value).toEqual('no, really, burritos'); }); }); function FakeLeadingIcon() { return
; } function FakeTrailingIcon() { return
; }