小编sir*_*ain的帖子

反复提示用户,直到使用nodeJS async-await解析

我尝试向用户重复询问问题,直到他们使用此代码给出正确的答案.

问题是,如果用户第一次没有给出正确的答案,它将无法解决.

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function promptAge() {
    return new Promise(function(resolve){
        rl.question('How old are you? ', function(answer) {
            age = parseInt(answer);
            if (age > 0) {
                resolve(age);
            } else {
                promptAge();
            }
        });
    });
}

(async function start() {
    var userAge =  await promptAge();
    console.log('USER AGE: ' + userAge);
    process.exit();
})();
Run Code Online (Sandbox Code Playgroud)

以下是每种情况的终端输出:

当用户第一次给出正确答案时,它很好......

How old are you? 14
USER AGE: 14
Run Code Online (Sandbox Code Playgroud)

当用户给出了错误的答案时,它被卡住了(不会解决,处理也不会退出)......

How old are you? asd
How old are …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous node.js async-await

6
推荐指数
1
解决办法
925
查看次数

在React上使用Jest和Enzyme调用clearInterval的单元测试方法

我有这个React组件,并且它具有调用clearInterval来清除其他方法设置的间隔的方法

class SomeComponent extends React.Component {
  setIntervalMethod = () => {
    this.interval = setInterval(this.method, 1000)
  }
  claerIntervalMethod = () => {
    clearInterval(this.interval)
  }
  
  render = () => null
}
Run Code Online (Sandbox Code Playgroud)

我该如何测试那些方法?

编辑:添加我做过的测试

it('should call clearInterval()', () => {
  const mounted = shallow(<SomeComponent/>)
  const clearIntervalMethod = mounted.instance().clearIntervalMethod

  jest.useFakeTimers()
  clearIntervalMethod()
  expect(clearInterval).toHaveBeenCalledWith(expect.any(Function))
})
Run Code Online (Sandbox Code Playgroud)

我已经搜寻了好几天,尝试使用jest.useFakeTimers()和调用expect(clearInterval).toHaveBeenCalledWith(expect.any(Function), 1000)以及许多其他荒谬的方法来测试我忘记的这种方法,但都无济于事。

因此,...如果有人能在这里分享解决方案,并且愿意在这里分享我的心意,那么我可以度过愉快的笑容和充满喜悦的心情度过这个周末。

提前致谢。干杯!

javascript testing reactjs jestjs enzyme

3
推荐指数
1
解决办法
1510
查看次数