我对原生、酶和jest的反应是新的。我正在尝试进行一个简单的测试,来测试子节点。(也许这是一种不正确的尝试方式)。
我的组件是:
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
class MyComponent extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View >
<Button title="My Component"/>
</View>
)
}
}
export default MyComponent;我的测试是
import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import MyComponent from '../components/MyComponent.js';
configure({ adapter: new Adapter() }) //setting up enzyme
const styles = require('../styles.js');
describe('rendering', () => {
it('checking View and Button exists', () => {
let wrapper
wrapper = shallow(<MyComponent/>);
expect(wrapper.find('View').children().find('Button')).toHaveProperty('title','My Component')
});
})
});我收到一个错误,返回的对象与预期的不匹配:
Expected the object:
< listing of full object...>
To have a nested property:
"title"
With a value of:
"My Component"返回的对象将MyComponent显示为根视图的子级,以及属性,但它失败了。我应该以不同的方式来做这件事吗?我希望能够创建一个测试结构,最终确认View组件下的一些子组件和道具。(顺便说一句,我更喜欢使用Mocha,但我遇到了this error,我还没能解决这个问题。
发布于 2017-11-23 11:13:43
这另一个问题帮助我回答了我的问题https://stackoverflow.com/a/46546619/4797507 (如果我没有正确地给予信任,很抱歉)
我的解决方案是:
expect(wrapper.find('View').children().find('Button').get(0).props.title).toEqual('My Component')
https://stackoverflow.com/questions/47407912
复制相似问题