TimeSrv.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { TimeSrv } from './TimeSrv';
  2. import moment from 'moment';
  3. describe('timeSrv', () => {
  4. const rootScope = {
  5. $on: jest.fn(),
  6. onAppEvent: jest.fn(),
  7. appEvent: jest.fn(),
  8. };
  9. const timer = {
  10. register: jest.fn(),
  11. cancel: jest.fn(),
  12. cancelAll: jest.fn(),
  13. };
  14. let location = {
  15. search: jest.fn(() => ({})),
  16. };
  17. let timeSrv;
  18. const _dashboard: any = {
  19. time: { from: 'now-6h', to: 'now' },
  20. getTimezone: jest.fn(() => 'browser'),
  21. };
  22. beforeEach(() => {
  23. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  24. timeSrv.init(_dashboard);
  25. _dashboard.refresh = false;
  26. });
  27. describe('timeRange', () => {
  28. it('should return unparsed when parse is false', () => {
  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', () => {
  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', () => {
  42. it('should handle relative times', () => {
  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', () => {
  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 ignore refresh if time absolute', () => {
  69. location = {
  70. search: jest.fn(() => ({
  71. from: '20140410T052010',
  72. to: '20140520T031022',
  73. })),
  74. };
  75. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  76. // dashboard saved with refresh on
  77. _dashboard.refresh = true;
  78. timeSrv.init(_dashboard);
  79. expect(timeSrv.refresh).toBe(false);
  80. });
  81. it('should handle formatted dates without time', () => {
  82. location = {
  83. search: jest.fn(() => ({
  84. from: '20140410',
  85. to: '20140520',
  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(new Date('2014-04-10T00:00:00Z').getTime());
  92. expect(time.to.valueOf()).toEqual(new Date('2014-05-20T00:00:00Z').getTime());
  93. });
  94. it('should handle epochs', () => {
  95. location = {
  96. search: jest.fn(() => ({
  97. from: '1410337646373',
  98. to: '1410337665699',
  99. })),
  100. };
  101. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  102. timeSrv.init(_dashboard);
  103. const time = timeSrv.timeRange();
  104. expect(time.from.valueOf()).toEqual(1410337646373);
  105. expect(time.to.valueOf()).toEqual(1410337665699);
  106. });
  107. it('should handle bad dates', () => {
  108. location = {
  109. search: jest.fn(() => ({
  110. from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
  111. to: 'now',
  112. })),
  113. };
  114. timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
  115. _dashboard.time.from = 'now-6h';
  116. timeSrv.init(_dashboard);
  117. expect(timeSrv.time.from).toEqual('now-6h');
  118. expect(timeSrv.time.to).toEqual('now');
  119. });
  120. });
  121. describe('setTime', () => {
  122. it('should return disable refresh if refresh is disabled for any range', () => {
  123. _dashboard.refresh = false;
  124. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  125. expect(_dashboard.refresh).toBe(false);
  126. });
  127. it('should restore refresh for absolute time range', () => {
  128. _dashboard.refresh = '30s';
  129. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  130. expect(_dashboard.refresh).toBe('30s');
  131. });
  132. it('should restore refresh after relative time range is set', () => {
  133. _dashboard.refresh = '10s';
  134. timeSrv.setTime({
  135. from: moment([2011, 1, 1]),
  136. to: moment([2015, 1, 1]),
  137. });
  138. expect(_dashboard.refresh).toBe(false);
  139. timeSrv.setTime({ from: '2011-01-01', to: 'now' });
  140. expect(_dashboard.refresh).toBe('10s');
  141. });
  142. it('should keep refresh after relative time range is changed and now delay exists', () => {
  143. _dashboard.refresh = '10s';
  144. timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
  145. expect(_dashboard.refresh).toBe('10s');
  146. });
  147. });
  148. });