@ -1,25 +1,45 @@
/* eslint-disable jest/require-top-level-describe */
/* eslint-disable jest/require-top-level-describe */
import React from 'react' ;
import React , { useState } from 'react' ;
import { fireEvent , render } from '@testing-library/react' ;
import { fireEvent , render } from '@testing-library/react' ;
import userEvent from '@testing-library/user-event' ;
import { TextField } from './text-field' ;
import { TextField } from './text-field' ;
// userEvent setup function as per testing-library docs
// https://testing-library.com/docs/user-event/intr
function setup ( jsx ) {
return {
user : userEvent . setup ( ) ,
... render ( jsx ) ,
} ;
}
// Custom userEvent setup function that renders the component in a controlled environment.
// This is used for the showClearButton and related props as the clearButton will only show in a controlled environment.
function setupControlled ( FormComponent , props ) {
const ControlledWrapper = ( ) => {
const [ value , setValue ] = useState ( '' ) ;
return (
< FormComponent
value = { value }
onChange = { ( e ) => setValue ( e . target . value ) }
{ ... props }
/ >
) ;
} ;
return { user : userEvent . setup ( ) , ... render ( < ControlledWrapper / > ) } ;
}
describe ( 'TextField' , ( ) => {
describe ( 'TextField' , ( ) => {
it ( 'should render correctly' , ( ) => {
it ( 'should render correctly' , ( ) => {
const { getByRole } = render ( < TextField / > ) ;
const { getByRole } = render ( < TextField / > ) ;
expect ( getByRole ( 'textbox' ) ) . toBeDefined ( ) ;
expect ( getByRole ( 'textbox' ) ) . toBeDefined ( ) ;
} ) ;
} ) ;
it ( 'should render and be able to input text' , ( ) => {
it ( 'should render and be able to input text' , async ( ) => {
const { getByTestId } = render (
const { user , getByRole } = setup ( < TextField / > ) ;
< TextField inputProps = { { 'data-testid' : 'text-field' } } / > ,
const textField = getByRole ( 'textbox' ) ;
) ;
await user . type ( textField , 'text value' ) ;
const textField = getByTestId ( 'text-field' ) ;
expect ( textField ) . toHaveValue ( 'text value' ) ;
expect ( textField . value ) . toBe ( '' ) ; // initial value is empty string
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
fireEvent . change ( textField , { target : { value : '' } } ) ; // reset value
expect ( textField . value ) . toBe ( '' ) ; // value is empty string after reset
} ) ;
} ) ;
it ( 'should render and fire onFocus and onBlur events' , ( ) => {
it ( 'should render and fire onFocus and onBlur events' , ( ) => {
const onFocus = jest . fn ( ) ;
const onFocus = jest . fn ( ) ;
@ -38,124 +58,80 @@ describe('TextField', () => {
fireEvent . blur ( textField ) ;
fireEvent . blur ( textField ) ;
expect ( onBlur ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( onBlur ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
it ( 'should render and fire onChange event' , ( ) => {
it ( 'should render and fire onChange event' , async ( ) => {
const onChange = jest . fn ( ) ;
const onChange = jest . fn ( ) ;
const { getByTestId } = render (
const { user , getByRole } = setup (
< TextField
< TextField
inputProps = { { 'data-testid' : 'text-field' } }
inputProps = { { 'data-testid' : 'text-field' } }
onChange = { onChange }
onChange = { onChange }
/ > ,
/ > ,
) ;
) ;
const textField = getByTestId ( 'text-field ' ) ;
const textField = getByRole ( 'textbox ' ) ;
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
await user . type ( textField , '123' ) ;
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( textField ) . toHaveValue ( '123' ) ;
expect ( onChange ) . toHaveBeenCalledTimes ( 3 ) ;
} ) ;
} ) ;
it ( 'should render and fire onClick event' , ( ) => {
it ( 'should render and fire onClick event' , async ( ) => {
const onClick = jest . fn ( ) ;
const onClick = jest . fn ( ) ;
const { getByTestId } = render (
const { user , getByTestId } = setup (
< TextField
< TextField data - testid = "text-field" onClick = { onClick } / > ,
inputProps = { { 'data-testid' : 'text-field' } }
onClick = { onClick }
/ > ,
) ;
) ;
const textField = getByTestId ( 'text-field' ) ;
await user . click ( getByTestId ( 'text-field' ) ) ;
fireEvent . click ( textField ) ;
expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
it ( 'should render showClear button when showClear is true and value exists' , ( ) => {
const { getByRole , getByTestId } = render (
< TextField
clearButtonProps = { { 'data-testid' : 'clear-button' } }
clearButtonIconProps = { { 'data-testid' : 'clear-button-icon' } }
showClear
/ > ,
) ;
const textField = getByRole ( 'textbox' ) ;
expect ( textField . value ) . toBe ( '' ) ; // initial value is empty string
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
expect ( getByTestId ( 'clear-button' ) ) . toBeDefined ( ) ;
expect ( getByTestId ( 'clear-button-icon' ) ) . toBeDefined ( ) ;
} ) ;
it ( 'should render with the rightAccessory' , ( ) => {
it ( 'should render with the rightAccessory' , ( ) => {
const { getByText } = render (
const { getByText } = render (
< TextField rightAccessory = { < div > right - accessory < /div>} / > ,
< TextField rightAccessory = { < div > right - accessory < /div>} / > ,
) ;
) ;
expect ( getByText ( 'right-accessory' ) ) . toBeDefined ( ) ;
expect ( getByText ( 'right-accessory' ) ) . toBeDefined ( ) ;
} ) ;
} ) ;
it ( 'should still render with the rightAccessory when showClear is true' , ( ) => {
it ( 'should render showClearButton button when showClearButton is true and value exists' , async ( ) => {
const { getByRole , getByTestId , getByText } = render (
// As showClearButton is intended to be used with a controlled input we need to use setupControlled
< TextField
const { user , getByRole } = setupControlled ( TextField , {
clearButtonProps = { { 'data-testid' : 'clear-button' } }
showClearButton : true ,
clearButtonIconProps = { { 'data-testid' : 'clear-button-icon' } }
} ) ;
rightAccessory = { < div > right - accessory < / d i v > }
await user . type ( getByRole ( 'textbox' ) , 'test value' ) ;
showClear
expect ( getByRole ( 'textbox' ) ) . toHaveValue ( 'test value' ) ;
/ > ,
expect ( getByRole ( 'button' , { name : /Clear/u } ) ) . toBeDefined ( ) ;
) ;
const textField = getByRole ( 'textbox' ) ;
expect ( textField . value ) . toBe ( '' ) ; // initial value is empty string
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
expect ( getByTestId ( 'clear-button' ) ) . toBeDefined ( ) ;
expect ( getByTestId ( 'clear-button-icon' ) ) . toBeDefined ( ) ;
expect ( getByText ( 'right-accessory' ) ) . toBeDefined ( ) ;
} ) ;
} ) ;
it ( 'should clear text when clear button is clicked' , ( ) => {
it ( 'should still render with the rightAccessory when showClearButton is true' , async ( ) => {
const { getByRole , getByTestId } = render (
// As showClearButton is intended to be used with a controlled input we need to use setupControlled
< TextField
const { user , getByRole , getByText } = setupControlled ( TextField , {
clearButtonProps = { { 'data-testid' : 'clear-button' } }
showClearButton : true ,
clearButtonIconProps = { { 'data-testid' : 'clear-button-icon' } }
rightAccessory : < div > right - accessory < / d i v > ,
rightAccessory = { < div > right - accessory < / d i v > }
} ) ;
showClear
await user . type ( getByRole ( 'textbox' ) , 'test value' ) ;
/ > ,
expect ( getByRole ( 'textbox' ) ) . toHaveValue ( 'test value' ) ;
) ;
expect ( getByRole ( 'button' , { name : /Clear/u } ) ) . toBeDefined ( ) ;
const textField = getByRole ( 'textbox' ) ;
expect ( getByText ( 'right-accessory' ) ) . toBeDefined ( ) ;
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
fireEvent . click ( getByTestId ( 'clear-button' ) ) ;
expect ( textField . value ) . toBe ( '' ) ;
} ) ;
} ) ;
it ( 'should fire onClear event when passed to onClear prop' , ( ) => {
it ( 'should fire onClick event when passed to clearButtonOnClick when clear button is clicked' , async ( ) => {
const onClear = jest . fn ( ) ;
// As showClearButton is intended to be used with a controlled input we need to use setupControlled
const { getByRole , getByTestId } = render (
const fn = jest . fn ( ) ;
< TextField
const { user , getByRole } = setupControlled ( TextField , {
onClear = { onClear }
showClearButton : true ,
clearButtonProps = { { 'data-testid' : 'clear-button' } }
clearButtonOnClick : fn ,
clearButtonIconProps = { { 'data-testid' : 'clear-button-icon' } }
} ) ;
showClear
await user . type ( getByRole ( 'textbox' ) , 'test value' ) ;
/ > ,
await user . click ( getByRole ( 'button' , { name : /Clear/u } ) ) ;
) ;
expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
const textField = getByRole ( 'textbox' ) ;
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
fireEvent . click ( getByTestId ( 'clear-button' ) ) ;
expect ( onClear ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
it ( 'should fire clearButtonProps.onClick event when passed to clearButtonProps.onClick prop' , ( ) => {
it ( 'should fire onClick event when passed to clearButtonProps.onClick prop' , async ( ) => {
const onClear = jest . fn ( ) ;
// As showClearButton is intended to be used with a controlled input we need to use setupControlled
const onClick = jest . fn ( ) ;
const fn = jest . fn ( ) ;
const { getByRole , getByTestId } = render (
const { user , getByRole } = setupControlled ( TextField , {
< TextField
showClearButton : true ,
onClear = { onClear }
clearButtonProps : { onClick : fn } ,
clearButtonProps = { { 'data-testid' : 'clear-button' , onClick } }
} ) ;
clearButtonIconProps = { { 'data-testid' : 'clear-button-icon' } }
await user . type ( getByRole ( 'textbox' ) , 'test value' ) ;
showClear
await user . click ( getByRole ( 'button' , { name : /Clear/u } ) ) ;
/ > ,
expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
) ;
const textField = getByRole ( 'textbox' ) ;
fireEvent . change ( textField , { target : { value : 'text value' } } ) ;
expect ( textField . value ) . toBe ( 'text value' ) ;
fireEvent . click ( getByTestId ( 'clear-button' ) ) ;
expect ( onClear ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
it ( 'should be able to accept inputProps' , ( ) => {
it ( 'should be able to accept inputProps' , ( ) => {
const { getByRole } = render (
const { getByTestId } = render (
< TextField inputProps = { { 'data-testid' : 'text-field' } } / > ,
< TextField inputProps = { { 'data-testid' : 'text-field' } } / > ,
) ;
) ;
const textField = getByRole ( 'textbox' ) ;
expect ( getByTestId ( 'text-field' ) ) . toBeDefined ( ) ;
expect ( textField ) . toBeDefined ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;