Javascript 酶';s shallow().text()与React Native不';I don’我没料到会这样
我试图对React本地测试和/和有一些基本的了解 下面不包括:一个为摩卡定制的编译器,用于获得巴别塔精华 我的代码如下:Javascript 酶';s shallow().text()与React Native不';I don’我没料到会这样,javascript,reactjs,react-native,mocha.js,enzyme,Javascript,Reactjs,React Native,Mocha.js,Enzyme,我试图对React本地测试和/和有一些基本的了解 下面不包括:一个为摩卡定制的编译器,用于获得巴别塔精华 我的代码如下: Block.jsx: import React from 'react'; import {View} from 'react-native'; export default ({title, ui}) => ( <View> Title: {title} </View> ); 测试结果 摩卡指挥部: mocha --comp
Block.jsx
:
import React from 'react';
import {View} from 'react-native';
export default ({title, ui}) => (
<View>
Title: {title}
</View>
);
测试结果
摩卡指挥部:
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().children
是数组[,“某物”]
因此,以下测试通过:
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).props().children
).to.contain( "Something" );
});
it('应该有正确的道具',()=>{
期待(
浅()道具()儿童
).包含(“某物”);
});
问题:
为什么酶API的行为与文档中描述的不同
特别是查看文档shallow().text()
应该等于:标题…某物
是我做了一些不正确的事情,还是我正在使用的技术
编辑1:其他问题
html()
,render()
,update()
似乎也不适合我的设置
EDIT:React native仅适用于shallow
expect(
shallow().prop('title')
).使…相等(“某物”)代码>
标记textContent
来自酶示例应用程序:
const title = "Blah";
const wrapper = shallow(<Block title={title} />);
expect(wrapper.length).to.equal(1);
expect(wrapper.contains(<Text>{title}</Text>)).to.equal(true);
Block.test.js
:
import React from 'react';
import {View, Text} from 'react-native';
export default ({title, ui}) => (
<View>
<Text data={title}>{title}</Text>
</View>
);
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)
});
it('应该有正确的道具',()=>{
常量标题=标题;
期待(
浅()
.find('Text')//使用选择器获取某些子项
.first()//获取第一个子项
.props()//获取其道具
.数据
).至.相等(头衔)
});
另一种解决方案(非常类似于props().children
)是访问prop中的子对象
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).prop('children')
).toBe( "Something" );
});
it('应该有正确的道具',()=>{
期待(
shallow().prop('children')
).托比(“某物”);
});
您可以使用expect(wrapper.find(Foo.render().text()).to.equal('Hello World')
技巧只需澄清一点,mocha的.to.equal
执行“三重相等”检查,这意味着两个对象将不会是==
,除非它们是对内存中同一对象的引用。相反,在比较对象时使用.to.deep.equal
。啊,是的,这可能会产生误导,我现在就改变它。但是是的,没有shallow().text()
返回渲染的内容。相反,请始终使用.props().children
@ZekeDroid我不懂^,因为这就是我阅读文档片段expect(shall(important).text()).to.equal('important')代码>啊,是的,但读上面一点,下面的例子:返回当前渲染树的渲染文本字符串。如果使用此函数来测试组件的实际HTML输出,则应持怀疑态度看待此函数。
只有在没有React组件的情况下,此函数才有效。一旦执行此操作,它将返回组件的.toString
方法,该方法通常只是名称。1。嗯,那也不行。这就是问题所在,如果你看一下我的输出——它实际上正在这样做。这当然令人困惑,谢谢。我尝试了解决方案2,现在它正在工作。但是我把它改为wrapper.find(Text).first().props().childrenSolution 1应该使用。containsMatchingElement
,。contains
对我不起作用,可能已经过时了。
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)
});
it('should have correct props', () => {
expect(
shallow(<Block title="Something" />).prop('children')
).toBe( "Something" );
});