Jest stage 2
Testing click behaviour
When testing rendered button, we want to make sure that clicking the component triggers the appropriate props. Rather than checking the onClick
props of the rendered button, it is easier to simulate a click:
describe("Delete button", () => {
beforeEach(() => {
onDeleteFn = jest.fn();
deleteButton = shallow(<DeleteButton deleteFunc={onDeleteFn} />);
});
it("click triggers delete function", () => {
deleteButton.simulate("click");
expect(onDeleteFn).toBeCalled();
});
});
Source:
Snapshot testing
Very cool feature for non regression testing. If not done yet, add yard add --dev react-test-renderer
. And it is now as simple as:
import React from "react";
import renderer from "react-test-renderer";
import { MyComponent } from "my-component-path;
describe("My Component", () => {
it ('match snapshot', () => {
const tree = renderer
.create(<MyComponent someparam="somevalue" />)
.toJSON();
expect(tree).toMatchSnapshot();
})
});
Articles: