timePicker.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { toUtc, AbsoluteTimeRange } from '@grafana/data';
  2. import { getShiftedTimeRange, getZoomedTimeRange } from './timePicker';
  3. export const setup = (options?: any) => {
  4. const defaultOptions = {
  5. range: {
  6. from: toUtc('2019-01-01 10:00:00'),
  7. to: toUtc('2019-01-01 16:00:00'),
  8. raw: {
  9. from: 'now-6h',
  10. to: 'now',
  11. },
  12. },
  13. direction: 0,
  14. };
  15. return { ...defaultOptions, ...options };
  16. };
  17. describe('getShiftedTimeRange', () => {
  18. describe('when called with a direction of -1', () => {
  19. it('then it should return correct result', () => {
  20. const { range, direction } = setup({ direction: -1 });
  21. const expectedRange: AbsoluteTimeRange = {
  22. from: toUtc('2019-01-01 07:00:00').valueOf(),
  23. to: toUtc('2019-01-01 13:00:00').valueOf(),
  24. };
  25. const result = getShiftedTimeRange(direction, range);
  26. expect(result).toEqual(expectedRange);
  27. });
  28. });
  29. describe('when called with a direction of 1', () => {
  30. it('then it should return correct result', () => {
  31. const { range, direction } = setup({ direction: 1 });
  32. const expectedRange: AbsoluteTimeRange = {
  33. from: toUtc('2019-01-01 13:00:00').valueOf(),
  34. to: toUtc('2019-01-01 19:00:00').valueOf(),
  35. };
  36. const result = getShiftedTimeRange(direction, range);
  37. expect(result).toEqual(expectedRange);
  38. });
  39. });
  40. describe('when called with any other direction', () => {
  41. it('then it should return correct result', () => {
  42. const { range, direction } = setup({ direction: 0 });
  43. const expectedRange: AbsoluteTimeRange = {
  44. from: toUtc('2019-01-01 10:00:00').valueOf(),
  45. to: toUtc('2019-01-01 16:00:00').valueOf(),
  46. };
  47. const result = getShiftedTimeRange(direction, range);
  48. expect(result).toEqual(expectedRange);
  49. });
  50. });
  51. });
  52. describe('getZoomedTimeRange', () => {
  53. describe('when called', () => {
  54. it('then it should return correct result', () => {
  55. const { range } = setup();
  56. const expectedRange: AbsoluteTimeRange = {
  57. from: toUtc('2019-01-01 07:00:00').valueOf(),
  58. to: toUtc('2019-01-01 19:00:00').valueOf(),
  59. };
  60. const result = getZoomedTimeRange(range, 2);
  61. expect(result).toEqual(expectedRange);
  62. });
  63. });
  64. describe('when called with a timespan of 0', () => {
  65. it('then it should return a timespan of 30s', () => {
  66. const range = {
  67. from: toUtc('2019-01-01 10:00:00'),
  68. to: toUtc('2019-01-01 10:00:00'),
  69. raw: {
  70. from: 'now',
  71. to: 'now',
  72. },
  73. };
  74. const expectedRange: AbsoluteTimeRange = {
  75. from: toUtc('2019-01-01 09:59:45').valueOf(),
  76. to: toUtc('2019-01-01 10:00:15').valueOf(),
  77. };
  78. const result = getZoomedTimeRange(range, 2);
  79. expect(result).toEqual(expectedRange);
  80. });
  81. });
  82. });