specs for timer

This commit is contained in:
AlecM33
2022-02-26 03:43:15 -05:00
parent 1cacaf511f
commit 6d5b74d9d9
3 changed files with 63 additions and 7 deletions

View File

@@ -0,0 +1,55 @@
// TODO: clean up these deep relative paths? jsconfig.json is not working...
const LOG_LEVEL = require("../../../../server/config/globals.js").LOG_LEVEL;
const ServerTimer = require('../../../../server/modules/ServerTimer.js');
const logger = require('../../../../server/modules/Logger.js')(LOG_LEVEL.DEBUG);
describe('ServerTimer', () => {
let serverTimer;
it ('should run a timer for the specified time', async () => {
serverTimer = new ServerTimer(0, 0.001, 10, logger);
spyOn(global, 'clearTimeout');
await serverTimer.runTimer(false).then(() => {
expect(serverTimer.currentTimeInMillis).toBeLessThanOrEqual(0);
expect(clearTimeout).toHaveBeenCalledWith(serverTimer.ticking);
}).catch((e) => {
fail(e);
});
});
it ('should stop the timer', () => {
serverTimer = new ServerTimer(1, 0, 10, logger);
spyOn(global, 'clearTimeout');
serverTimer.runTimer(false).then(() => {
fail();
}).catch((e) => {
fail(e);
});
serverTimer.stopTimer();
expect(clearTimeout).toHaveBeenCalledWith(serverTimer.ticking);
});
it ('should stop and resume the timer', async () => {
serverTimer = new ServerTimer(0, 1, 10, logger);
spyOn(global, 'clearTimeout');
serverTimer.runTimer(false).then(() => {
fail();
}).catch((e) => {
fail(e);
});
await new Promise(function (resolve) {
setTimeout(function () {
serverTimer.stopTimer();
let timeRemainingWhenStopped = serverTimer.currentTimeInMillis;
serverTimer.resumeTimer();
expect(clearTimeout).toHaveBeenCalledTimes(1);
expect(serverTimer.totalTime).toEqual(timeRemainingWhenStopped);
serverTimer.stopTimer();
resolve();
}, 50);
});
});
});