国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Enzyme

Amos / 1653人閱讀

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")

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<