我如何测试这个自定义反应钩子

时间:2021-06-22 07:39:17

标签: reactjs testing jestjs react-hooks enzyme

import { useState } from 'react';

export function useTabs(initialState: string[]) {
  const [tabs] = useState(initialState);
  const [activeTab, setActiveTab] = useState(0);

  return {
    tabs,
    activeTab,
    setActiveTab,
  };
}

我试试这个:

import React from 'react';
import { shallow } from 'enzyme';
import PaymentMethods from '../index';

const mockedSetState = jest.fn();

describe('test PaymentMethods component', () => {
  jest.mock('../../../hooks/useTabs', () => ({
    useTabs: () => ({
      tabs: ['Полная оплата', 'В кредит', 'Подписка'],
      activeTab: 0,
      setActiveTab: mockedSetState,
    }),
  }));

  it('call mocked set state', () => {
    expect(mockedSetState).toHaveBeenCalledWith(1);
  });

  it('should render PaymentMethods component', () => {
    expect(shallow(<PaymentMethods />)).toMatchSnapshot();
  });
});

但我有错误:

  1. expect(jest.fn()).toHaveBeenCalledWith(...expected) 预期:1 调用次数:0

  2. 测试 PaymentMethods 组件 › 应呈现 PaymentMethods 组件 类型错误:useTabs_1.useTabs 不是函数

0 个答案:

没有答案