testing inputs with react testing library

import React from 'react'
import { render, fireEvent } from '@testing-library/react'

class CostInput extends React.Component {
  state = {
    value: '',
  }

  removeDollarSign = value => (value[0] === '$' ? value.slice(1) : value)
  getReturnValue = value => (value === '' ? '' : `$${value}`)
  handleChange = ev => {
    ev.preventDefault()
    const inputtedValue = ev.currentTarget.value
    const noDollarSign = this.removeDollarSign(inputtedValue)
    if (isNaN(noDollarSign)) return
    this.setState({ value: this.getReturnValue(noDollarSign) })
  }

  render() {
    return (
      <input
        value={this.state.value}
        aria-label="cost-input"
        onChange={this.handleChange}
      />
    )
  }
}

const setup = () => {
  const utils = render(<CostInput />)
  const input = utils.getByLabelText('cost-input')
  return {
    input,
    ...utils,
  }
}

test('It should keep a $ in front of the input', () => {
  const { input } = setup()
  fireEvent.change(input, { target: { value: '23' } })
  expect(input.value).toBe('$23')
})
test('It should allow a $ to be in the input when the value is changed', () => {
  const { input } = setup()
  fireEvent.change(input, { target: { value: '$23.0' } })
  expect(input.value).toBe('$23.0')
})

test('It should not allow letters to be inputted', () => {
  const { input } = setup()
  expect(input.value).toBe('') // empty before
  fireEvent.change(input, { target: { value: 'Good Day' } })
  expect(input.value).toBe('') //empty after
})

test('It should allow the $ to be deleted', () => {
  const { input } = setup()
  fireEvent.change(input, { target: { value: '23' } })
  expect(input.value).toBe('$23') // need to make a change so React registers "" as a change
  fireEvent.change(input, { target: { value: '' } })
  expect(input.value).toBe('')
})
Copy

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
screen react testing library get by input type react testing library get all inputs testing input react testing library react testing library select input options input field selection react testing library react testing library update input text input field react testing library get input field react testing library react testing library on input testing get input value react testing library react testing library intput element get value react testing library input component value react testing get input react testing library get input event how to test input with react testing library getInput in react testing library inputfiled in react testing library react testing library role for input get an inupt field react testing library react testing library inut value how would you test an input form react-testing-library how to type data into an input field using react-testing-library react-testing-library get value from text input field react-testing-library input field react testing library find by input type library react test input tag select input react testing library react testing library input text react testing lib get input react testing library get input by value react testing library get by input name React testing library test input how to get the form input in testing library react react testing library user input react testing library get input by name react testing library query by input react testing library find by input value react testing library named input react test library input react-testing-library type in input react testing library get by input type react testing library give input to true input type text testing in react testing library react testing library test an input is rendered react testing library change input react testing library get input by name attribute react testing library test custom input react testing library input components react testing library input role react testing library get form input value type on input react testing library what kind of test should i write for input in react testing library react testing library file input get input value jest react testing library react testing library keyboard input fill input react testing library react testing library input name react testing library get input element @testing-library/react input react testing library with input react testing library for component input form react test library input value test input value react testing library how to test input fields using react js testing library react testing library get text input role of input type in react testing library react testing library select input by name testing library react type into input field react testing library get input field type value input react testing library how to get input value react testing library react testing library tiping in a input type input react testing library input attributes testing library react take input react with testing librairy find input react testing library react-testing-library check input value testing input in react testing library react testing library putting the value in input box? react testing library find input react testing library input fields onVhange react testing library check input value input setup react testing library testing inputs react testing library find an input with label in react testing library react testing library test select input simulate a input react testing library react testing library input field react testing library form test input react testing library react testing library to contain form how to mock test input to input tag in react testing library test all type form fields react react testing library test select inputs in react testing library react testing library typing multiple characters react testing library for text input fireevent input react testing library test input change in state update react testing library to contain text test in input field react check for input error react testing library change input react testing library fireevent.input testing library get input how to write unit test cases for e.target.value using react testing library get by placeholder react testing library fireevent input multiple inputs findbytestid react testing library @testing-library/react change input value modify value input react testing library input is &quot;&quot; react testing library input react testing library react testing library set input value react-testing-library get input value getinputandtypetext react testing library jest fireevent change value input test react enter text into input react testing library react testing library enter text jest react testing library get value of input react testing library add value to element react testing library check for input how to send mock values to type other than string react testing library react testing library get value of input Test an input jest react testing library check a type of input testing-library/react how to fill input with react testing library react testing library test value of element testing library form input is valid get input field react-testing-library react-testing libary input fields input field testing react-testing library react testing library read textbox value react testing library get textbox value react testing library get text box value getbytestid react testing library onchange react testing library update input value input value react testing library react testing library input how to test a search box react testing library react testing library expect target value react testing library check target value how to test input value react testing library react testing library type how to test required inputs with react testing library react test library controlled form react testing library controlled forms react testing library get state with empty string value react testing library type into input get input react testing library react testing library fill input react testing library clear input text input wihtout form fireevent change react testing library get fill input react testing expect input to be empty set input value react testing library test onchagne with jest dom and react testing library test input has value react testing library react getbytestid react testing library type in input react testing library test input field test an input field react testing library check input value react testing library test on array react testing library react testing library input onchange react testing library get input react testing libarry get input value testing library test change text expect input to have value react testing library testing-library/react type input react-testing-library input value react testing library test input value insert value into input react testing library fireevent.input react-testing-library change input value react testing library get input value in react testing library react testing library testing input inputs react testing library react testing library get input value react testing library set field value test a text field react testing library react testing library input value testing input with react testin library
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source