TimeSrv.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { TimeSrv } from './TimeSrv';
  2. import { ContextSrvStub } from 'test/specs/helpers';
  3. import { isDateTime, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  4. describe('timeSrv', () => {
  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(() => {
  24. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  25. timeSrv.init(_dashboard);
  26. _dashboard.refresh = false;
  27. });
  28. describe('timeRange', () => {
  29. it('should return unparsed when parse is false', () => {
  30. timeSrv.setTime({ from: 'now', to: 'now-1h' });
  31. const time = timeSrv.timeRange();
  32. expect(time.raw.from).toBe('now');
  33. expect(time.raw.to).toBe('now-1h');
  34. });
  35. it('should return parsed when parse is true', () => {
  36. timeSrv.setTime({ from: 'now', to: 'now-1h' });
  37. const time = timeSrv.timeRange();
  38. expect(isDateTime(time.from)).toBe(true);
  39. expect(isDateTime(time.to)).toBe(true);
  40. });
  41. });
  42. describe('init time from url', () => {
  43. it('should handle relative times', () => {
  44. location = {
  45. search: jest.fn(() => ({
  46. from: 'now-2d',
  47. to: 'now',
  48. })),
  49. };
  50. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  51. timeSrv.init(_dashboard);
  52. const time = timeSrv.timeRange();
  53. expect(time.raw.from).toBe('now-2d');
  54. expect(time.raw.to).toBe('now');
  55. });
  56. it('should handle formatted dates', () => {
  57. location = {
  58. search: jest.fn(() => ({
  59. from: '20140410T052010',
  60. to: '20140520T031022',
  61. })),
  62. };
  63. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  64. timeSrv.init(_dashboard);
  65. const time = timeSrv.timeRange();
  66. expect(time.from.valueOf()).toEqual(new Date('2014-04-10T05:20:10Z').getTime());
  67. expect(time.to.valueOf()).toEqual(new Date('2014-05-20T03:10:22Z').getTime());
  68. });
  69. it('should ignore refresh if time absolute', () => {
  70. location = {
  71. search: jest.fn(() => ({
  72. from: '20140410T052010',
  73. to: '20140520T031022',
  74. })),
  75. };
  76. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  77. // dashboard saved with refresh on
  78. _dashboard.refresh = true;
  79. timeSrv.init(_dashboard);
  80. expect(timeSrv.refresh).toBe(false);
  81. });
  82. it('should handle formatted dates without time', () => {
  83. location = {
  84. search: jest.fn(() => ({
  85. from: '20140410',
  86. to: '20140520',
  87. })),
  88. };
  89. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  90. timeSrv.init(_dashboard);
  91. const time = timeSrv.timeRange();
  92. expect(time.from.valueOf()).toEqual(new Date('2014-04-10T00:00:00Z').getTime());
  93. expect(time.to.valueOf()).toEqual(new Date('2014-05-20T00:00:00Z').getTime());
  94. });
  95. it('should handle epochs', () => {
  96. location = {
  97. search: jest.fn(() => ({
  98. from: '1410337646373',
  99. to: '1410337665699',
  100. })),
  101. };
  102. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  103. timeSrv.init(_dashboard);
  104. const time = timeSrv.timeRange();
  105. expect(time.from.valueOf()).toEqual(1410337646373);
  106. expect(time.to.valueOf()).toEqual(1410337665699);
  107. });
  108. it('should handle bad dates', () => {
  109. location = {
  110. search: jest.fn(() => ({
  111. from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
  112. to: 'now',
  113. })),
  114. };
  115. timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
  116. _dashboard.time.from = 'now-6h';
  117. timeSrv.init(_dashboard);
  118. expect(timeSrv.time.from).toEqual('now-6h');
  119. expect(timeSrv.time.to).toEqual('now');
  120. });
  121. });
  122. describe('setTime', () => {
  123. it('should return disable refresh if refresh is disabled for any range', () => {
  124. _dashboard.refresh = false;
  125. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  126. expect(_dashboard.refresh).toBe(false);
  127. });
  128. it('should restore refresh for absolute time range', () => {
  129. _dashboard.refresh = '30s';
  130. timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
  131. expect(_dashboard.refresh).toBe('30s');
  132. });
  133. it('should restore refresh after relative time range is set', () => {
  134. _dashboard.refresh = '10s';
  135. timeSrv.setTime({
  136. from: dateTime([2011, 1, 1]),
  137. to: dateTime([2015, 1, 1]),
  138. });
  139. expect(_dashboard.refresh).toBe(false);
  140. timeSrv.setTime({ from: '2011-01-01', to: 'now' });
  141. expect(_dashboard.refresh).toBe('10s');
  142. });
  143. it('should keep refresh after relative time range is changed and now delay exists', () => {
  144. _dashboard.refresh = '10s';
  145. timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
  146. expect(_dashboard.refresh).toBe('10s');
  147. });
  148. });
  149. });