Created
August 3, 2018 15:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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