11import React , { useState } from 'react'
22import { render } from '@testing-library/react'
33import userEvent from '@testing-library/user-event'
4- import { TextInput , TextInputField } from '../'
4+ import { TextInput } from '../'
5+ import { mockRef } from '../../test/utils'
56
67function makeTextInputFixture ( props = { } ) {
78 return < TextInput data-testid = "input" { ...props } />
89}
910
10- function makeTextInputFieldFixture ( props = { } ) {
11- return < TextInputField data-testid = "input" label = "Name" { ...props } />
12- }
13-
1411describe ( 'TextInput' , ( ) => {
15- it ( 'Should render without crashing' , ( ) => {
12+ it ( 'should forward ref to underlying <input />' , ( ) => {
13+ const ref = mockRef ( )
14+
15+ render ( makeTextInputFixture ( { ref } ) )
16+
17+ expect ( ref . current ) . toBeInstanceOf ( HTMLInputElement )
18+ } )
19+
20+ it ( 'should render without crashing' , ( ) => {
1621 expect ( ( ) => render ( makeTextInputFixture ( ) ) ) . not . toThrow ( )
1722 } )
1823
19- it ( 'Should accept placeholder text' , ( ) => {
24+ it ( 'should accept placeholder text' , ( ) => {
2025 const { getByPlaceholderText } = render ( makeTextInputFixture ( { placeholder : 'Enter text here' } ) )
26+
2127 expect ( getByPlaceholderText ( 'Enter text here' ) ) . toBeInTheDocument ( )
2228 } )
2329
24- it ( 'Should set an invalid state if `isInvalid` is `true`' , ( ) => {
30+ it ( 'should set an invalid state if `isInvalid` is `true`' , ( ) => {
2531 const { getByTestId } = render ( makeTextInputFixture ( { isInvalid : true } ) )
2632 const input = getByTestId ( 'input' )
33+
2734 expect ( input ) . toHaveAttribute ( 'aria-invalid' , 'true' )
2835 } )
2936
30- it ( 'Should accept an `onChange` handler to be a controlled component' , ( ) => {
37+ it ( 'should accept an `onChange` handler to be a controlled component' , ( ) => {
3138 function ControlledTextInput ( ) {
3239 const [ value , setValue ] = useState ( '' )
3340 return (
@@ -43,42 +50,18 @@ describe('TextInput', () => {
4350 const { getByDisplayValue, getByTestId } = render ( < ControlledTextInput /> )
4451 const input = getByTestId ( 'input' )
4552 userEvent . click ( input )
53+
4654 expect ( document . activeElement ) . toEqual ( input )
4755 userEvent . type ( input , 'Testing' )
4856 expect ( getByDisplayValue ( 'Testing' ) ) . toEqual ( input )
4957 } )
5058
51- it ( 'Should not be interactive if `disabled` is passed in' , ( ) => {
59+ it ( 'should not be interactive if `disabled` is passed in' , ( ) => {
5260 const { getByDisplayValue, getByTestId } = render ( makeTextInputFixture ( { disabled : true } ) )
5361 const input = getByTestId ( 'input' )
5462 userEvent . type ( input , 'Testing' )
63+
5564 expect ( ( ) => getByDisplayValue ( 'Testing' ) ) . toThrowError ( )
5665 expect ( getByDisplayValue ( '' ) ) . toEqual ( input )
5766 } )
5867} )
59-
60- describe ( 'TextInputField' , ( ) => {
61- it ( 'Should render without crashing' , ( ) => {
62- expect ( ( ) => render ( makeTextInputFieldFixture ( ) ) ) . not . toThrow ( )
63- } )
64-
65- it ( 'Should render a required `label` when passed in' , ( ) => {
66- const { getByLabelText } = render ( makeTextInputFieldFixture ( ) )
67- expect ( getByLabelText ( 'Name' ) ) . toBeInTheDocument ( )
68- } )
69-
70- it ( 'Should render a `hint` underneath the input' , ( ) => {
71- const { getByText } = render ( makeTextInputFieldFixture ( { hint : 'Enter a value in the input' } ) )
72- expect ( getByText ( 'Enter a value in the input' ) ) . toBeInTheDocument ( )
73- } )
74-
75- it ( 'Should render an astrix when `required` is passed in' , ( ) => {
76- const { getByTitle } = render ( makeTextInputFieldFixture ( { required : true } ) )
77- expect ( getByTitle ( 'This field is required.' ) ) . toBeInTheDocument ( )
78- } )
79-
80- it ( 'Should not render a `validationMessage` when passed in' , ( ) => {
81- const { getByText } = render ( makeTextInputFieldFixture ( { validationMessage : 'Please enter a value' } ) )
82- expect ( getByText ( 'Please enter a value' ) ) . toBeInTheDocument ( )
83- } )
84- } )
0 commit comments