测试状态更新功能通过组件的道具传递
我正在尝试学习与开玩笑和酶的React进行测试,我想测试我制作的开关组件:
import Switch from "./index";
import { mount, ReactWrapper} from "enzyme";
describe("[UNIT] Testing Switch component", () => {
let wrapper: ReactWrapper
beforeEach(() => {
wrapper = mount(<Switch/>)
})
describe('Component validation', () => {
let initialState = wrapper.state("state")
wrapper.simulate("click")
it('should toggle value', function () {
expect(wrapper.state("state")).toEqual(!initialState)
});
})
})
事实是,我的开关组件采用两个道具,状态和setstate,通常由父母中的usestate挂钩定义成分。
我如何实施假的“ Usestate”?还是我的测试理念完全错了?
I'm trying to learn testing in react with Jest and Enzyme and I'd like to test a switch component I made:
import Switch from "./index";
import { mount, ReactWrapper} from "enzyme";
describe("[UNIT] Testing Switch component", () => {
let wrapper: ReactWrapper
beforeEach(() => {
wrapper = mount(<Switch/>)
})
describe('Component validation', () => {
let initialState = wrapper.state("state")
wrapper.simulate("click")
it('should toggle value', function () {
expect(wrapper.state("state")).toEqual(!initialState)
});
})
})
The thing is that my Switch component takes two props, state and setState, normally defined by a useState hook in the parent component.
How could I implement a fake "useState"? Or am I totally wrong in my testing philosophy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论