使用React Native的酶的浅().text()不能像我预期的那样工作

Ash*_*man 7 javascript mocha.js reactjs react-native enzyme

我正试图对React Native测试w/enzymereact-native-mock进行一些基本的了解.

不包括在下面:用于mocha的自定义编译器以获得babel善良.

我的代码如下:

Block.jsx:

import React from 'react';
import {View} from 'react-native';

export default ({title, ui}) => (
  <View>
    Title: {title}
  </View>
);
Run Code Online (Sandbox Code Playgroud)

Block.test.js

import { shallow } from 'enzyme';
import { expect } from 'chai';
import {Block} from '../';
import React from 'react';

describe('<Block /> with props: title', () => {

  it('should have correct props', () => {
    expect(
      shallow(<Block title="Something" />).props()
    ).to.deep.equal( {title:"Something"} );
  });

  it('should have correct title', () => {
    expect(
      shallow(<Block title="Something" />).text()
    ).to.equal( "Something" );
  });

});
Run Code Online (Sandbox Code Playgroud)

检测结果

摩卡命令:

mocha --compilers js:./test/support/compiler.js --require react-native-mock/mock --recursive **/test/*.test.js --watch
Run Code Online (Sandbox Code Playgroud)

摩卡测试结果:

  <Block /> with props: title
    1) should have correct props
    2) should have correct title

  2 failing

  1) <Block /> with props: title should have correct props <Text />:

      AssertionError: expected { Object (children) } to equal { title: 'Something' }
      + expected - actual

       {
      -  "children": [
      -    "Title: "
      -    "Something"
      -  ]
      +  "title": "Something"
       }

      at Context.<anonymous> (components/test/Block.test.js:24:120)

  2) <Block /> with props: title should have correct title <Text />:

      AssertionError: expected '<View />' to equal 'Something'
      + expected - actual

      -<View />
      +Something

      at Context.<anonymous> (components/test/Block.test.js:28:119)
Run Code Online (Sandbox Code Playgroud)

意外的行为

  1. props() 似乎得到了正确的值,但格式与api描述的格式不同
  2. text()不会渲染节点textContent,而是渲染字符串标签" <View />"

替代方案:道具().儿童

给定组件:

import React from 'react';
import {View, Text} from 'react-native';

export default ({title, ui}) => (
  <View>
    <Text> The title...</Text>
    {title}
  </View>
);
Run Code Online (Sandbox Code Playgroud)

props().children 是数组 [<Text component instance>, "Something"]

所以跟随测试通过:

  it('should have correct props', () => {
    expect(
      shallow(<Block title="Something" />).props().children
    ).to.contain( "Something" );
  });
Run Code Online (Sandbox Code Playgroud)

为什么Enzyme API的行为与文档中描述的不同?

具体看文档shallow(<Block title="Something" />).text()应该等于什么样:The title...Something

我做错了什么,还是我正在使用的技术?

编辑1:其他问题

html(),render(),update()也似乎并没有与我的设置工作

编辑:React native仅适用shallow 于此刻

Ash*_*man 6

解决方案1:解决方案 textContent

来自Enzyme示例应用程序:

const title = "Blah";
const wrapper = shallow(<Block title={title} />);
expect(wrapper.length).to.equal(1);
expect(wrapper.contains(<Text>{title}</Text>)).to.equal(true);
Run Code Online (Sandbox Code Playgroud)

解决方案2:更具语义性

好的,替代品的语义版本更多:props().上面的孩子在下面.这次Github讨论也有所帮助.

Block.js:

import React from 'react';
import {View, Text} from 'react-native';

export default ({title, ui}) => (
  <View>
    <Text data={title}>{title}</Text>
  </View>
);
Run Code Online (Sandbox Code Playgroud)

Block.test.js:

  it('should have correct props', () => {
    const title = title;
    expect(
      shallow(<Block title={title} />)
         .find('Text') // Use selector to get certain children
         .first() // Get the first child
         .props() // Get its props
         .data
    ).to.equal(title)
  });
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.我尝试了解决方案2,它现在正在运行.但我把它改成了wrapper.find(Text).first().props().children (2认同)