[英]How to change value of hook state in jest?
I'm new to jest.我是新来的笑话。 I'm trying to change hook state in jest.But I couldn't find any docs for implementing the same.Following is my hook state in SignIn screen.
我正在尝试在玩笑中更改挂钩状态。但我找不到任何用于实现相同功能的文档。以下是我在登录屏幕中的挂钩状态。
const [form, setForm] = React.useState({
email: "",
phoneNumber: "",
password: "",
signUpType: SignUpType.PHONE_NUMBER,
});
I want to change the state of signUpType
to EMAIL
while testing.How this can be achieved ?I've done the following我想在测试时将
signUpType
的状态signUpType
为EMAIL
。如何实现?我已经完成了以下操作
it("Email validation", () => {
const email = "sample.s@abc.com";
// Stub the initial state
const stubInitialState = [{ signUpTyp: "EMAIL" }];
React.useState = jest.fn().mockReturnValue([stubInitialState, {}]);
const tree = mount(
<Provider store={configureStore}>
<SignIn />
</Provider>
);
expect(tree.find('CustomTextInput[attrName="email"]').prop("value")).toBe(
""
);
expect(email).toMatch(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/);
});
But the above code is not working for me.Please help me to find a solution for this.Also the TextInput
will be either email
or phoneNumebr
depending on user selection.但是上面的代码对我不起作用。请帮我找到解决方案。此外,根据用户的选择,
TextInput
将是email
或phoneNumebr
。
You can try to use the jest.spyOn
api to mock the React.useState
method:你可以尝试使用
jest.spyOn
api 来模拟React.useState
方法:
const stubInitialState = [{ signUpTyp: "EMAIL" }];
const mockedSetform = jest.fn();
jest.spyOn(React, 'useState').mockReturnValue([stubInitialState, mockedSetform]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.