使用多个选择测试 redux-saga

Testing redux-saga with multiple selects

我有一个带有 select 函数的 saga,应该从状态中获取一些信息并使用它们转到新的 url。

传奇代码:

export function* appRedirectToNewTopic() {
  const getCreatingNewTopicError = yield select(getCreatingTopicError);
  const newTopicId = yield select(getNewTopicId);
  const groupId = yield select(getGroupId);
  if (isEmpty(getCreatingNewTopicError)) { // this is lodash isEmpty
    yield put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
      groupId,
      topicId: newTopicId,
    })));
  } // TODO add here when notification is made
}

SAGA 测试代码:

describe('appRedirectToNewTopic', () => {
  const action = appCreateNewTopicFinishedAction();
  const generator =
    cloneableGenerator(appRedirectToNewTopic)(action);
  const expectedGroupId = {
    apiGroups: {
      groupInfo: {
        groupInfo: {
          id: 466,
        },
      },
    },
  };
  const expectedTopicId = {
    apiTopics: {
      newTopicId: 22466,
    },
  };
  let topicId;
  let groupId;

  it('should return empty error', () => {
    expect(generator.next().value)
      .toEqual(select(getCreatingTopicError));
  });

  it('should return new topicId', () => {
    topicId = generator.next(expectedTopicId).value;
    expect(topicId)
      .toEqual(select(getNewTopicId));
  });

  it('should return groupId', () => {
    groupId = generator.next(expectedGroupId).value;
    expect(groupId)
      .toEqual(select(getGroupId));
  });

  it('should redirect user to new topic screen', () => {
    expect(generator.next().value)
      .toEqual(put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
        groupId,
        topicId,
      }))));
  });
});

我得到的错误是应该将用户重定向到新主题,错误是 Expected value to equal: {"@@redux-saga/IO": true, "PUT": {"action": {"payload": {"args": ["/chat/[object Object]/[object Object]"], "method": "push"}, "type": "@@router/CALL_HISTORY_METHOD"}, "channel": null}} Received: undefined

您传递给下一个方法的值是当前 yield 应该产生的值,而不是您之后测试的值。试试这个:

describe('appRedirectToNewTopic', () => {
  const action = appCreateNewTopicFinishedAction();
  const generator =
    cloneableGenerator(appRedirectToNewTopic)(action);
  const expectedGroupId = {
    apiGroups: {
      groupInfo: {
        groupInfo: {
          id: 466,
        },
      },
    },
  };
  const expectedTopicId = {
    apiTopics: {
      newTopicId: 22466,
    },
  };
  let topicId;
  let groupId;

  it('should return empty error', () => {
    expect(generator.next().value)
      .toEqual(select(getCreatingTopicError));
  });

  it('should return new topicId', () => {
    topicId = generator.next().value;
    expect(topicId)
      .toEqual(select(getNewTopicId));
  });

  it('should return groupId', () => {
    groupId = generator.next(expectedTopicId).value;
    expect(groupId)
      .toEqual(select(getGroupId));
  });

  it('should redirect user to new topic screen', () => {
    expect(generator.next(expectedGroupId).value)
      .toEqual(put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
        groupId,
        topicId,
      }))));
  });
});