admin管理员组文章数量:1392068
After user click login, I need check the server status every 3 seconds. After 20 times check, display timeout and clear the timer.
my code:
onCheckStatus = () => {
MAX_POLLING_COUNT = 20;
this.timer = setInterval(() => {
if (MAX_POLLING_COUNT > 0) {
MAX_POLLING_COUNT -= 1;
loginStatus(this.state.sessionId).then(
res => {
const { goto, sessionId, errorMessageCode, result, hint } = res;
........
if (goto === 'SUCCESS') {
this.setState({
messageInfo: {
type: 'success',
title: '',
description: result.description,
},
});
} else if (goto === 'ERROR') {
}
},
() => {
clearInterval(this.timer);
this.setState({ error: true, loading: false });
}
);
} else {
this.setState({ error: true, loading: false });
}
}, 3000);
};
test code:
jest.useFakeTimers();
const wrapper = shallow(
<AddGPForm step="GP_SIGNIN" uuid="testuuid" {...props} />
);
const form = shallow(wrapper.prop('children')(getMockFormApi(wrapper)));
form.find('Login').simulate('submit', {
reqestBody: '10x010101x0101x0101x10x0',
});
jest.runAllTimer();
// jest.runTimersToTime(60000);
// jest.runOnlyPendingTimers();
onCheckStatus will be triggered, the the codes inside the timer never trigged. I tried to pick the logic inside the timer as one single method.
new code:
onCheckStatus = () => {
this.timer = setInterval(this.check(), 3000);
}
check method only be triggered once.
After user click login, I need check the server status every 3 seconds. After 20 times check, display timeout and clear the timer.
my code:
onCheckStatus = () => {
MAX_POLLING_COUNT = 20;
this.timer = setInterval(() => {
if (MAX_POLLING_COUNT > 0) {
MAX_POLLING_COUNT -= 1;
loginStatus(this.state.sessionId).then(
res => {
const { goto, sessionId, errorMessageCode, result, hint } = res;
........
if (goto === 'SUCCESS') {
this.setState({
messageInfo: {
type: 'success',
title: '',
description: result.description,
},
});
} else if (goto === 'ERROR') {
}
},
() => {
clearInterval(this.timer);
this.setState({ error: true, loading: false });
}
);
} else {
this.setState({ error: true, loading: false });
}
}, 3000);
};
test code:
jest.useFakeTimers();
const wrapper = shallow(
<AddGPForm step="GP_SIGNIN" uuid="testuuid" {...props} />
);
const form = shallow(wrapper.prop('children')(getMockFormApi(wrapper)));
form.find('Login').simulate('submit', {
reqestBody: '10x010101x0101x0101x10x0',
});
jest.runAllTimer();
// jest.runTimersToTime(60000);
// jest.runOnlyPendingTimers();
onCheckStatus will be triggered, the the codes inside the timer never trigged. I tried to pick the logic inside the timer as one single method.
new code:
onCheckStatus = () => {
this.timer = setInterval(this.check(), 3000);
}
check method only be triggered once.
Share Improve this question edited Dec 12, 2018 at 14:52 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jul 16, 2018 at 4:27 Gary WangGary Wang 911 gold badge1 silver badge6 bronze badges2 Answers
Reset to default 1Use jest.advanceTimersByTime to advance your timer and test in your test file.
Use jest.runAllTimers()
instead of jest.runAllTicks()
本文标签: javascriptJest how to test setIntervalStack Overflow
版权声明:本文标题:javascript - Jest: how to test setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744762327a2623825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论