Reactjs 努力模仿定制反应钩子

Reactjs 努力模仿定制反应钩子,reactjs,jestjs,react-hooks,enzyme,Reactjs,Jestjs,React Hooks,Enzyme,我想知道是否有人能帮我解决我的问题 试图模拟自定义钩子,但jest/Ezyme没有录制对函数的任何调用 我的测试: const mockHandleEditProtection = jest.fn(); const mockUseEditProtection = jest.fn(() => [null, mockHandleEditProtection]); jest.mock('../../common/useEditProtection', () => ({ __esMod

我想知道是否有人能帮我解决我的问题

试图模拟自定义钩子,但jest/Ezyme没有录制对函数的任何调用

我的测试:

const mockHandleEditProtection = jest.fn();
const mockUseEditProtection = jest.fn(() => [null, mockHandleEditProtection]);

jest.mock('../../common/useEditProtection', () => ({
  __esModule: true,
  default: () => mockUseEditProtection,
}));

describe('my test', () => {
  const wrapper = mount(<AComponent proposal={mockProposalDataWithEnrichedValues}/>)
  it('should call useEditProtection with proposal object', () => {
      expect(mockUseEditProtection).toBeCalledWith(mockProposalDataWithEnrichedValues);
  });

  it('should call edit protection when edit button is clicked', () => {
    wrapper.find(TableCell).at(4).find(Button).at(0).simulate('click');
    expect(mockHandleEditProtection).toBeCalled();
  });
})
const mockHandleEditProtection=jest.fn();
const mockUseEditProtection=jest.fn(()=>[null,mockHandleEditProtection]);
jest.mock('..//common/useEditProtection',()=>({
__艾斯莫杜勒:没错,
默认值:()=>mockUseEditProtection,
}));
描述('我的测试',()=>{
const wrapper=mount()
它('should call useEditProtection with proposal object',()=>{
expect(mockUseEditProtection).toBeCalledWith(MockProposalDataWithEnrichedValue);
});
它('单击编辑按钮时应调用编辑保护',()=>{
find(TableCell).at(4).find(Button).at(0).simulate('click');
expect(mockHandleEditProtection).toBeCalled();
});
})
一个钩子如何在组件中使用的基本示例

const AComponent = ({proposal}) => {
    const [EditWarning, editProtection] = useEditProtection(proposal);
    return <div>
        {EditWarning}
        <button onClick={editProtection}>Edit</button>
    </div>
};
constacomponent=({proposal})=>{
const[EditWarning,editProtection]=使用editProtection(建议);
返回
{EditWarning}
编辑
};

我很困惑,为什么它不工作,所以任何指针都是值得赞赏的

您是否已检查(1)模拟提案对象是否已实际交付给组件?和(2)显式设置钩子,查看它是否在spied mock函数中注册?@keremistan I have,检查来自模拟提议对象的值的其他测试通过(并在应该通过时失败),我在组件中添加了一个console.log来查看钩子解析为什么,它显示了mock对象,但是调用没有被注册:(你得到了什么,你期望得到什么?