Skip to content

Commit 5277e78

Browse files
Alejandro Fernándezelasticmachine
andcommitted
[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>
1 parent 8a8e87d commit 5277e78

2 files changed

Lines changed: 271 additions & 2 deletions

File tree

x-pack/plugins/infra/public/utils/datemath.test.ts

Lines changed: 269 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe('extendDatemath()', () => {
198198
});
199199
});
200200

201-
describe('with a positive Operator', () => {
201+
describe('with a positive operator', () => {
202202
it('Halves miliseconds', () => {
203203
expect(extendDatemath('now+250ms')).toEqual({
204204
value: 'now+125ms',
@@ -307,6 +307,274 @@ describe('extendDatemath()', () => {
307307
});
308308
});
309309
});
310+
311+
describe('moving after', () => {
312+
describe('with a negative operator', () => {
313+
it('Halves miliseconds', () => {
314+
expect(extendDatemath('now-250ms', 'after')).toEqual({
315+
value: 'now-125ms',
316+
diffAmount: 125,
317+
diffUnit: 'ms',
318+
});
319+
});
320+
321+
it('Halves seconds', () => {
322+
expect(extendDatemath('now-10s', 'after')).toEqual({
323+
value: 'now-5s',
324+
diffAmount: 5,
325+
diffUnit: 's',
326+
});
327+
});
328+
329+
it('Halves minutes when the amount is low', () => {
330+
expect(extendDatemath('now-2m', 'after')).toEqual({
331+
value: 'now-1m',
332+
diffAmount: 1,
333+
diffUnit: 'm',
334+
});
335+
expect(extendDatemath('now-4m', 'after')).toEqual({
336+
value: 'now-2m',
337+
diffAmount: 2,
338+
diffUnit: 'm',
339+
});
340+
expect(extendDatemath('now-6m', 'after')).toEqual({
341+
value: 'now-3m',
342+
diffAmount: 3,
343+
diffUnit: 'm',
344+
});
345+
});
346+
347+
it('advances minutes in half ammounts when the amount is high', () => {
348+
expect(extendDatemath('now-30m', 'after')).toEqual({
349+
value: 'now-20m',
350+
diffAmount: 10,
351+
diffUnit: 'm',
352+
});
353+
});
354+
355+
it('advances half an hour when the amount is one hour', () => {
356+
expect(extendDatemath('now-1h', 'after')).toEqual({
357+
value: 'now-30m',
358+
diffAmount: 30,
359+
diffUnit: 'm',
360+
});
361+
});
362+
363+
it('advances one hour when the amount is one day', () => {
364+
expect(extendDatemath('now-1d', 'after')).toEqual({
365+
value: 'now-23h',
366+
diffAmount: 1,
367+
diffUnit: 'h',
368+
});
369+
});
370+
371+
it('advances one day when the amount is more than one day', () => {
372+
expect(extendDatemath('now-2d', 'after')).toEqual({
373+
value: 'now-1d',
374+
diffAmount: 1,
375+
diffUnit: 'd',
376+
});
377+
expect(extendDatemath('now-3d', 'after')).toEqual({
378+
value: 'now-2d',
379+
diffAmount: 1,
380+
diffUnit: 'd',
381+
});
382+
});
383+
384+
it('advances one day when the amount is one week', () => {
385+
expect(extendDatemath('now-1w', 'after')).toEqual({
386+
value: 'now-6d',
387+
diffAmount: 1,
388+
diffUnit: 'd',
389+
});
390+
});
391+
392+
it('advances one week when the amount is more than one week', () => {
393+
expect(extendDatemath('now-2w', 'after')).toEqual({
394+
value: 'now-1w',
395+
diffAmount: 1,
396+
diffUnit: 'w',
397+
});
398+
});
399+
400+
it('advances one week when the amount is one month', () => {
401+
expect(extendDatemath('now-1M', 'after')).toEqual({
402+
value: 'now-3w',
403+
diffAmount: 1,
404+
diffUnit: 'w',
405+
});
406+
});
407+
408+
it('advances one month when the amount is more than one month', () => {
409+
expect(extendDatemath('now-2M', 'after')).toEqual({
410+
value: 'now-1M',
411+
diffAmount: 1,
412+
diffUnit: 'M',
413+
});
414+
});
415+
416+
it('advances one month when the amount is one year', () => {
417+
expect(extendDatemath('now-1y', 'after')).toEqual({
418+
value: 'now-11M',
419+
diffAmount: 1,
420+
diffUnit: 'M',
421+
});
422+
});
423+
424+
it('advances one year when the amount is in years', () => {
425+
expect(extendDatemath('now-2y', 'after')).toEqual({
426+
value: 'now-1y',
427+
diffAmount: 1,
428+
diffUnit: 'y',
429+
});
430+
});
431+
});
432+
433+
describe('with a positive operator', () => {
434+
it('doubles miliseconds', () => {
435+
expect(extendDatemath('now+250ms', 'after')).toEqual({
436+
value: 'now+500ms',
437+
diffAmount: 250,
438+
diffUnit: 'ms',
439+
});
440+
});
441+
442+
it('normalizes miliseconds', () => {
443+
expect(extendDatemath('now+500ms', 'after')).toEqual({
444+
value: 'now+1s',
445+
diffAmount: 500,
446+
diffUnit: 'ms',
447+
});
448+
});
449+
450+
it('doubles seconds', () => {
451+
expect(extendDatemath('now+10s', 'after')).toEqual({
452+
value: 'now+20s',
453+
diffAmount: 10,
454+
diffUnit: 's',
455+
});
456+
});
457+
458+
it('normalizes seconds', () => {
459+
expect(extendDatemath('now+30s', 'after')).toEqual({
460+
value: 'now+1m',
461+
diffAmount: 30,
462+
diffUnit: 's',
463+
});
464+
});
465+
466+
it('doubles minutes when amount is low', () => {
467+
expect(extendDatemath('now+1m', 'after')).toEqual({
468+
value: 'now+2m',
469+
diffAmount: 1,
470+
diffUnit: 'm',
471+
});
472+
expect(extendDatemath('now+2m', 'after')).toEqual({
473+
value: 'now+4m',
474+
diffAmount: 2,
475+
diffUnit: 'm',
476+
});
477+
expect(extendDatemath('now+3m', 'after')).toEqual({
478+
value: 'now+6m',
479+
diffAmount: 3,
480+
diffUnit: 'm',
481+
});
482+
});
483+
484+
it('adds half the minutes when the amount is high', () => {
485+
expect(extendDatemath('now+20m', 'after')).toEqual({
486+
value: 'now+30m',
487+
diffAmount: 10,
488+
diffUnit: 'm',
489+
});
490+
});
491+
492+
it('Adds half an hour when the amount is one hour', () => {
493+
expect(extendDatemath('now+1h', 'after')).toEqual({
494+
value: 'now+90m',
495+
diffAmount: 30,
496+
diffUnit: 'm',
497+
});
498+
});
499+
500+
it('Adds one hour when the amount more than one hour', () => {
501+
expect(extendDatemath('now+2h', 'after')).toEqual({
502+
value: 'now+3h',
503+
diffAmount: 1,
504+
diffUnit: 'h',
505+
});
506+
});
507+
508+
it('Adds one hour when the amount is one day', () => {
509+
expect(extendDatemath('now+1d', 'after')).toEqual({
510+
value: 'now+25h',
511+
diffAmount: 1,
512+
diffUnit: 'h',
513+
});
514+
});
515+
516+
it('Adds one day when the amount is more than one day', () => {
517+
expect(extendDatemath('now+2d', 'after')).toEqual({
518+
value: 'now+3d',
519+
diffAmount: 1,
520+
diffUnit: 'd',
521+
});
522+
expect(extendDatemath('now+3d', 'after')).toEqual({
523+
value: 'now+4d',
524+
diffAmount: 1,
525+
diffUnit: 'd',
526+
});
527+
});
528+
529+
it('Adds one day when the amount is one week', () => {
530+
expect(extendDatemath('now+1w', 'after')).toEqual({
531+
value: 'now+8d',
532+
diffAmount: 1,
533+
diffUnit: 'd',
534+
});
535+
});
536+
537+
it('Adds one week when the amount is more than one week', () => {
538+
expect(extendDatemath('now+2w', 'after')).toEqual({
539+
value: 'now+3w',
540+
diffAmount: 1,
541+
diffUnit: 'w',
542+
});
543+
});
544+
545+
it('Adds one week when the amount is one month', () => {
546+
expect(extendDatemath('now+1M', 'after')).toEqual({
547+
value: 'now+5w',
548+
diffAmount: 1,
549+
diffUnit: 'w',
550+
});
551+
});
552+
553+
it('Adds one month when the amount is more than one month', () => {
554+
expect(extendDatemath('now+2M', 'after')).toEqual({
555+
value: 'now+3M',
556+
diffAmount: 1,
557+
diffUnit: 'M',
558+
});
559+
});
560+
561+
it('Adds one month when the amount is one year', () => {
562+
expect(extendDatemath('now+1y', 'after')).toEqual({
563+
value: 'now+13M',
564+
diffAmount: 1,
565+
diffUnit: 'M',
566+
});
567+
});
568+
569+
it('Adds one year when the amount is in years', () => {
570+
expect(extendDatemath('now+2y', 'after')).toEqual({
571+
value: 'now+3y',
572+
diffAmount: 1,
573+
diffUnit: 'y',
574+
});
575+
});
576+
});
577+
});
310578
});
311579

312580
describe('convertDate()', () => {

x-pack/plugins/infra/public/utils/datemath.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ function extendRelativeDatemath(
6868
return undefined;
6969
}
7070

71-
const mustIncreaseAmount = operator === '-' && direction === 'before';
71+
const mustIncreaseAmount =
72+
(operator === '-' && direction === 'before') || (operator === '+' && direction === 'after');
7273
const parsedAmount = parseInt(amount, 10);
7374
let newUnit: Unit = unit as Unit;
7475
let newAmount: number;

0 commit comments

Comments
 (0)