Jestjs 使用多个选择测试redux传奇
我有一个选择函数的传奇故事,它应该从州政府获取一些信息,并使用它们转到新的url 传奇的代码:Jestjs 使用多个选择测试redux传奇,jestjs,redux-saga,Jestjs,Redux Saga,我有一个选择函数的传奇故事,它应该从州政府获取一些信息,并使用它们转到新的url 传奇的代码: export function* appRedirectToNewTopic() { const getCreatingNewTopicError = yield select(getCreatingTopicError); const newTopicId = yield select(getNewTopicId); const groupId = yield select(getGro
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,
}))));
});
});
我遇到的错误应将用户重定向到新主题,错误为期望值等于:
{“@@redux saga/IO”:true,“PUT”:{“action”:{“payload”:{“args”:[“/chat/[object object]/[object object]”],“method”:“push”},键入“@@router/CALL\u HISTORY\u method”},channel:null}
收到:
未定义
传递给下一个方法的值是当前的收益率
应该产生的值,而不是在该值之后使用相等值进行测试的值。试试这个:
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,
}))));
});
});
坦克斯·马丁。成功了。老实说,我不明白为什么它会起作用。你能再解释一下你的答案吗?或者给我指一些阅读材料。对不起,我只能给你25品脱。如果可以的话,我会给你更多:)这不是传奇故事的特例,而是一般意义上的发电机。我建议只玩一下生成器——你也可以在MDN这样的热门网站上阅读它们。也就是说,我将尝试进一步解释这个问题。记住生成器的哪个部分在哪个时刻执行是很重要的。当您创建一个生成器时,里面的任何代码都不会执行,在您第一次调用
next
时,生成器从开始到第一次执行。您从firstnext()
调用中获得的值是第一次生成的值。现在,当您第二次调用next
时,您可以将一个值传递给它,它将被用来代替第一个yield表达式,然后该函数将继续执行,并将这个新值传递给第二个yield表达式,依此类推