Properly test roundTimeframe

This commit is contained in:
Matthias 2022-11-26 09:11:44 +01:00
parent 338d9d548b
commit 3a65b4880a
3 changed files with 58 additions and 7 deletions

View File

@ -1,5 +1,5 @@
const ROUND_UP = 2;
const ROUND_DOWN = 3;
export const ROUND_UP = 2;
export const ROUND_DOWN = 3;
export function roundTimeframe(
timeframems: number,

View File

@ -14,15 +14,15 @@ const { getTimeZone } = exportForTesting;
describe('timeformatter.ts', () => {
it('sets timezone correctly', () => {
expect(getTimeZone() == 'UTC');
expect(getTimeZone(undefined) == 'UTC');
expect(getTimeZone()).toEqual('UTC');
expect(getTimeZone(undefined)).toEqual('UTC');
setTimezone('CET');
expect(getTimeZone() == 'CET');
expect(getTimeZone()).toEqual('CET');
setTimezone('UTC');
expect(getTimeZone() == 'UTC');
expect(getTimeZone()).toEqual('UTC');
});
it('timestampmsWithTimezone convert correctly', () => {
expect(getTimeZone(undefined) == 'UTC');
expect(getTimeZone(undefined)).toEqual('UTC');
setTimezone('UTC');
expect(timestampmsWithTimezone(1651057500000, 'CET')).toEqual('2022-04-27 13:05:00 (GMT+2)');
expect(timestampmsWithTimezone(1651057500000, 'UTC')).toEqual('2022-04-27 11:05:00 (UTC)');

View File

@ -0,0 +1,51 @@
import { roundTimeframe, ROUND_DOWN, ROUND_UP } from '@/shared/timemath';
// 1651021200000 = 2022-04-27T11:05:00+00:00
const TIMEFRAME_MS_1M = 1000 * 60;
const TIMEFRAME_MS_5M = 1000 * 60 * 5;
const TIMEFRAME_MS_1H = 1000 * 60 * 60;
const TIMEFRAME_MS_1D = 1000 * 60 * 60 * 24;
describe('timemath.ts', () => {
// Use EpochConverter for details on the test data
it('rounds values down 1m', () => {
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500000)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500000, ROUND_DOWN)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500200)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500200, ROUND_DOWN)).toEqual(1651057500000);
});
it('rounds values down 5m', () => {
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500000)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500000, ROUND_DOWN)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500200)).toEqual(1651057500000);
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500200, ROUND_DOWN)).toEqual(1651057500000);
// Expect to round down to :00
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057499999, ROUND_DOWN)).toEqual(1651057200000);
});
it('rounds values down 1h', () => {
expect(roundTimeframe(TIMEFRAME_MS_1H, 1651057500000)).toEqual(1651057200000);
expect(roundTimeframe(TIMEFRAME_MS_1H, 1651057500200)).toEqual(1651057200000);
});
it('rounds values down 1D', () => {
expect(roundTimeframe(TIMEFRAME_MS_1D, 1651057500000)).toEqual(1651017600000);
expect(roundTimeframe(TIMEFRAME_MS_1D, 1651057500200)).toEqual(1651017600000);
});
it('rounds values up 1m', () => {
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500000, ROUND_UP)).toEqual(1651057560000);
expect(roundTimeframe(TIMEFRAME_MS_1M, 1651057500200, ROUND_UP)).toEqual(1651057560000);
});
it('rounds values up 5m', () => {
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500000, ROUND_UP)).toEqual(1651057800000);
expect(roundTimeframe(TIMEFRAME_MS_5M, 1651057500200, ROUND_UP)).toEqual(1651057800000);
});
it('rounds values up 1h', () => {
expect(roundTimeframe(TIMEFRAME_MS_1H, 1651057500000, ROUND_UP)).toEqual(1651060800000);
expect(roundTimeframe(TIMEFRAME_MS_1H, 1651057500200, ROUND_UP)).toEqual(1651060800000);
});
it('rounds values up 1d', () => {
expect(roundTimeframe(TIMEFRAME_MS_1D, 1651057500000, ROUND_UP)).toEqual(1651104000000);
expect(roundTimeframe(TIMEFRAME_MS_1D, 1651057500200, ROUND_UP)).toEqual(1651104000000);
});
});