Javascript 测试:模拟调度和获取状态

发布于 2025-01-11 05:48:02 字数 1998 浏览 0 评论 0原文

测试应检查是否使用当前用户 ID 12345 调用 inbox.get 来满足 currentuser 过滤条件

当我运行测试时,无法输入 return(dispatch, getstate),我在此处仅添加了两个控制台日志 Console.log(-------- -test 1------------) 正在打印。第二个不是。所以我认为问题在于我如何嘲笑 dispatchgetState

代码:

function loadInboxSummary(paginationOffset) 
  Console.log(--------test 1-----------);
  {return (dispatch, getState) => {
    Console.log(--------testing inside 2 ---------);
    const state = getState();
    const userId = getCurrentUserId(state);
    const filterCriteria = getCurrentFilter(state);

    const adjustedInboxViewFilter = {
      ...filterCriteria,
      assignedUserIds: mapCurrentAssignee(userId),
    };

    return InboxItems.get(
      userId,
      adjustedInboxViewFilter,
      paginationOffset,
    ) ................

测试调用 InboxItems.get(..) 时,它具有 adjustmentInboxView 的正确参数。

测试:

describe("loadInboxSummary", () => {
  const dispatch = jest.fn();
  const getState = jest.fn().mockReturnValue("mock_state");

  beforeEach(() => {
    jest.spyOn(getCurrentUserDetails, "getCurrentUserId").mockReturnValue("12345");
    jest.spyOn(getCurrentFilter, "default").mockReturnValue({assignedUserIds: ["currentUser"]});
    jest.spyOn(InboxItems, "get").mockResolvedValue({});
  });

  it("calls InboxItems.get with current user id", () => {
    loadInboxSummary("mock_pagination")(dispatch, getState);
    const adjustedFilters = {
      assignedUserIds: ["12345"],
    };

    expect(InboxItems.get).toHaveBeenCalledWith(
      "12345",
      adjustedFilters,
      "mock_pagination",
    ); }); });
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "12345", {"assignedUserIds": ["12345"]}, "mock_pagination"
Number of calls: 0

这是我第一次进行前端测试,我搜索了堆栈溢出以寻找可能的解决方案,但它们看起来完全不同/具有异步函数,所以我无法理解它们。非常感谢一些帮助。不确定是什么原因导致此错误,因此请随时提出问题。

The test should be checking if inbox.get is getting called with current user id 12345 for currentuser filter criteria.

When I run the tests, it's not able to enter return(dispatch, getstate), out of the two console logs that i added here only Console.log(--------test 1-----------) is being printed. Second one is not. So im thinking that the problem is with how im mocking dispatch and getState.

Code:

function loadInboxSummary(paginationOffset) 
  Console.log(--------test 1-----------);
  {return (dispatch, getState) => {
    Console.log(--------testing inside 2 ---------);
    const state = getState();
    const userId = getCurrentUserId(state);
    const filterCriteria = getCurrentFilter(state);

    const adjustedInboxViewFilter = {
      ...filterCriteria,
      assignedUserIds: mapCurrentAssignee(userId),
    };

    return InboxItems.get(
      userId,
      adjustedInboxViewFilter,
      paginationOffset,
    ) ................

Testing when InboxItems.get(..) is called, it has correct parameters for adjustedInboxView.

Test:

describe("loadInboxSummary", () => {
  const dispatch = jest.fn();
  const getState = jest.fn().mockReturnValue("mock_state");

  beforeEach(() => {
    jest.spyOn(getCurrentUserDetails, "getCurrentUserId").mockReturnValue("12345");
    jest.spyOn(getCurrentFilter, "default").mockReturnValue({assignedUserIds: ["currentUser"]});
    jest.spyOn(InboxItems, "get").mockResolvedValue({});
  });

  it("calls InboxItems.get with current user id", () => {
    loadInboxSummary("mock_pagination")(dispatch, getState);
    const adjustedFilters = {
      assignedUserIds: ["12345"],
    };

    expect(InboxItems.get).toHaveBeenCalledWith(
      "12345",
      adjustedFilters,
      "mock_pagination",
    ); }); });
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "12345", {"assignedUserIds": ["12345"]}, "mock_pagination"
Number of calls: 0

This is my first time doing front end testing, I searched stack overflow for possible solutions but they looked quite different/had async functions so I couldn't understand them. Would really appreciate some help. Not sure what could be causing this error so feel free to ask questions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文