time_srv_specs.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. define([
  2. 'test/mocks/dashboard-mock',
  3. 'test/specs/helpers',
  4. 'lodash',
  5. 'moment',
  6. 'app/core/services/timer',
  7. 'app/features/dashboard/timeSrv'
  8. ], function(dashboardMock, helpers, _, moment) {
  9. 'use strict';
  10. describe('timeSrv', function() {
  11. var ctx = new helpers.ServiceTestContext();
  12. var _dashboard;
  13. beforeEach(module('grafana.core'));
  14. beforeEach(module('grafana.services'));
  15. beforeEach(ctx.providePhase(['$routeParams']));
  16. beforeEach(ctx.createService('timeSrv'));
  17. beforeEach(function() {
  18. _dashboard = dashboardMock.create();
  19. ctx.service.init(_dashboard);
  20. });
  21. describe('timeRange', function() {
  22. it('should return unparsed when parse is false', function() {
  23. ctx.service.setTime({from: 'now', to: 'now-1h' });
  24. var time = ctx.service.timeRange(false);
  25. expect(time.from).to.be('now');
  26. expect(time.to).to.be('now-1h');
  27. });
  28. it('should return parsed when parse is true', function() {
  29. ctx.service.setTime({from: 'now', to: 'now-1h' });
  30. var time = ctx.service.timeRange(true);
  31. expect(moment.isMoment(time.from)).to.be(true);
  32. expect(moment.isMoment(time.to)).to.be(true);
  33. });
  34. });
  35. describe('init time from url', function() {
  36. it('should handle relative times', function() {
  37. ctx.$routeParams.from = 'now-2d';
  38. ctx.$routeParams.to = 'now';
  39. ctx.service.init(_dashboard);
  40. var time = ctx.service.timeRange(false);
  41. expect(time.from).to.be('now-2d');
  42. expect(time.to).to.be('now');
  43. });
  44. it('should handle formated dates', function() {
  45. ctx.$routeParams.from = '20140410T052010';
  46. ctx.$routeParams.to = '20140520T031022';
  47. ctx.service.init(_dashboard);
  48. var time = ctx.service.timeRange(true);
  49. expect(time.from.valueOf()).to.equal(new Date("2014-04-10T05:20:10Z").getTime());
  50. expect(time.to.valueOf()).to.equal(new Date("2014-05-20T03:10:22Z").getTime());
  51. });
  52. it('should handle formated dates without time', function() {
  53. ctx.$routeParams.from = '20140410';
  54. ctx.$routeParams.to = '20140520';
  55. ctx.service.init(_dashboard);
  56. var time = ctx.service.timeRange(true);
  57. expect(time.from.valueOf()).to.equal(new Date("2014-04-10T00:00:00Z").getTime());
  58. expect(time.to.valueOf()).to.equal(new Date("2014-05-20T00:00:00Z").getTime());
  59. });
  60. it('should handle epochs', function() {
  61. ctx.$routeParams.from = '1410337646373';
  62. ctx.$routeParams.to = '1410337665699';
  63. ctx.service.init(_dashboard);
  64. var time = ctx.service.timeRange(true);
  65. expect(time.from.valueOf()).to.equal(1410337646373);
  66. expect(time.to.valueOf()).to.equal(1410337665699);
  67. });
  68. it('should handle bad dates', function() {
  69. ctx.$routeParams.from = '20151126T00010%3C%2Fp%3E%3Cspan%20class';
  70. ctx.$routeParams.to = 'now';
  71. _dashboard.time.from = 'now-6h';
  72. ctx.service.init(_dashboard);
  73. expect(ctx.service.time.from).to.equal('now-6h');
  74. expect(ctx.service.time.to).to.equal('now');
  75. });
  76. });
  77. describe('setTime', function() {
  78. it('should return disable refresh if refresh is disabled for any range', function() {
  79. _dashboard.refresh = false;
  80. ctx.service.setTime({from: '2011-01-01', to: '2015-01-01' });
  81. expect(_dashboard.refresh).to.be(false);
  82. });
  83. it('should restore refresh for absolute time range', function() {
  84. _dashboard.refresh = '30s';
  85. ctx.service.setTime({from: '2011-01-01', to: '2015-01-01' });
  86. expect(_dashboard.refresh).to.be('30s');
  87. });
  88. it('should restore refresh after relative time range is set', function() {
  89. _dashboard.refresh = '10s';
  90. ctx.service.setTime({from: moment([2011,1,1]), to: moment([2015,1,1])});
  91. expect(_dashboard.refresh).to.be(false);
  92. ctx.service.setTime({from: '2011-01-01', to: 'now' });
  93. expect(_dashboard.refresh).to.be('10s');
  94. });
  95. it('should keep refresh after relative time range is changed and now delay exists', function() {
  96. _dashboard.refresh = '10s';
  97. ctx.service.setTime({from: 'now-1h', to: 'now-10s' });
  98. expect(_dashboard.refresh).to.be('10s');
  99. });
  100. });
  101. });
  102. });