我是新手,对天然,酶和开玩笑。我正在尝试进行简单的测试工作,以测试子节点。(也许这是尝试这样做的一种不正确的方法)。
我的组件是:
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作为root视图的孩子以及道具,但失败了。我应该做不同的事情吗?我希望能够创建一个测试结构,该结构最终将确认视图组件下的许多儿童组件和道具。(作为附带说明,我更喜欢使用摩卡咖啡,但是我要解决这个错误,但我无法解决。
这个另一个问题帮助我回答了我的问题https://stackoverflow.com/a/46546619/4797507(如果我不正确地给予信用,歉意)
)解决方案是我使用的:
expect(wrapper.find('View').children().find('Button').get(0).props.title).toEqual('My Component')