Skip to content

Instantly share code, notes, and snippets.

View christopherkade's full-sized avatar

Christopher Kade christopherkade

View GitHub Profile
@christopherkade
christopherkade / logo.svg
Created December 21, 2017 13:39
Star Wars SVG logo
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:07
Unit testing example 01
// 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", () => {
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:39
Unit testing example 02
it("should return -2", () => {
expect(add(0, -2)).toBe(-2)
})
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:40
Unit testing example 03
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", () => {
@christopherkade
christopherkade / test.spec.js
Last active April 2, 2019 07:43
Unit testing example 04
// /src/setupTests.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:52
Unit testing example 05
// 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);
});
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.
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:54
Unit testing example 06
class App extends Component {
state = {
counter: 0
}
handleClick = () => {
this.setState(state => {
return {
counter: state.counter + 1
}
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:55
Unit testing example 07
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(() => {
@christopherkade
christopherkade / test.spec.js
Created April 2, 2019 07:56
Unit test example 08
it("should increase counter when the button is clicked", () => {
wrapper.find("button").simulate("click")
expect(wrapper.find("h1").text()).toContain("1")
})