预期的模拟函数已被调用,但未调用

时间:2018-10-23 22:26:34

标签: reactjs unit-testing jasmine enzyme

我有一个测试,只是试图查看是否调用了onSearch。

it("calls the onSearch method when searching", () => {
    const testFilter = {
      filterIndex: "testFilter4",
      searchResults: [],
      selectedItems: [],
      isComplete: false,
      isSearchMultiSelect: true,
      entity: "Organization"
    };
    const spy = jest.fn();
    const component = shallow(
      <FilterSelect
        {...preloadedState.main}
        filter={testFilter}
        onSearch={spy}
      />
    );
    component.find("Select").simulate("search", "term1");
    expect(spy).toHaveBeenCalledWith("Organization", "testFilter4", "term1");
  });

我收到此错误:

expect(jest.fn()).toHaveBeenCalledWith(expected)

    Expected mock function to have been called with:
      ["Organization", "testFilter4", "term1"]
    But it was not called.

这也是“ component”的json

{ node:
         { nodeType: 'class',
           type:
            { [Function: Select]
              Option: [Function],
              OptGroup: [Function],
              defaultProps: [Object],
              propTypes: [Object],
              contextTypes: [Object] },
           props:
            { showSearch: true,
              placeholder: 'Select',
              value: [],
              onSelect: [Function: onSelect],
              disabled: false,
              notFoundContent: 'No results found',
              filterOption: false,
              onSearch: [Function: onSearch],
              children: [],
              prefixCls: 'ant-select',
              transitionName: 'slide-up',
              choiceTransitionName: 'zoom' },
           key: undefined,
           ref: null,
           instance: null,
           rendered: [] },
        type: 'Select',
        props:
         { showSearch: true,
           placeholder: 'Select',
           value: [],
           onSelect: [Function: onSelect],
           disabled: false,
           notFoundContent: 'No results found',
           filterOption: false,
           onSearch: [Function: onSearch],
           prefixCls: 'ant-select',
           transitionName: 'slide-up',
           choiceTransitionName: 'zoom' },
        children: null,
        '$$typeof': Symbol(react.test.json) }

这也是Select的onSearch部分:

onSearch={term => {
          this.handleSearch(filter.entity, filter.filterIndex, term);
        }}

浏览浅层Json'Select'并具有onSearch,一切似乎都很好...

不知道这里发生了什么,这让我发疯。 谢谢

1 个答案:

答案 0 :(得分:0)

出现了反跳现象,阻止了该测试的运行。

此问题已解决: https://gist.github.com/apieceofbart/d28690d52c46848c39d904ce8968bb27