我正在尝试为react-countdown-now计时器编写笑话测试。它应该很简单,但是我对开玩笑还是陌生的,我很难编写它。
import React from 'react';
import ReactDOM from 'react-dom';
import Countdown, { zeroPad } from 'react-countdown-now';
import "./TimeOut.scss";
const renderer = ({ minutes, seconds, completed }) => {
return <div><span>{zeroPad(minutes, 2)}:{zeroPad(seconds, 2)}</span></div>;
};
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = {
timeout: props.timeout * 60 * 1000
};
}
render() {
return (
<Countdown
date={Date.now() + this.state.timeout}
renderer={renderer}/>
);
}
}
export default Timer;
答案 0 :(得分:1)
在我的代码中,我有一个函数WaitFor(),它应该暂停代码并延迟几秒钟。为了进行测试,我然后做:
describe('WaitFor Testing', () => {
it('should take at least 3 seconds to execute the test', () => {
const Start:number = moment.now();
WaitFor(3)
const Duration = (moment.now() - Start);
expect(Duration).toBeGreaterThan(2000);
expect(Duration).toBeLessThan(10000);
});
});
这将导致代码等待,并确保时钟至少等待了近三秒钟。
对于Jest,您可以关注Jest Timer Mocks
// __tests__/timerGame-test.js
'use strict';
jest.useFakeTimers();
test('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
timerGame();
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000);
});