signoz/frontend/src/lib/__tests__/getStep.test.ts
Pranshu Chittora 566c2becdf
feat: dynamic step size for the data for graphs (#929)
* feat: dynamic step size for the data for graphs

* fix: remove console.log

* chore: add jest globals

* feat: add step size for dashboard

* chore: undo .eslintignore
2022-04-05 16:09:57 +05:30

70 lines
1.5 KiB
TypeScript

import { expect } from '@jest/globals';
import dayjs from 'dayjs';
import getStep, { DefaultStepSize, MaxDataPoints } from 'lib/getStep';
describe('lib/getStep', () => {
test('should return default step when the given range is less than 1 day', () => {
const start = dayjs();
const end = start.add(1, 'hour');
const startUnix = start.valueOf();
const endUnix = end.valueOf();
expect(
getStep({
start: startUnix / 1e3,
end: endUnix / 1e3,
inputFormat: 's',
}),
).toEqual(DefaultStepSize);
expect(
getStep({
start: startUnix,
end: endUnix,
inputFormat: 'ms',
}),
).toEqual(DefaultStepSize);
expect(
getStep({
start: startUnix * 1e6,
end: endUnix * 1e6,
inputFormat: 'ns',
}),
).toEqual(DefaultStepSize);
});
test('should return relevant step when the given range is greater than 1 day', () => {
const start = dayjs();
const end = start.add(1, 'Day').add(1, 'Second');
const startUnix = start.valueOf();
const endUnix = end.valueOf();
const expectedStepSize = Math.floor(end.diff(start, 's') / MaxDataPoints);
expect(
getStep({
start: startUnix / 1e3,
end: endUnix / 1e3,
inputFormat: 's',
}),
).toEqual(expectedStepSize);
expect(
getStep({
start: startUnix,
end: endUnix,
inputFormat: 'ms',
}),
).toEqual(expectedStepSize);
expect(
getStep({
start: startUnix * 1e6,
end: endUnix * 1e6,
inputFormat: 'ns',
}),
).toEqual(expectedStepSize);
});
});