TimeRange: Fixes issue when zooming out on a timerange with timespan 0 (#49622)

This commit is contained in:
Joao Silva 2022-05-26 10:53:30 +01:00 committed by GitHub
parent 78bef7a26a
commit 63848ad2e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -73,6 +73,26 @@ describe('getZoomedTimeRange', () => {
const result = getZoomedTimeRange(range, 2);
expect(result).toEqual(expectedRange);
});
});
describe('when called with a timespan of 0', () => {
it('then it should return a timespan of 30s', () => {
const range = {
from: toUtc('2019-01-01 10:00:00'),
to: toUtc('2019-01-01 10:00:00'),
raw: {
from: 'now',
to: 'now',
},
};
const expectedRange: AbsoluteTimeRange = {
from: toUtc('2019-01-01 09:59:45').valueOf(),
to: toUtc('2019-01-01 10:00:15').valueOf(),
};
const result = getZoomedTimeRange(range, 2);
expect(result).toEqual(expectedRange);
});
});

View File

@ -30,9 +30,11 @@ export const getShiftedTimeRange = (direction: number, origRange: TimeRange): Ab
export const getZoomedTimeRange = (range: TimeRange, factor: number): AbsoluteTimeRange => {
const timespan = range.to.valueOf() - range.from.valueOf();
const center = range.to.valueOf() - timespan / 2;
// If the timepsan is 0, zooming out would do nothing, so we force a zoom out to 30s
const newTimespan = timespan === 0 ? 30000 : timespan * factor;
const to = center + (timespan * factor) / 2;
const from = center - (timespan * factor) / 2;
const to = center + newTimespan / 2;
const from = center - newTimespan / 2;
return { from, to };
};