/* eslint-disable jest/require-top-level-describe */ import React from 'react'; import { fireEvent, render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { SIZES } from '../../../helpers/constants/design-system'; import { TextFieldBase } from './text-field-base'; describe('TextFieldBase', () => { it('should render correctly', () => { const { getByRole } = render(); expect(getByRole('textbox')).toBeDefined(); }); it('should render and be able to input text', () => { const { getByTestId } = render( , ); const textFieldBase = getByTestId('text-field-base'); expect(textFieldBase.value).toBe(''); // initial value is empty string fireEvent.change(textFieldBase, { target: { value: 'text value' } }); expect(textFieldBase.value).toBe('text value'); fireEvent.change(textFieldBase, { target: { value: '' } }); // reset value expect(textFieldBase.value).toBe(''); // value is empty string after reset }); it('should render and fire onFocus and onBlur events', () => { const onFocus = jest.fn(); const onBlur = jest.fn(); const { getByTestId } = render( , ); const textFieldBase = getByTestId('text-field-base'); fireEvent.focus(textFieldBase); expect(onFocus).toHaveBeenCalledTimes(1); fireEvent.blur(textFieldBase); expect(onBlur).toHaveBeenCalledTimes(1); }); it('should render and fire onChange event', () => { const onChange = jest.fn(); const { getByTestId } = render( , ); const textFieldBase = getByTestId('text-field-base'); fireEvent.change(textFieldBase, { target: { value: 'text value' } }); expect(onChange).toHaveBeenCalledTimes(1); }); it('should render and fire onClick event', () => { const onClick = jest.fn(); const { getByTestId } = render( , ); const textFieldBase = getByTestId('text-field-base'); fireEvent.click(textFieldBase); expect(onClick).toHaveBeenCalledTimes(1); }); it('should render with different size classes', () => { const { getByTestId } = render( <> , ); expect(getByTestId('sm')).toHaveClass('mm-text-field-base--size-sm'); expect(getByTestId('md')).toHaveClass('mm-text-field-base--size-md'); expect(getByTestId('lg')).toHaveClass('mm-text-field-base--size-lg'); }); it('should render with different types', () => { const { getByTestId } = render( <> , ); expect(getByTestId('text-field-base-text')).toHaveAttribute('type', 'text'); expect(getByTestId('text-field-base-number')).toHaveAttribute( 'type', 'number', ); expect(getByTestId('text-field-base-password')).toHaveAttribute( 'type', 'password', ); }); it('should render with truncate class', () => { const { getByTestId } = render( , ); expect(getByTestId('truncate')).toHaveClass('mm-text-field-base--truncate'); }); it('should render with right and left accessories', () => { const { getByRole, getByText } = render( left accessory} rightAccessory={
right accessory
} />, ); expect(getByRole('textbox')).toBeDefined(); expect(getByText('left accessory')).toBeDefined(); expect(getByText('right accessory')).toBeDefined(); }); it('should render with working ref using inputRef prop', () => { // Because the 'ref' attribute wont flow down to the DOM // I'm not exactly sure how to test this? const mockRef = jest.fn(); const { getByRole } = render(); expect(getByRole('textbox')).toBeDefined(); expect(mockRef).toHaveBeenCalledTimes(1); }); it('should render with autoComplete', () => { const { getByTestId } = render( , ); expect(getByTestId('text-field-base-auto-complete')).toHaveAttribute( 'autocomplete', 'on', ); }); it('should render with autoFocus', () => { const { getByRole } = render(); expect(getByRole('textbox')).toHaveFocus(); }); it('should render with a defaultValue', () => { const { getByRole } = render( , ); expect(getByRole('textbox').value).toBe('default value'); }); it('should render in disabled state and not focus or be clickable', () => { const mockOnClick = jest.fn(); const mockOnFocus = jest.fn(); const { getByRole } = render( , ); getByRole('textbox').focus(); expect(getByRole('textbox')).toBeDisabled(); expect(mockOnClick).toHaveBeenCalledTimes(0); expect(mockOnFocus).toHaveBeenCalledTimes(0); }); it('should render with error className when error is true', () => { const { getByTestId } = render( , ); expect(getByTestId('text-field-base-error')).toHaveClass( 'mm-text-field-base--error', ); }); it('should render with maxLength and not allow more than the set characters', async () => { const { getByRole } = render(); const textFieldBase = getByRole('textbox'); await userEvent.type(textFieldBase, '1234567890'); expect(getByRole('textbox')).toBeDefined(); expect(textFieldBase.maxLength).toBe(5); expect(textFieldBase.value).toBe('12345'); expect(textFieldBase.value).toHaveLength(5); }); it('should render with readOnly attr when readOnly is true', () => { const { getByTestId } = render( , ); expect(getByTestId('text-field-base-readonly')).toHaveAttribute( 'readonly', '', ); }); it('should render with required attr when required is true', () => { const { getByTestId } = render( , ); expect(getByTestId('text-field-base-required')).toHaveAttribute( 'required', '', ); }); });