Enzyme 的 shallow().text() 和 React Native 没有像我预期的那样工作
Enzyme's shallow().text() with React Native doesn't work as I expected
我正试图通过 enzyme and react-native-mock 对 React Native 测试有一些基本的了解。
下面不包括:用于 mocha 的自定义编译器,以获得 babel 的优点。
我的代码如下:
Block.jsx
:
import React from 'react';
import {View} from 'react-native';
export default ({title, ui}) => (
<View>
Title: {title}
</View>
);
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" );
});
});
测试结果
摩卡命令:
mocha --compilers js:./test/support/compiler.js --require react-native-mock/mock --recursive **/test/*.test.js --watch
摩卡测试结果:
<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)
意外行为
props()
似乎得到了正确的值,但格式与 api 描述的不同
text()
不呈现节点 textContent
,而是呈现字符串化标签“<View />
”
备选方案:props().children
给定组件:
import React from 'react';
import {View, Text} from 'react-native';
export default ({title, ui}) => (
<View>
<Text> The title...</Text>
{title}
</View>
);
props().children
是数组[<Text component instance>, "Something"]
所以下面的测试通过了:
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).props().children
).to.contain( "Something" );
});
问题
为什么 Enzyme API 的行为与文档中描述的不同?
具体查看文档 shallow(<Block title="Something" />).text()
应该等于 something 比如:The title...Something
我做错了什么,还是我使用的技术之一?
编辑 1:其他问题
html()
、render()
、update()
似乎也不适用于我的设置
编辑:React native 仅适用于 shallow
at the moment
您可以参考您要测试的具体道具:
expect(
shallow(<Block title="Something" />).prop('title')
).to.equal( "Something" );
text() 并没有按照您的想法行事。看看文档中的第二个例子,shallow 不会渲染你的 <View>
标签
解决方案 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);
解决方案 2:更语义化
好的备选方案的更多语义版本:上面的 props().children 如下。这 Github discussion 也有帮助。
Block.js
:
import React from 'react';
import {View, Text} from 'react-native';
export default ({title, ui}) => (
<View>
<Text data={title}>{title}</Text>
</View>
);
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)
});
另一种解决方案(与props().children
非常相似)是在prop
中访问children
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).prop('children')
).toBe( "Something" );
});
你可以使用expect(wrapper.find(Foo).render().text()).to.equal('Hello World')
技巧
我正试图通过 enzyme and react-native-mock 对 React Native 测试有一些基本的了解。
下面不包括:用于 mocha 的自定义编译器,以获得 babel 的优点。
我的代码如下:
Block.jsx
:
import React from 'react';
import {View} from 'react-native';
export default ({title, ui}) => (
<View>
Title: {title}
</View>
);
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" );
});
});
测试结果
摩卡命令:
mocha --compilers js:./test/support/compiler.js --require react-native-mock/mock --recursive **/test/*.test.js --watch
摩卡测试结果:
<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)
意外行为
props()
似乎得到了正确的值,但格式与 api 描述的不同
text()
不呈现节点textContent
,而是呈现字符串化标签“<View />
”
备选方案:props().children
给定组件:
import React from 'react';
import {View, Text} from 'react-native';
export default ({title, ui}) => (
<View>
<Text> The title...</Text>
{title}
</View>
);
props().children
是数组[<Text component instance>, "Something"]
所以下面的测试通过了:
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).props().children
).to.contain( "Something" );
});
问题
为什么 Enzyme API 的行为与文档中描述的不同?
具体查看文档 shallow(<Block title="Something" />).text()
应该等于 something 比如:The title...Something
我做错了什么,还是我使用的技术之一?
编辑 1:其他问题
html()
、render()
、update()
似乎也不适用于我的设置
编辑:React native 仅适用于 shallow
at the moment
您可以参考您要测试的具体道具:
expect( shallow(<Block title="Something" />).prop('title') ).to.equal( "Something" );
text() 并没有按照您的想法行事。看看文档中的第二个例子,shallow 不会渲染你的
<View>
标签
解决方案 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);
解决方案 2:更语义化
好的备选方案的更多语义版本:上面的 props().children 如下。这 Github discussion 也有帮助。
Block.js
:
import React from 'react';
import {View, Text} from 'react-native';
export default ({title, ui}) => (
<View>
<Text data={title}>{title}</Text>
</View>
);
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)
});
另一种解决方案(与props().children
非常相似)是在prop
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).prop('children')
).toBe( "Something" );
});
你可以使用expect(wrapper.find(Foo).render().text()).to.equal('Hello World')
技巧