资讯专栏INFORMATION COLUMN

Enzyme

Amos / 1523人阅读

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

Render a component

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 component
const {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 dependency
const {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.

find a child component find

the powerful find method can find a node with powerful Enzyme Selector which support CSS selector and component constructor.

const wrapper = mount(
  
); //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)
ref
class Foo extends React.Component {
  render() {
    return (
      
First Second Third
); } } const wrapper = mount(); expect(wrapper.ref("secondRef").prop("amount")).to.equal(4);
at/childAt

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(
  
) //find the first li wrapper.find("li").at(0) wrapper.find("li").first() //find the third li node wrapper.find("li").at(2) wrapper.find("li").last() wrapper.childAt(2)
instance

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 shown
const wrapper = mount(
); 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;
Have or contain some text
const wrapper = mount(
  
Test
) 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")
Have some prop or className
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、Enzyme 简介 Jest 是 Facebook 发布的一个开源的、基于 Jasmine 框架的 JavaScript 单元测试工具。 Enzyme 是 React 的测试类库。 Enzy...

    xushaojieaaa 评论0 收藏0
  • 【全栈React】第25天: 使用Enzyme做更好的测试

    摘要:昨天我们使用了库来编写我们对组件的第一个测试。是由团队发布和维护的测试实用程序库它提供了一个更好的高级的来处理测试中的组件。我们将使用导出的函数来装载我们的组件。相反我们必须使用提供的方法。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3806原文:https://www.fullstackreact.com/30-days-...

    baukh789 评论0 收藏0
  • 前端组件的测试

    摘要:的配置文件是为了解析那些需要测试的源文件相关的文件,然后再给的单元测试用例去识别。其作用是仅仅渲染至虚拟节点,不会返回真实的节点,能极大提高测试性能。 为解放劳动力,发展生产力 测试有了这般变化: 鼠标点击手动测试 -> 用脚本模拟,自动化测试 Vue中的组件测试 需要安装的包 全局安装:babel、mocha、karma 其他局部安装的包在下面的【测试环境搭建】最下方配置文件中给出...

    haobowd 评论0 收藏0
  • javascript 迁移 typescript 实践

    摘要:但是,从长远来看,尤其是多人协作的项目,还是很有必要的。第二参数为了某些场景下要大写强调,只需要传入即可自动将结果转成大写。这个有可能是业务上线了之后才发生,直接导致业务不可用。而这也被证明是个好的编码方式。 只是抱着尝试的心态对项目进行了迁移,体验了一番typeScript的强大,当然,习惯了JavaScript的灵活,弱类型,刚用上typeScript时会很不适应,犹如懒散惯了的人...

    niceforbear 评论0 收藏0
  • 前端进阶(8) - 前端开发需要了解的工具集合:webpack, eslint, prettier,

    摘要:前端开发需要了解的工具集合前端开发需要了解的一些工具,这些工具能够帮助你在项目开发中事半功倍。总之,是前端打包的不二选择。所以,很多情况下都是与配合使用。它的一个理念就是提供一套完整集成的零配置测试体验。 前端开发需要了解的工具集合:webpack, eslint, prettier, ... 前端开发需要了解的一些工具,这些工具能够帮助你在项目开发中事半功倍。 1. nrm: npm...

    SillyMonkey 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<