首页 > 解决方案 > 如何测试酶中的按钮道具

问题描述

试图测试这个组件,我得到了这个

错误

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>
        );
    };
};

标签: reactjsjestjsenzyme

解决方案


您不会将任何道具传递给您的组件。

const wrapper =shallow(<TodoAddItem onItemAdded={() => jest.fn()}/>)

您可以使用 .props() 检查道具,例如:

console.log('props',wrapper.find('button').props());

推荐阅读