react-native - 在 React Native 中使用 Jest 和 Enzym 进行单元测试
问题描述
如何用玩笑和酶找到组件内的元素?
假设,我有 1 个组件父组件(登录)和 2 个子组件(标题和表单),所以在组件登录中,我想找到表单组件内是否有 TextInput 元素,或者其他可能是表单组件内的另一个元素,开玩笑和酶,那么我怎样才能通过 1 个单元测试(Login.test.js)得到它?
这是我的 ilustration 登录组件
<Login>
<Title title='Login Page' />
<Form
email={this.state.email}
password={this.state.password}
/>
</Login>
标题组件
<Text>{this.props.title}</Text>
表单组件
<View>
<TextInput value={this.props.email} placeHolder="Your Email" />
<TextInput value={this.props.password} placeHolder="Your Password" />
</View>
这是我当前的 Login.test.js
import React from 'react';
import { shallow } from 'enzyme';
import Login from './Login';
import renderer from 'react-test-renderer';
describe('Login', () => {
const wrapper = shallow(<Login />);
const instaceOf = wrapper.instance();
it('renders correctly', () => {
const rendered = renderer.create(<Login />).toJSON();
expect(rendered).toBeTruthy();
});
it('should render the Text Input Element', () => {
let form = wrapper.find('Form');
expect(form.find('TextInput"]')).toHaveLength(2);
});
});
解决方案
推荐阅读
- django - Django 选择与存储在数据库中
- apache-spark - 如何更改 _spark_metadata 目录的位置?
- vue.js - Nuxt Vuex 商店 Cookie 问题
- r - 如何创建按顺序使用数据框中数据的分段图?
- typescript - TSLint 无法识别正确的按位运算符
- python - Python 数组 rows*1column numpy for numpy.dot(matrix,matrix2)
- javascript - React 组件无法读取未定义的属性
- python - 在 Raspbian 上手动安装 Python 库
- azure - Sql Server 中是否有办法查找视图中的哪些列未编入索引
- intellij-idea - 不要在 PhpStorm 中封装 else ifs