Javascript Jest-断言调用模拟函数回调

Javascript Jest-断言调用模拟函数回调,javascript,unit-testing,jestjs,mocking,Javascript,Unit Testing,Jestjs,Mocking,我试图实现对该函数的完全覆盖,但似乎无法模拟该函数,以便它调用onSuccess回调 代码 export const handleDelete = (action) => { const { id, type, actions } = action; actions.dispatch( actions.openDialog(`delete${type}`, Dialog, { onSuccess: () => { actions.disp

我试图实现对该函数的完全覆盖,但似乎无法模拟该函数,以便它调用
onSuccess
回调

代码

export const handleDelete = (action) => {
  const { id, type, actions } = action;

  actions.dispatch(
    actions.openDialog(`delete${type}`, Dialog, {
      onSuccess: () => {
        actions.dispatch(actions.thunk(id));
        actions.notify(`${type} deleted`, {
          variant: 'success',
        });
      },
      body: `Are you sure you want to delete this ${type}?`,
      title: `Delete ${type}`,
      confirm: 'Delete',
      cancel: 'Cancel',
      danger: true,
    })
  );
};
单元测试:

  it('should handle delete', () => {
    const mockDispatch = jest.fn();
    const mockOnSuccess = jest.fn();
    const mockOpenDialog = jest.fn(() => ({
      onSuccess: mockOnSuccess,
    }));
    const mockAction = {
      id: 1,
      type: 'example',
      actions: {
        dispatch: mockDispatch,
        openDialog: mockOpenDialog,
      },
    };

    handleDelete(mockAction);

    expect(mockDispatch).toHaveBeenCalled();
    expect(mockOpenDialog).toHaveBeenCalled();
  });
覆盖范围:

  it('should handle delete', () => {
    const mockDispatch = jest.fn();
    const mockOnSuccess = jest.fn();
    const mockOpenDialog = jest.fn(() => ({
      onSuccess: mockOnSuccess,
    }));
    const mockAction = {
      id: 1,
      type: 'example',
      actions: {
        dispatch: mockDispatch,
        openDialog: mockOpenDialog,
      },
    };

    handleDelete(mockAction);

    expect(mockDispatch).toHaveBeenCalled();
    expect(mockOpenDialog).toHaveBeenCalled();
  });
单元测试溶液:

index.ts

export const handleDelete=(操作)=>{
const{id,type,actions}=action;
const Dialog='Dialog';
行动、调度(
actions.openDialog(`delete${type}`,Dialog{
onSuccess:()=>{
actions.dispatch(actions.thunk(id));
actions.notify(`${type}已删除`{
变体:“成功”,
});
},
正文:`确实要删除此${type}吗?`,
标题:`Delete${type}`,
确认:“删除”,
取消:“取消”,
危险:没错,
}),
);
};
index.test.ts

从“/”导入{handleDelete};
描述('64803187',()=>{
它('应该通过',()=>{
常量动作={
id:'1',
键入:“用户”,
行动:{
dispatch:jest.fn(),
openDialog:jest.fn().mockImplementationOnce((类型、对话框、选项)=>{
options.onSuccess();
返回{type:'OPEN_DIALOG'};
}),
notify:jest.fn(),
thunk:jest.fn().mockReturnValueOnce({type:'DELETE_USER_SUCCESS'}),
},
};
Handledelet(行动);
expect(action.actions.dispatch).toBeCalledWith({type:'OPEN_DIALOG'});
expect(action.actions.openDialog).toBeCalledWith('deleteuser','Dialog','{
onSuccess:expect.any(函数),
正文:`您确定要删除此用户吗?`,
标题:`删除用户',
确认:“删除”,
取消:“取消”,
危险:没错,
});
expect(action.actions.dispatch).toBeCalledWith({type:'DELETE\u USER\u SUCCESS});
expect(action.actions.notify).toBeCalledWith('user deleted',{variant:'success'});
});
});
单元测试结果:

 PASS  src/stackoverflow/64803187/index.test.ts
  64803187
    ✓ should pass (8ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.267s, estimated 13s