Skip to content

Instantly share code, notes, and snippets.

@codegagan
Created August 30, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegagan/0ff772ba01ae53cfc2079aa02a551430 to your computer and use it in GitHub Desktop.
Save codegagan/0ff772ba01ae53cfc2079aa02a551430 to your computer and use it in GitHub Desktop.
Test with multiple useState implementations
import * as reactModule from "react";
import { shallow } from "enzyme";
import MultipleStatesComponent from "./MultipleStatesComponent";
describe("test multiple state in component", () => {
let wrapper;
let setDataLength;
let setLoading;
let setText;
beforeEach(() => {
setDataLength = jest.fn(x => {});
setLoading = jest.fn(x => {});
setText = jest.fn(x => {});
reactModule.useState = jest
.fn()
.mockImplementationOnce(x => [x, setDataLength])
.mockImplementationOnce(x => [x, setLoading])
.mockImplementationOnce(x => [x, setText]);
wrapper = shallow(<MultipleStatesComponent />);
});
it("should test button one", () => {
wrapper
.find("button")
.at(0)
.simulate("click");
expect(setDataLength).toHaveBeenCalledWith(10);
});
it("should test button two", () => {
wrapper
.find("button")
.at(1)
.simulate("click");
expect(setLoading).toHaveBeenCalledWith(true);
expect(setText).toHaveBeenCalledWith("text set by button");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment