time_srv.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { TimeSrv } from '../time_srv';
  2. import '../time_srv';
  3. import moment from 'moment';
  4. describe('timeSrv', function() {
  5. const rootScope = {
  6. $on: jest.fn(),
  7. onAppEvent: jest.fn(),
  8. appEvent: jest.fn(),
  9. };
  10. const timer = {
  11. register: jest.fn(),
  12. cancel: jest.fn(),
  13. cancelAll: jest.fn(),
  14. };
  15. let location = {
  16. search: jest.fn(() => ({})),
  17. };
  18. let timeSrv;
  19. const _dashboard: any = {
  20. time: { from: 'now-6h', to: 'now' },
  21. getTimezone: jest.fn(() => 'browser'),
  22. };
  23. beforeEach(function() {
  24. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  25. timeSrv.init(_dashboard);
  26. });
  27. describe('timeRange', function() {
  28. it('should return unparsed when parse is false', function() {
  29. timeSrv.setTime({ from: 'now', to: 'now-1h' });
  30. const time = timeSrv.timeRange();
  31. expect(time.raw.from).toBe('now');
  32. expect(time.raw.to).toBe('now-1h');
  33. });
  34. it('should return parsed when parse is true', function() {
  35. timeSrv.setTime({ from: 'now', to: 'now-1h' });
  36. const time = timeSrv.timeRange();
  37. expect(moment.isMoment(time.from)).toBe(true);
  38. expect(moment.isMoment(time.to)).toBe(true);
  39. });
  40. });
  41. describe('init time from url', function() {
  42. it('should handle relative times', function() {
  43. location = {
  44. search: jest.fn(() => ({
  45. from: 'now-2d',
  46. to: 'now',
  47. })),
  48. };
  49. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  50. timeSrv.init(_dashboard);
  51. const time = timeSrv.timeRange();
  52. expect(time.raw.from).toBe('now-2d');
  53. expect(time.raw.to).toBe('now');
  54. });
  55. it('should handle formatted dates', function() {
  56. location = {
  57. search: jest.fn(() => ({
  58. from: '20140410T052010',
  59. to: '20140520T031022',
  60. })),
  61. };
  62. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  63. timeSrv.init(_dashboard);
  64. const time = timeSrv.timeRange();
  65. expect(time.from.valueOf()).toEqual(new Date('2014-04-10T05:20:10Z').getTime());
  66. expect(time.to.valueOf()).toEqual(new Date('2014-05-20T03:10:22Z').getTime());
  67. });
  68. it('should handle formatted dates without time', function() {
  69. location = {
  70. search: jest.fn(() => ({
  71. from: '20140410',
  72. to: '20140520',
  73. })),
  74. };
  75. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  76. timeSrv.init(_dashboard);
  77. const time = timeSrv.timeRange();
  78. expect(time.from.valueOf()).toEqual(new Date('2014-04-10T00:00:00Z').getTime());
  79. expect(time.to.valueOf()).toEqual(new Date('2014-05-20T00:00:00Z').getTime());
  80. });
  81. it('should handle epochs', function() {
  82. location = {
  83. search: jest.fn(() => ({
  84. from: '1410337646373',
  85. to: '1410337665699',
  86. })),
  87. };
  88. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  89. timeSrv.init(_dashboard);
  90. const time = timeSrv.timeRange();
  91. expect(time.from.valueOf()).toEqual(1410337646373);
  92. expect(time.to.valueOf()).toEqual(1410337665699);
  93. });
  94. it('should handle bad dates', function() {
  95. location = {
  96. search: jest.fn(() => ({
  97. from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
  98. to: 'now',
  99. })),
  100. };
  101. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  102. _dashboard.time.from = 'now-6h';
  103. timeSrv.init(_dashboard);
  104. expect(timeSrv.time.from).toEqual('now-6h');
  105. expect(timeSrv.time.to).toEqual('now');
  106. });
  107. });
  108. describe('setTime', function() {
  109. it('should return disable refresh if refresh is disabled for any range', function() {
  110. _dashboard.refresh = false;
  111. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  112. expect(_dashboard.refresh).toBe(false);
  113. });
  114. it('should restore refresh for absolute time range', function() {
  115. _dashboard.refresh = '30s';
  116. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  117. expect(_dashboard.refresh).toBe('30s');
  118. });
  119. it('should restore refresh after relative time range is set', function() {
  120. _dashboard.refresh = '10s';
  121. timeSrv.setTime({
  122. from: moment([2011, 1, 1]),
  123. to: moment([2015, 1, 1]),
  124. });
  125. expect(_dashboard.refresh).toBe(false);
  126. timeSrv.setTime({ from: '2011-01-01', to: 'now' });
  127. expect(_dashboard.refresh).toBe('10s');
  128. });
  129. it('should keep refresh after relative time range is changed and now delay exists', function() {
  130. _dashboard.refresh = '10s';
  131. timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
  132. expect(_dashboard.refresh).toBe('10s');
  133. });
  134. });
  135. });