reactjs - 如何测试酶中的按钮道具
问题描述
试图测试这个组件,我得到了这个
错误
TypeError:this.props.onItemAdded 不是函数
我已经引用了这个,但这个解决方案并不真正适用于我的问题
酶测试:TypeError:expect(...).find 不是函数
如果按钮是道具,我将如何测试按钮功能?
todo-add-item.test.js
import React from "react";
import { shallow } from "enzyme";
import TodoAddItem from './todo-add-item';
describe('Should render add item component', ()=> {
it('should render add item component', () => {
const wrapper = shallow(<TodoAddItem/>)
})
})
describe('Should simulate button click', ()=> {
it('should simulate button click', () => {
const wrapper =shallow(<TodoAddItem/>)
wrapper.find('button').simulate('click') // getting the type error here.
})
})
todo-add-item.js
import React, { Component } from 'react';
import './todo-add-item.css';
export default class TodoAddItem extends Component {
render() {
return (
<div className="todo-add-item">
<button
className="test-button btn btn-outline-secondary float-left"
onClick={() => this.props.onItemAdded('Hello world')}>
Add Item
</button>
</div>
);
}
}
应用程序.js
import React, { Component } from 'react';
import AppHeader from '../app-header';
import SearchPanel from '../search-panel';
import TodoList from '../todo-list';
import ItemStatusFilter from '../item-status-filter';
import TodoAddItem from '../todo-add-item';
import './app.css';
export default class App extends Component {
constructor() {
super();
this.createTodoItem = (label) => {
return {
label,
important: false,
done: false,
id: this.maxId++
}
};
this.maxId = 100;
this.state = {
todoData: [
this.createTodoItem('Drink Coffee'),
this.createTodoItem('Make Awesome App'),
this.createTodoItem('Have a lunch')
]
};
this.deleteItem = (id) => {
this.setState(({ todoData }) => {
const idx = todoData.findIndex((el) => el.id === id);
const newArray = [
...todoData.slice(0, idx),
...todoData.slice(idx + 1)
];
return {
todoData: newArray
};
});
};
this.addItem = (text) => {
const newItem = this.createTodoItem(text);
this.setState(({ todoData }) => {
const newArray = [
...todoData,
newItem
];
return {
todoData: newArray
};
});
};
this.onToggleImportant = (id) => {
console.log('toggle important', id);
};
this.onToggleDone = (id) => {
console.log('toggle done', id);
};
};
render() {
return (
<div className="todo-app">
<AppHeader toDo={ 1 } done={ 3 } />
<div className="top-panel d-flex">
<SearchPanel />
<ItemStatusFilter />
</div>
<TodoList
todos={ this.state.todoData }
onDeleted={ this.deleteItem }
onToggleImportant={ this.onToggleImportant }
onToggleDone={ this.onToggleDone } />
<TodoAddItem onItemAdded={ this.addItem } />
</div>
);
};
};
解决方案
您不会将任何道具传递给您的组件。
const wrapper =shallow(<TodoAddItem onItemAdded={() => jest.fn()}/>)
您可以使用 .props() 检查道具,例如:
console.log('props',wrapper.find('button').props());
推荐阅读
- cdn - 有没有办法比较从 2 个不同服务器提供的同一图像的响应时间?
- javascript - 如何在反应和表达中一次上传文件和json数据
- shell - 什么是:/usr/sbin/grub-probe: 错误:未能获得“覆盖”的规范路径?
- c# - 如何在 C#.net 中使用操作标签在本地启动 html 文件
- c# - 获取一周中的几天,一个月中的一天
- r - 使用 R 中的标签从 df 自动创建变量
- wordpress - 在电子邮件激活链接后启用和创建帐户
- css - 具有`margin 0:auto`的容器和具有`display:grid`的子级冲突
- amazon-web-services - AWS EMR 使用 Terraform 启动无法复制 /usr/lib/spark/jars 下的 jars
- google-sheets - 谷歌电子表格功能为日期列生成周数?