Reputation: 392
I am learning how to write test cases by using Jest. In my project I have a button If I Click that button then in paragraph it will show how many times I clicked. Please tell me how to write test case for that component.
This is App.js
import React from 'react';
import './App.css';
import Button from './Button/Button';
const App = () => {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<Button></Button>
</div>
</div>
</div>
)
}
export default App
This is Button.js
import React, { useState } from 'react';
import './Button.css';
const Button = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
)
}
export default Button
There is nothing in Button.test.js
This is package.json
{
"name": "one",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.5",
"@testing-library/react": "^11.1.1",
"@testing-library/user-event": "^12.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Upvotes: 0
Views: 1193
Reputation: 102307
Unit test solution:
Button.jsx
:
import React, { useState } from 'react';
const Button = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
};
export default Button;
Button.test.jsx
:
import React from 'react';
import Button from './Button';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
describe('64683116', () => {
it('should pass', () => {
const { container } = render(<Button></Button>);
expect(container.querySelector('p')).toHaveTextContent('You clicked 0 times');
expect(container.querySelector('button')).toHaveTextContent('Increment');
fireEvent.click(container.querySelector('button'));
expect(container.querySelector('p')).toHaveTextContent('You clicked 1 times');
fireEvent.click(container.querySelector('button'));
expect(container.querySelector('p')).toHaveTextContent('You clicked 2 times');
});
});
unit test result:
PASS examples/64683116/Button.test.jsx
64683116
✓ should pass (27 ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Button.jsx | 100 | 100 | 100 | 100 |
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.68 s
source code: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/64683116
Upvotes: 3