import React from 'react'; import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Input } from './'; describe('Input component', () => { describe('given default props', () => { it('supports several listener', async () => { const value = 'abc'; const onChange = vi.fn(); const onFocus = vi.fn(); const onBlur = vi.fn(); const onKeyDown = vi.fn(); render( , ); expect(screen.getByRole('textbox')).toBeVisible(); expect(screen.getByRole('textbox')).toHaveValue(value); expect(onChange).not.toHaveBeenCalled(); expect(onFocus).not.toHaveBeenCalled(); await userEvent.type(screen.getByRole('textbox'), 'def'); expect(onFocus).toHaveBeenCalledTimes(1); // User typed 3 letters, so 3 times onChange and onKeyDown expect(onChange).toHaveBeenCalledTimes(3); expect(onKeyDown).toHaveBeenCalledTimes(3); // Unfocus input await userEvent.tab(); expect(onBlur).toHaveBeenCalledTimes(1); }); }); describe('given HTML attributs', () => { it('sets them to the input', () => { const id = 'my-input'; const name = 'my-input'; const placeholder = 'Insert something here'; render( , ); expect(screen.getByRole('textbox')).toHaveAttribute('id', id); expect(screen.getByRole('textbox')).toHaveAttribute('name', name); expect(screen.getByRole('textbox')).toHaveAttribute( 'placeholder', placeholder, ); }); }); describe('given a `isDisabled` props', () => { it('disables the input when set to `true`', () => { render(); expect(screen.getByRole('textbox')).toBeDisabled(); }); }); describe('given a `isInvalid` props', () => { it('invalidates the input when set to `true`', () => { render(
Invalid email
My label
>, ); expect(screen.getByRole('textbox', { name: 'My label' })).toBeVisible(); }); it('supports aria-label', () => { render(); expect(screen.getByRole('textbox', { name: 'My label' })).toBeVisible(); }); }); describe('given a `maskOptions` props', () => { it('uses Cleave input to format value', () => { render( , ); expect(screen.getByRole('textbox')).toHaveValue('4444 3333 2222 1111'); }); }); });