"testing inputs with react testing library" Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "testing inputs with react testing library" answered properly. Developers are finding an appropriate answer about testing inputs with react testing library related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like testing inputs with react testing library. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on testing inputs with react testing library.
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
Source: testing-library.com
All those coders who are working on the TypeScript based application and are stuck on testing inputs with react testing library can get a collection of related answers to their query. Programmers need to enter their query on testing inputs with react testing library related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about testing inputs with react testing library for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of testing inputs with react testing library while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "testing inputs with react testing library". Visit this developer's friendly online web community, CodeProZone, and get your queries like testing inputs with react testing library resolved professionally and stay updated to the latest TypeScript updates.