代码之家  ›  专栏  ›  技术社区  ›  Lazar Nikolic

用多重选择测试redux传奇

  •  0
  • Lazar Nikolic  · 技术社区  · 6 年前

    我有一个传奇与选择功能,应该采取从国家的信息夫妇,并使用他们去到新的网址。

    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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Martin Kadlec    6 年前

    传递给下一个方法的值是当前 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,
          }))));
      });
    });