测试状态更新功能通过组件的道具传递

发布于 2025-02-13 10:11:45 字数 679 浏览 0 评论 0原文

我正在尝试学习与开玩笑和酶的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文