Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. The method we want to test | |
function add(x, y) { | |
return x + y | |
} | |
// 2. A test suite | |
describe("add method", () => { | |
// 3. A unit test | |
it("should return 2", () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("should return -2", () => { | |
expect(add(0, -2)).toBe(-2) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(x, y) { | |
// Check if the parameters are numbers | |
// If not, throw an error | |
if (isNaN(x) || isNaN(y)) { | |
throw new Error("Parameter is not a number !") | |
} | |
return x + y | |
} | |
describe("add method", () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /src/setupTests.js | |
import { configure } from 'enzyme'; | |
import Adapter from 'enzyme-adapter-react-16'; | |
configure({ adapter: new Adapter() }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App.spec.js | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import App from './App'; | |
it('renders without crashing', () => { | |
const div = document.createElement('div'); | |
ReactDOM.render(<App />, div); | |
ReactDOM.unmountComponentAtNode(div); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PASS src/App.spec.js | |
✓ renders without crashing (2ms) | |
Test Suites: 1 passed, 1 total | |
Tests: 1 passed, 1 total | |
Snapshots: 0 total | |
Time: 0.097s, estimated 1s | |
Ran all test suites. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
state = { | |
counter: 0 | |
} | |
handleClick = () => { | |
this.setState(state => { | |
return { | |
counter: state.counter + 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import App from './App'; | |
import { shallow } from 'enzyme' | |
// 1. Test suite | |
describe("[UNIT] Testing the App component", () => { | |
let wrapper | |
// 2. A Jest setup helper function | |
beforeEach(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("should increase counter when the button is clicked", () => { | |
wrapper.find("button").simulate("click") | |
expect(wrapper.find("h1").text()).toContain("1") | |
}) |
OlderNewer