scratch-vm/test/unit/util_timer.js

43 lines
942 B
JavaScript
Raw Normal View History

const test = require('tap').test;
const Timer = require('../../src/util/timer');
2016-04-18 17:20:30 -04:00
test('spec', t => {
const timer = new Timer();
2016-04-18 17:20:30 -04:00
t.type(Timer, 'function');
t.type(timer, 'object');
t.type(timer.startTime, 'number');
t.type(timer.time, 'function');
t.type(timer.start, 'function');
t.type(timer.timeElapsed, 'function');
2016-04-18 17:20:30 -04:00
t.end();
});
test('time', t => {
const timer = new Timer();
const time = timer.time();
2016-04-18 17:20:30 -04:00
t.ok(Date.now() >= time);
t.end();
});
test('start / timeElapsed', t => {
const timer = new Timer();
const delay = 100;
2017-08-26 13:07:47 -04:00
const threshold = 1000 / 60; // 60 hz
2016-04-18 17:20:30 -04:00
// Start timer
timer.start();
// Wait and measure timer
setTimeout(() => {
const timeElapsed = timer.timeElapsed();
t.ok(timeElapsed >= 0);
t.ok(timeElapsed >= (delay - threshold) &&
timeElapsed <= (delay + threshold));
2016-04-18 17:20:30 -04:00
t.end();
}, delay);
});