Redux工具包的单元测试准备回调函数

dddzy1tm  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(108)

我的准则

addTodo: {
  reducer: (state: State, action: { payload: Record<string, string>; type: string }) => {
    state.todoMessage = action.payload.todoMessage;
    state.id = action.payload.id;
    state.completed = action.payload.completed;
  },
  prepare: (todoMessage: string) => {
    return {
      payload: { message: todoMessage, id: uuid(), completed: false }
    };
  }
},

我的Unit Test:

const action = {
  type: addTodo.type,
  payload: {
    message: 'buy milk',
    id: 'id',
    completed: false,
  },
};
const state = productDetailsReducer(reducerState, action);
expect(state.todoMessage).toEqual('buy milk');
expect(state.id).toEqual('id');
expect(state.completed).toBeFalsy();

我的问题是伊斯坦布尔显示prepare功能不包括在内。你知道怎么掩盖吗?先谢了
尝试搜索redux工具包文档,找不到任何相关内容

vxbzzdmp

vxbzzdmp1#

您应该在单元测试中调用addTodo操作,以便覆盖其代码。模拟uuid返回行列式id值。
示例:

// I'm assuming uuid npm package here, tweak for your specific use case
jest.mock('uuid', () => ({
  v4: jest.fn(() => '12345'),
}));

...

describe("todo slice", () => {
  ...

  it("should add todo", () => {
    const action = addTodo("buy milk");

    const state = productDetailsReducer(reducerState, action);

    expect(state).toEqual({
      todoMessage: "buy milk",
      id: "12345",
      completed: false,
    });
  });

  ...
});

相关问题