Skip to content

Instantly share code, notes, and snippets.

@richleach
Created June 10, 2021 14:43
Show Gist options
  • Save richleach/eb9ec81a6c674409d3d166ead3456370 to your computer and use it in GitHub Desktop.
Save richleach/eb9ec81a6c674409d3d166ead3456370 to your computer and use it in GitHub Desktop.
REACT TESTING LIBRARY - component used to test against
import React, { useState } from 'react'
import "../Counter/counter.css"
function Counter() {
const [counterValue, setCounterValue] = useState(0)
const [inputValue, setInputValue] = useState(1)
const addToCounter = () => {
setCounterValue(counterValue + inputValue)
}
const subtractFromCounter = () => {
setCounterValue(counterValue - inputValue)
}
return (
<div>
<h3 data-testid="header">My Counter</h3>
<h2 data-testid="counter"
className=
{`${counterValue >= 100 ? "green" : ""}${counterValue <= -100 ? "red" : ""}`}
>
{counterValue}
</h2>
<button data-testid="subtract-btn" onClick={subtractFromCounter}>-</button>
<input data-testid="input" type="number" defaultValue={inputValue} className="text-center" onChange={(e) => {setInputValue(parseInt(e.target.value))}} />
<button data-testid="add-btn" onClick={addToCounter}>+</button>
</div>
)
}
export default Counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment