【问题标题】:How to call multiple setProps in enzyme?如何在酶中调用多个 setProps?
【发布时间】:2019-06-28 00:42:29
【问题描述】:

我有以下测试:

import React from 'react';
import { configure, shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

import ListAdapter from './ListAdapter'
import TweetCard from './TweetCard'


describe('<ListAdapter />', () => {
    let wrapper, props

    beforeEach(() => {
        props = {
            data: [{
                user: { profile_image_url: 'someimage.png', name: 'Some name' },
                created_at: 'Sat Feb 02 19:06:09 +0000 2019',
                text: 'Hello word'
            }],
        };
        wrapper = shallow(<ListAdapter  {...props} />);

    })

    it('renders without crashing', () => {
        expect(wrapper).toBeDefined();
    });

    it('renders one link anchor element', () => {
        expect(wrapper.find('button')).toHaveLength(1);
        expect(wrapper.find(TweetCard)).toHaveLength(0);
    });

    it('check anchor tag text when it gets data and than try to set props', () => {
        expect(wrapper.find('button').at(0).text()).toEqual('You have 1 tweet.');
        var { data } = wrapper.instance().props
        data = [data].concat({
            user: { profile_image_url: 'someimage.png', name: 'Some name' },
            created_at: 'Sat Feb 02 19:06:09 +0000 2019',
            text: 'Hello word'
        })
        wrapper = wrapper.setProps({ data })
        expect(wrapper.find('button').at(0).text()).toEqual('You have 2 tweets.');
        expect(wrapper.instance().props.data).toHaveLength(2)


    // UPDATED TEST TO GET 3 TWEETS
data = wrapper.instance().props
        data = [data].concat({
            user: { profile_image_url: 'someimage.png', name: 'Some name' },
            created_at: 'Sat Feb 02 19:06:09 +0000 2019',
            text: 'Hello word'
        })
        wrapper = wrapper.setProps({ data })
        expect(wrapper.find('button').at(0).text()).toEqual('You have 3 tweets.');
        expect(wrapper.instance().props.data).toHaveLength(3)
    });

    it('check state initialization defaults', () => {
        // we have one props getting passed so it will be 1
        expect(wrapper.state('data_count')).toEqual(1);
    });

    it('test onClick event of button', () => {
        // Try to click and than we should have toggleFlag be true and data_count be 0
        wrapper.find('button').simulate('click')
        expect(wrapper.state('data_count')).toEqual(0);
        expect(wrapper.find(TweetCard)).toHaveLength(1);
        expect(wrapper.find('button')).toHaveLength(0);
    });

})

而且,在测试中它说:check anchor tag text when it gets data and than try to set props 我想在添加第二个道具后添加更多道具,但我对第三个道具尝试了相同的方法,但它根本不影响!酶有时真的很棒,有时只是让它变得容易。我知道 setProps 也是一个异步调用。

有什么事吗?

谢谢

【问题讨论】:

  • 听起来你知道你的问题……如果它是异步的,你需要等待 setProps 完成。
  • setProps 不是异步的。你的测试用例会发生什么?我创建了简单的测试用例,其中 wrapper.setProps 使用更改的数组数据进行更新,并且一切都按预期工作。
  • @skyboyer 我已经更新了该测试用例以获得 3 个道具...请看一下,但它没有用。相反,它说You have 1 tweet
  • 我刚刚发现您指的是wrapper.instance。我使用wrapper.setProps()wrapper.props()。尝试使用相同的方式(虽然我相信它应该是相同的)
  • @skyboyer,我也试过了,它给出了未定义的比!

标签: reactjs jestjs enzyme react-props


【解决方案1】:

您可以使用setProps 多次更新道具,但您没有正确设置道具,即您没有正确使用concat 方法。你也不需要设置setProps的返回值

it('check anchor tag text when it gets data and than try to set props', () => {
    expect(wrapper.find('button').at(0).text()).toEqual('You have 1 tweet.');
    var { data } = wrapper.instance().props
    data = data.concat({
        user: { profile_image_url: 'someimage.png', name: 'Some name' },
        created_at: 'Sat Feb 02 19:06:09 +0000 2019',
        text: 'Hello word'
    }) // data here is now an array of objects
    wrapper.setProps({ data })
    expect(wrapper.find('button').at(0).text()).toEqual('You have 2 tweets.');
    expect(wrapper.instance().props.data).toHaveLength(2)

    data = wrapper.instance().props
    data = data.concat({
        user: { profile_image_url: 'someimage.png', name: 'Some name' },
        created_at: 'Sat Feb 02 19:06:09 +0000 2019',
        text: 'Hello word'
    })
    wrapper.setProps({ data })
    expect(wrapper.find('button').at(0).text()).toEqual('You have 3 tweets.');
    expect(wrapper.instance().props.data).toHaveLength(3)
});

【讨论】:

  • @Maverick,上述解决方案是否适合您。你能确认一下吗
【解决方案2】:

其实不用再打wrapper了。

wrapper.setProps({ data }) 而不是wrapper = wrapper.setProps({ data })

你写得对,但你的data 已经是数组,然后你用[data].concat 而不是data.concat。但我不确定这是否是问题所在。

【讨论】:

  • 我已经更新了该测试用例以获得 3 个道具...请看一下,但它没有用。相反,它说You have 1 tweet
  • data = wrapper.instance().props 它获取 props 但不是 props.data 您应该更改为 data = wrapper.instance().props.data
  • 能否请您 console.log(wrapper.instance().props) 或调试我想看看显示什么
  • 这是一件更烦人的事情,console.log 有时根本不起作用!
  • :)))) 你使用 webstorm 吗?你可以轻松调试
猜你喜欢
  • 2017-07-19
  • 2014-09-28
  • 1970-01-01
  • 2018-09-12
  • 2016-12-30
  • 2020-10-10
  • 2021-09-23
  • 2021-07-22
  • 2019-04-21
相关资源
最近更新 更多