【问题标题】:how to jest test connected component which has api calls in componentDidMount如何开玩笑测试在componentDidMount中有api调用的连接组件
【发布时间】:2020-09-23 05:23:04
【问题描述】:

我正在尝试测试连接的组件,但它似乎没有在 componentDidMount 函数中进行 api 调用。我需要它来进行 api 调用,这样我就可以根据 api 调用返回的值来测试这个组件将如何呈现。 api 调用由 axios 使用 redux 操作进行。一切都存储在 redux 中。

这是我的测试

it('should dispatch an action on mount', () => {
        const component =  shallow(
            <ProcessingStatus store={store}/>
        );
        const didMount = jest.spyOn(component, 'componentDidMount');
        expect(didMount).toHaveBeenCalledTimes(1);

        //console.log(component.html())
        expect(store.dispatch).toHaveBeenCalledTimes(3);

    });

这是我组件中的componentDidMount

componentDidMount() {
        const {
            matches: { params: { id } },
            processingStatus,
            securityStatus,
            routingStatus,
            soxStatus,
            remedyStatus,
            user: {
                priv: {
                    my_sox_requests,
                    view_remedy
                }
            }
        } = this.props;
        let params = 'id=' + id;
        if(processingStatus !== undefined){
            processingStatus(params)
            .catch(thrown => {
                console.log(thrown);
            });
        }
        if(securityStatus !== undefined){
            securityStatus(params)
                .catch(thrown => {
                    console.log(thrown);
                });
        }
        if(routingStatus !== undefined){
            routingStatus(params)
                .catch(thrown => {
                    console.log(thrown);
                });
        }
        if(my_sox_requests && my_sox_requests === 'on' && soxStatus !== undefined){
            soxStatus(params)
                .catch(thrown => {
                    console.log(thrown);
                });
        }

        if(view_remedy && view_remedy === 'on' && remedyStatus !== undefined){
            remedyStatus(params)
                .catch(thrown => {
                    console.log(thrown);
                });
        }
    }

我得到的错误是

FAIL  tests/jest/components/common/ProcessingStatus/index.test.js
  <ProcessingStatus />
    ✓ should render with given state from Redux store (90ms)
    ✕ should dispatch an action on mount (7ms)

  ● <ProcessingStatus /> › should dispatch an action on mount

    Cannot spy the componentDidMount property because it is not a function; undefined given instead

      85 |             <ProcessingStatus store={store}/>
      86 |         );
    > 87 |         const didMount = jest.spyOn(component, 'componentDidMount');
         |                               ^
      88 |         expect(didMount).toHaveBeenCalledTimes(1);
      89 |
      90 |         //console.log(component.html())

      at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:841:15)
      at Object.<anonymous> (tests/jest/components/common/ProcessingStatus/index.test.js:87:31)

我尝试使用const didMount = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');,但我得到的错误是

  ● <ProcessingStatus /> › should dispatch an action on mount

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0

      85 |         );
      86 |         const didMount = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');
    > 87 |         expect(didMount).toHaveBeenCalledTimes(1);
         |                          ^
      88 |
      89 |         //console.log(component.html())
      90 |         expect(store.dispatch).toHaveBeenCalledTimes(3);

我设法测试了是否调用了 didmount,但不确定如何检查是否已进行 api 调用。

it('should run componentDidMount', () => {
        spy = jest.spyOn(ProcessingStatus.prototype, 'componentDidMount');
        component = mount(
            <ProcessingStatus store={store}/>
        );
        expect(spy).toHaveBeenCalledTimes(1);
    });

【问题讨论】:

    标签: react-redux jestjs connected-components


    【解决方案1】:

    我在这里搜索了一个类似的问题,发现您有一些订单错误:

    1. 你应该在 shallow(Component) 之前设置 spyOn componentDidMount
    2. 你应该要求在浅组件之后调用 Component.prototype.componentDidMount
    jest.spyOn(Component.prototype, 'componentDidMount')
    
    shallow(<Component/>)
    
    expect(Component.prototype.componentDidMount).toHaveBeenCalled();
    

    如果组件期望接收将要在componentDidMount内部调用的props函数,则应在浅组件之类时添加它们

    const mockExpectedFunction= jest.fn()
    shallow(<Component expectedFunction={mockExpectedFunction} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2021-01-09
      • 2018-02-25
      • 1970-01-01
      相关资源
      最近更新 更多