2021-04-22 10:07:10 +02:00
|
|
|
import timer from './timer';
|
2019-08-04 11:10:51 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
function timeout(fn, ms): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
return new Promise((resolve) =>
|
2019-08-04 11:10:51 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
fn();
|
|
|
|
resolve();
|
2020-04-14 22:29:11 +02:00
|
|
|
}, ms),
|
2019-08-04 11:10:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should calculate the correct time in seconds', () => {
|
|
|
|
expect(timer.seconds([1, 0])).toBe(1);
|
|
|
|
expect(timer.seconds([0, 1e6])).toBe(0.001);
|
|
|
|
expect(timer.seconds([1, 1e6])).toBe(1.001);
|
|
|
|
expect(timer.seconds([1, 552])).toBe(1.000000552);
|
2019-08-04 11:10:51 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('timer should track the time', async () => {
|
2022-09-06 13:22:41 +02:00
|
|
|
jest.useFakeTimers();
|
2019-08-04 11:10:51 +02:00
|
|
|
const tt = timer.new();
|
|
|
|
let diff;
|
2021-05-28 11:10:24 +02:00
|
|
|
timeout(() => {
|
2019-08-04 11:10:51 +02:00
|
|
|
diff = tt();
|
|
|
|
}, 20);
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.advanceTimersByTime(20);
|
|
|
|
expect(diff).toBeGreaterThan(0.0019);
|
|
|
|
expect(diff).toBeLessThan(0.05);
|
|
|
|
jest.useRealTimers();
|
2019-08-04 11:10:51 +02:00
|
|
|
});
|