Created
August 30, 2020 07:52
-
-
Save codegagan/0ff772ba01ae53cfc2079aa02a551430 to your computer and use it in GitHub Desktop.
Test with multiple useState implementations
This file contains hidden or 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 * 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