Avoiding Implementation Details
Why We're Here
import React from "react";
import { shallow } from "enzyme";
import MyCoolCounter from "./my-cool-counter";
describe('<MyCoolCounter />', () => {
let wrapper;
let mockDispatch;
beforeEach(() => {
wrapper = shallow(<MyCoolCounter />);
});
describe('when the counter is clicked', () => {
beforeEach(() => {
wrapper.find(BigButton).simulate('click');
});
it('dispatches INCREASE_COUNTER action', () => {
expect(mockDispatch).toBeCalledWith({
type: "INCREASE_COUNTER"
});
});
});
});
Why Can A Test Fail
export const add = (a, b) => a + b;
const multiply = (a, b) => {
let total = 0;
let repCount = 0;
while (repCount < b) {
total = add(total, a);
repCount = repCount + 1;
}
return total;
};
import multiply, { add } from "./multiply";
jest.mock(add);
describe("multiply", () => {
beforeEach(() => {
jest.clearMocks();
});
it("calls add 4 times when supplied with (3,4)", () => {
multiply(3, 4);
expect(add).toBeCalledTimes(4);
});
it("calls add 5 times when supplied with (3,5)", () => {
multiply(3, 5);
expect(add).toBeCalledTimes(5);
});
});
export const add = (a, b) => a + b;
const multiply = (a, b) => {
return a * b;
};
import multiply from "./multiply";
describe("multiply", () => {
it("returns 12 when supplied with 3 & 4", () => {
expect(multiply(3, 4)).toBe(12);
});
it("returns 15 when supplied with 3 & 5", () => {
expect(multiply(3, 5)).toBe(15);
});
it("returns 0 when supplied with 0 and 1", () => {
expect(multiply(0, 1)).toBe(0);
});
});
Having no test is better than having a false negative or false positive test
export const initialState = {
todos: [];
completed: [];
}
const todoReducer = (state = initialState, action) => {
switch(action.type) {
case "TODO_ADDED": {
const todo = action.payload;
return {
...state,
todos: [...state.todos, todo ],
}
}
default:
return state;
}
}
import reducer, { initialState } from "./reducer";
describe(() => {
it("returns the correct initial state", () => {
// complete this.
});
});
Black Box Testing Philosophy
Function, module, or class
input
output
Environment
side effect
Black Box Testing Philosophy
multiply
(2,3)
6
Environment
const AnimalPreview = (props) => (
<MuiCard>
<MuiCardImage src={prop.imageSource} variant={1} />
<MuiCardTitle>{prop.name}</MuiCardTitle>
<MuiCardText>{prop.summary}</MuiCardText>
<MuiCardActions>
<MuiButton onClick={prop.onShareClick}>Share</MuiButton>
<MuiButton onLearMoreClick={prop.onLearnMoreClick}>
Learn More
</MuiButton>
</MuiCardActions>
</MuiCard>
);
AnimalPreview
MuiCard
MuiCardTitle
h1
MuiCardText
p
MuiCardImage
img
MuiCardActin
button
Button
Button
button
AnimalPreview
h1
p
img
button
button
Requirements
import React from "react";
import {render} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import AnimalPreview from "./animal-preview";
describe("AnimalPreview", () => {
// write tests to meet requirements
it("should render information correctly", () => {
// setup
});
it("should fire provided handler when 'share' is clicked", () => {
// setup
});
it("should fire provided handler when 'learn more' is clicked", () => {
});
});
const AnimalPreview = (props) => (
<AntTile type="square">
<AntImg src={prop.imageSource} variant={1} />
<AntTileTitle>{prop.name}</AntTileTitle>
<AntTileText>{prop.summary}</AntTileText>
<AntTileActionArea>
<AntButton onClick={prop.onShareClick}>Share</AntButton>
<AntButton onLearMoreClick={prop.onLearnMoreClick}>
Learn More
</AntButton>
</MuiCardActions>
</MuiCard>
);
Spot A Unit Test