Skip to content

Instantly share code, notes, and snippets.

@vjwilson
Created August 3, 2018 15:15
Show Gist options
  • Save vjwilson/0c68f9505ba043b3a16b2dcfdc348fe6 to your computer and use it in GitHub Desktop.
Save vjwilson/0c68f9505ba043b3a16b2dcfdc348fe6 to your computer and use it in GitHub Desktop.
Testing a JavaScript framework on-the-cheap, with no test framework or test runner
if (getTimeRemaining(new Date('2018-07-29T03:24:45'), new Date('2018-07-29T03:24:45')) != '00:00') {
throw new Error('should return no time if end time is the same as the current time');
}
if (getTimeRemaining(new Date('2018-07-29T03:24:45'), new Date('2018-07-29T03:24:12')) !== '00:33') {
throw new Error('should returning the time difference when subtracting the second arg from the first');
}
if (getTimeRemaining(new Date('2018-07-29T03:24:45'), new Date('2018-07-29T03:04:36')) !== '20:09') {
throw new Error('should returning a time difference that includes minutes and seconds');
}
if (getTimeRemaining(new Date('2018-07-29T03:24:45'), new Date('2018-07-29T03:22:46')) !== '01:59') {
throw new Error('should returning a time difference where minutes less than 10');
}
function getTimeRemaining(stopTime, currentTime) {
const stopTimestamp = stopTime.getTime();
const currentTimestamp = currentTime.getTime();
const totalSecondsRemaining = ((stopTimestamp - currentTimestamp) / 1000);
const minutesRemaining = Math.floor(totalSecondsRemaining / 60);
const secondsRemaining = totalSecondsRemaining % 60;
const formattedMinutes = (`0${minutesRemaining}`).slice(-2);
const formattedSeconds = (`0${secondsRemaining}`).slice(-2);
return `${formattedMinutes}:${formattedSeconds}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment