[Logs UI] Complete extendDatemath test suite (#64069)

* Add tests for datemath: `after` direction with a negative operator

* Add tests for datemath: `after` direction with a positive operator

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Alejandro Fernández 2020-04-24 22:30:10 +02:00 committed by GitHub
parent 53b33bb3cb
commit 085c2452d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 271 additions and 2 deletions

View file

@ -198,7 +198,7 @@ describe('extendDatemath()', () => {
});
});
describe('with a positive Operator', () => {
describe('with a positive operator', () => {
it('Halves miliseconds', () => {
expect(extendDatemath('now+250ms')).toEqual({
value: 'now+125ms',
@ -307,6 +307,274 @@ describe('extendDatemath()', () => {
});
});
});
describe('moving after', () => {
describe('with a negative operator', () => {
it('Halves miliseconds', () => {
expect(extendDatemath('now-250ms', 'after')).toEqual({
value: 'now-125ms',
diffAmount: 125,
diffUnit: 'ms',
});
});
it('Halves seconds', () => {
expect(extendDatemath('now-10s', 'after')).toEqual({
value: 'now-5s',
diffAmount: 5,
diffUnit: 's',
});
});
it('Halves minutes when the amount is low', () => {
expect(extendDatemath('now-2m', 'after')).toEqual({
value: 'now-1m',
diffAmount: 1,
diffUnit: 'm',
});
expect(extendDatemath('now-4m', 'after')).toEqual({
value: 'now-2m',
diffAmount: 2,
diffUnit: 'm',
});
expect(extendDatemath('now-6m', 'after')).toEqual({
value: 'now-3m',
diffAmount: 3,
diffUnit: 'm',
});
});
it('advances minutes in half ammounts when the amount is high', () => {
expect(extendDatemath('now-30m', 'after')).toEqual({
value: 'now-20m',
diffAmount: 10,
diffUnit: 'm',
});
});
it('advances half an hour when the amount is one hour', () => {
expect(extendDatemath('now-1h', 'after')).toEqual({
value: 'now-30m',
diffAmount: 30,
diffUnit: 'm',
});
});
it('advances one hour when the amount is one day', () => {
expect(extendDatemath('now-1d', 'after')).toEqual({
value: 'now-23h',
diffAmount: 1,
diffUnit: 'h',
});
});
it('advances one day when the amount is more than one day', () => {
expect(extendDatemath('now-2d', 'after')).toEqual({
value: 'now-1d',
diffAmount: 1,
diffUnit: 'd',
});
expect(extendDatemath('now-3d', 'after')).toEqual({
value: 'now-2d',
diffAmount: 1,
diffUnit: 'd',
});
});
it('advances one day when the amount is one week', () => {
expect(extendDatemath('now-1w', 'after')).toEqual({
value: 'now-6d',
diffAmount: 1,
diffUnit: 'd',
});
});
it('advances one week when the amount is more than one week', () => {
expect(extendDatemath('now-2w', 'after')).toEqual({
value: 'now-1w',
diffAmount: 1,
diffUnit: 'w',
});
});
it('advances one week when the amount is one month', () => {
expect(extendDatemath('now-1M', 'after')).toEqual({
value: 'now-3w',
diffAmount: 1,
diffUnit: 'w',
});
});
it('advances one month when the amount is more than one month', () => {
expect(extendDatemath('now-2M', 'after')).toEqual({
value: 'now-1M',
diffAmount: 1,
diffUnit: 'M',
});
});
it('advances one month when the amount is one year', () => {
expect(extendDatemath('now-1y', 'after')).toEqual({
value: 'now-11M',
diffAmount: 1,
diffUnit: 'M',
});
});
it('advances one year when the amount is in years', () => {
expect(extendDatemath('now-2y', 'after')).toEqual({
value: 'now-1y',
diffAmount: 1,
diffUnit: 'y',
});
});
});
describe('with a positive operator', () => {
it('doubles miliseconds', () => {
expect(extendDatemath('now+250ms', 'after')).toEqual({
value: 'now+500ms',
diffAmount: 250,
diffUnit: 'ms',
});
});
it('normalizes miliseconds', () => {
expect(extendDatemath('now+500ms', 'after')).toEqual({
value: 'now+1s',
diffAmount: 500,
diffUnit: 'ms',
});
});
it('doubles seconds', () => {
expect(extendDatemath('now+10s', 'after')).toEqual({
value: 'now+20s',
diffAmount: 10,
diffUnit: 's',
});
});
it('normalizes seconds', () => {
expect(extendDatemath('now+30s', 'after')).toEqual({
value: 'now+1m',
diffAmount: 30,
diffUnit: 's',
});
});
it('doubles minutes when amount is low', () => {
expect(extendDatemath('now+1m', 'after')).toEqual({
value: 'now+2m',
diffAmount: 1,
diffUnit: 'm',
});
expect(extendDatemath('now+2m', 'after')).toEqual({
value: 'now+4m',
diffAmount: 2,
diffUnit: 'm',
});
expect(extendDatemath('now+3m', 'after')).toEqual({
value: 'now+6m',
diffAmount: 3,
diffUnit: 'm',
});
});
it('adds half the minutes when the amount is high', () => {
expect(extendDatemath('now+20m', 'after')).toEqual({
value: 'now+30m',
diffAmount: 10,
diffUnit: 'm',
});
});
it('Adds half an hour when the amount is one hour', () => {
expect(extendDatemath('now+1h', 'after')).toEqual({
value: 'now+90m',
diffAmount: 30,
diffUnit: 'm',
});
});
it('Adds one hour when the amount more than one hour', () => {
expect(extendDatemath('now+2h', 'after')).toEqual({
value: 'now+3h',
diffAmount: 1,
diffUnit: 'h',
});
});
it('Adds one hour when the amount is one day', () => {
expect(extendDatemath('now+1d', 'after')).toEqual({
value: 'now+25h',
diffAmount: 1,
diffUnit: 'h',
});
});
it('Adds one day when the amount is more than one day', () => {
expect(extendDatemath('now+2d', 'after')).toEqual({
value: 'now+3d',
diffAmount: 1,
diffUnit: 'd',
});
expect(extendDatemath('now+3d', 'after')).toEqual({
value: 'now+4d',
diffAmount: 1,
diffUnit: 'd',
});
});
it('Adds one day when the amount is one week', () => {
expect(extendDatemath('now+1w', 'after')).toEqual({
value: 'now+8d',
diffAmount: 1,
diffUnit: 'd',
});
});
it('Adds one week when the amount is more than one week', () => {
expect(extendDatemath('now+2w', 'after')).toEqual({
value: 'now+3w',
diffAmount: 1,
diffUnit: 'w',
});
});
it('Adds one week when the amount is one month', () => {
expect(extendDatemath('now+1M', 'after')).toEqual({
value: 'now+5w',
diffAmount: 1,
diffUnit: 'w',
});
});
it('Adds one month when the amount is more than one month', () => {
expect(extendDatemath('now+2M', 'after')).toEqual({
value: 'now+3M',
diffAmount: 1,
diffUnit: 'M',
});
});
it('Adds one month when the amount is one year', () => {
expect(extendDatemath('now+1y', 'after')).toEqual({
value: 'now+13M',
diffAmount: 1,
diffUnit: 'M',
});
});
it('Adds one year when the amount is in years', () => {
expect(extendDatemath('now+2y', 'after')).toEqual({
value: 'now+3y',
diffAmount: 1,
diffUnit: 'y',
});
});
});
});
});
describe('convertDate()', () => {

View file

@ -68,7 +68,8 @@ function extendRelativeDatemath(
return undefined;
}
const mustIncreaseAmount = operator === '-' && direction === 'before';
const mustIncreaseAmount =
(operator === '-' && direction === 'before') || (operator === '+' && direction === 'after');
const parsedAmount = parseInt(amount, 10);
let newUnit: Unit = unit as Unit;
let newAmount: number;