I am trying to basically just change a counter and show that the value has changed. I am doing this with getByTestId
so that could be the problem ?
Here is my component:
import React, { useState } from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
const [count, setCounter] = useState(0)
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
div
<div
onClick={() => setCounter(prevCount => prevCount + 1)}
data-testid="addCount"
>
+
</div>
<div data-testid="count">
{count}
</div>
<div
onClick={() => setCounter(prevCount => prevCount - 1)}
data-testid="minusCount"
>
-
</div>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
Here is the test I am trying to run:
describe('State is managed correctly', () => {
const { getByTestId } = render(<App />)
const add = getByTestId(`addCount`)
const count = getByTestId(`count`)
it('count starts at 0', () => {
expect(count).toHaveTextContent("0")
})
it('count added, value should be 1', () => {
fireEvent.click(add)
expect(count).toHaveTextContent("1") // error count is still 0
})
})
cleanup
before each test run, remount the component, and manipulate the state only in the way you want to change it for that test. – Grishilde