Since the enzyme can make us get easier to update to new version of React and make our test code more lean and maintainable
You"d better not use props() to get props from component, because Enzyme document said that only the root component can get props by the function. We can use chai-moment to do the assertion
Please use methods in require("test/unit/helpers/renderHelpers") to render component, do not use enzyme directly, this gives us the flexibility to handle the memory issue.
Currently the renderHelper.js is only on the spike branch. And will make a pr soon.
You can also checkout this spike branch to see how we change the existing tests to enzyme.
UI componentconst {mount} = require("test/unit/helpers/renderHelpers") const wrapper = mount();
You can specify the DOM node you want to mount on by provide the second argument:
const container = document.createElement("div"); const wrapper = mount(, { attachTo: container });
enzyme also provide shallow render, but I suggest to use mount for keeping things consistant.
Page or component with route dependencyconst {mountWithRouteContext} = require("test/unit/helpers/renderHelpers") const wrapper = mount(Wrapper methods of enzyme);
When you call the render method we metioned before, instead of return a ReactComponent, now we returns a wrapper provide by enzyme.
Here I would like to introduce some most useful methods. And please check the document for more information.
the powerful find method can find a node with powerful Enzyme Selector which support CSS selector and component constructor.
const wrapper = mount(ref); //by jQuery selector const fooWrapper = wrapper.find(".foo") //find also returns a wrapper, so it support chaining call const barWrapper = wrapper.find(".foo").find(".bar") //by Component type const iconWrapper = wrapper.find(Icon)
class Foo extends React.Component { render() { return (at/childAtFirst Second Third); } } const wrapper = mount(); expect(wrapper.ref("secondRef").prop("amount")).to.equal(4);
The wrapper in enzyme is just like jQuery object. so it might have multiple components inside, the at and childAt can find a node in the current wrapper by index passed in
Note : the index is zero-based
const wrapper = mount(
You can get the ReactComponent instance, but it"s only available for the wrapper of root node (the node passed into mount())
wrapper.state() in enzyme is only available for root component
It"s useful when you want to stub a method like transitionTo
buttonLink.instance().transitionTo = spy(); //This is unvaliable! .instance is only for the root node buttonLink.find("span").instance()simulate
To simulate events:
const wrapper = mount() wrapper.simulate("click")Assertion
suggest to use chai and chai-enzyme to do the assertion instead of should.js, we"ve created some customized assert method like containText work with should.js before, but I think chai-enzyme cover more scenarioes.
highly suggest you to check the document of chai-enzyme when you don"t know how to do the assertion, and below are the most common cases we will meet
A node is shownconst wrapper = mount(Have or contain some text); expect(wrapper.find("span")).to.be.present(); expect(wrapper.find("span")).to.exist; //exist is an alias expect(wrapper.find("ul")).to.not.be.present(); expect(wrapper.find("ul")).to.not.exist;
const wrapper = mount(Have some prop or classNameTest) expect(wrapper.find("#child")).to.have.text("Test") expect(wrapper.find("#child")).to.contain.text("Te") expect(wrapper.find("#child")).to.not.have.text("Other text")
const wrapper = mount() expect(wrapper.find(User).first()).to.have.prop("index") expect(wrapper.find(User).first()).to.not.have.prop("invalid") expect(wrapper.find(User).first()).to.have.prop("index", 1) expect(wrapper.find(User).first()).to.not.have.prop("index", 2) expect(wrapper.find(User).first()).to.have.prop("index").equal(1) expect(wrapper.find(User).first()).to.have.prop("user").deep.equal({name: "Jane"}) And if you want to check the className, here is a better way: const wrapper = mount(
test) expect(wrapper.find("span")).to.have.className("child") expect(wrapper.find("span")).to.not.have.className("root") to.be.true; to.be.null; to.have.lengthOf(2); to.have.deep.property(‘response.title’, ’sdfdgd")
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/92609.html
摘要:简介是发布的一个开源的基于框架的单元测试工具。具体版本对照如下版本版本此处使用的版本为,所以我们需要安装依赖安装完成,接下来需要进行相关的配置。这样就可以将测试集中在组件的结构和逻辑上。 Jest、Enzyme 简介 Jest 是 Facebook 发布的一个开源的、基于 Jasmine 框架的 JavaScript 单元测试工具。 Enzyme 是 React 的测试类库。 Enzy...
摘要:昨天我们使用了库来编写我们对组件的第一个测试。是由团队发布和维护的测试实用程序库它提供了一个更好的高级的来处理测试中的组件。我们将使用导出的函数来装载我们的组件。相反我们必须使用提供的方法。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3806原文:https://www.fullstackreact.com/30-days-...
摘要:但是,从长远来看,尤其是多人协作的项目,还是很有必要的。第二参数为了某些场景下要大写强调,只需要传入即可自动将结果转成大写。这个有可能是业务上线了之后才发生,直接导致业务不可用。而这也被证明是个好的编码方式。 只是抱着尝试的心态对项目进行了迁移,体验了一番typeScript的强大,当然,习惯了JavaScript的灵活,弱类型,刚用上typeScript时会很不适应,犹如懒散惯了的人...
摘要:前端开发需要了解的工具集合前端开发需要了解的一些工具,这些工具能够帮助你在项目开发中事半功倍。总之,是前端打包的不二选择。所以,很多情况下都是与配合使用。它的一个理念就是提供一套完整集成的零配置测试体验。 前端开发需要了解的工具集合:webpack, eslint, prettier, ... 前端开发需要了解的一些工具,这些工具能够帮助你在项目开发中事半功倍。 1. nrm: npm...
阅读 890·2021-11-24 09:38
阅读 841·2021-11-23 09:51
阅读 2914·2021-11-16 11:44
阅读 1708·2021-09-22 15:52
阅读 1600·2021-09-10 11:20
阅读 1342·2019-08-30 13:47
阅读 1259·2019-08-29 12:36
阅读 3275·2019-08-26 10:43