如何测试酵素中的纽扣道具

时间:2019-05-14 11:03:46

标签: reactjs jestjs enzyme

尝试测试此组件,并即时获取

错误

  

TypeError:this.props.onItemAdded不是函数

我已经引用了这个,但是这个解决方案并不能真正解决我的问题

Enzyme test: TypeError: expect(...).find is not a function

如果按钮是道具,我将如何测试按钮功能?

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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