template testing

This commit is contained in:
cv
2021-06-10 23:31:08 +02:00
parent 780c355a10
commit 30c6592767
4 changed files with 2498 additions and 12 deletions
+16
View File
@@ -0,0 +1,16 @@
import { stringFromMillis } from '../../src/utils/time';
const t1 = { val: null, result: '...' };
test('test stringFromMillis() on null values', () => {
expect(stringFromMillis(t1.val)).toBe(t1.result);
});
const t2 = { val: 3600000, result: '01:00:00' };
test('test stringFromMillis() on valid millis', () => {
expect(stringFromMillis(t2.val)).toBe(t2.result);
});
const t3 = { val: -3600000, result: '-01:00:00' };
test('test stringFromMillis() on negative millis', () => {
expect(stringFromMillis(t3.val)).toBe(t3.result);
});
+5 -1
View File
@@ -16,6 +16,10 @@
"socket.io": "^4.0.0"
},
"scripts": {
"start": "node src/app.js"
"start": "node src/app.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"devDependencies": {
"jest": "^27.0.4"
}
}
+17 -5
View File
@@ -1,3 +1,12 @@
/**
* @description Converts milliseconds to string representing time
* @param {number} ms - time in milliseconds
* @param {boolean} showSeconds - wether to show the seconds
* @param {string} delim - character between HH MM SS
* @param {string} ifNull - what to return if value is null
* @returns {string} String representing time 00:12:02
*/
export const stringFromMillis = (
ms,
showSeconds = true,
@@ -5,14 +14,17 @@ export const stringFromMillis = (
ifNull = '...'
) => {
if (ms === null || isNaN(ms)) return ifNull;
const isNegative = ms < 0 ? '-' : '';
const millis = Math.abs(ms);
const showWith0 = (value) => (value < 10 ? `0${value}` : value);
const hours = showWith0(Math.floor(((ms / (1000 * 60 * 60)) % 60) % 24));
const minutes = showWith0(Math.floor((ms / (1000 * 60)) % 60));
const seconds = showWith0(Math.floor((ms / 1000) % 60));
const hours = showWith0(Math.floor(((millis / (1000 * 60 * 60)) % 60) % 24));
const minutes = showWith0(Math.floor((millis / (1000 * 60)) % 60));
const seconds = showWith0(Math.floor((millis / 1000) % 60));
return showSeconds
? `${
? `${isNegative}${
parseInt(hours) ? `${hours}${delim}` : `00${delim}`
}${minutes}${delim}${seconds}`
: `${parseInt(hours) ? `${hours}` : '00'}${delim}${minutes}`;
: `${isNegative}${parseInt(hours) ? `${hours}` : '00'}${delim}${minutes}`;
};
+2460 -6
View File
File diff suppressed because it is too large Load Diff