如何为 React Native 编写 dispatch in jest 测试用例?

问题描述 投票:0回答:0
 React.useEffect(() => {
    branch.subscribe(({ params }) => {
      if (
        params &&
        params['+clicked_branch_link'] &&
        params['+match_guaranteed']
      ) {
        dispatch(updateBranchioDetails(params));
      }
    });
  }, []);

我已经使用了dispatch函数,并尝试为上面的代码写了一个测试用例

it('calls dispatch when branch.subscribe is called with valid params', () => {
    const dispatchMock = jest.fn();
    jest.spyOn(mockDispatch, 'useDispatch').mockReturnValue(dispatchMock);

    const mockParams = {
      '+clicked_branch_link': true,
      '+match_guaranteed': true,
    };

    branch.subscribe({ params: mockParams });

    expect(mockDispatch.useDispatch).toHaveBeenCalledWith(updateBranchioDetails(mockParams));
  });

以上是测试套件,当我尝试运行测试覆盖率时,我面临以下问题。

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

Expected: \[Function anonymous\]

Number of calls: 0
react-native unit-testing testing jestjs automated-tests
© www.soinside.com 2019 - 2024. All rights reserved.