timeSrv-specs.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define([
  2. '../mocks/dashboard-mock',
  3. './helpers',
  4. 'lodash',
  5. 'moment',
  6. 'app/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.services'));
  14. beforeEach(ctx.providePhase(['$routeParams']));
  15. beforeEach(ctx.createService('timeSrv'));
  16. beforeEach(function() {
  17. _dashboard = dashboardMock.create();
  18. ctx.service.init(_dashboard);
  19. });
  20. describe('timeRange', function() {
  21. it('should return unparsed when parse is false', function() {
  22. ctx.service.setTime({from: 'now', to: 'now-1h' });
  23. var time = ctx.service.timeRange(false);
  24. expect(time.from).to.be('now');
  25. expect(time.to).to.be('now-1h');
  26. });
  27. it('should return parsed when parse is true', function() {
  28. ctx.service.setTime({from: 'now', to: 'now-1h' });
  29. var time = ctx.service.timeRange(true);
  30. expect(moment.isMoment(time.from)).to.be(true);
  31. expect(moment.isMoment(time.to)).to.be(true);
  32. });
  33. });
  34. describe('init time from url', function() {
  35. it('should handle relative times', function() {
  36. ctx.$routeParams.from = 'now-2d';
  37. ctx.$routeParams.to = 'now';
  38. ctx.service.init(_dashboard);
  39. var time = ctx.service.timeRange(false);
  40. expect(time.from).to.be('now-2d');
  41. expect(time.to).to.be('now');
  42. });
  43. it('should handle formated dates', function() {
  44. ctx.$routeParams.from = '20140410T052010';
  45. ctx.$routeParams.to = '20140520T031022';
  46. ctx.service.init(_dashboard);
  47. var time = ctx.service.timeRange(true);
  48. expect(time.from.valueOf()).to.equal(new Date("2014-04-10T05:20:10Z").getTime());
  49. expect(time.to.valueOf()).to.equal(new Date("2014-05-20T03:10:22Z").getTime());
  50. });
  51. it('should handle formated dates without time', function() {
  52. ctx.$routeParams.from = '20140410';
  53. ctx.$routeParams.to = '20140520';
  54. ctx.service.init(_dashboard);
  55. var time = ctx.service.timeRange(true);
  56. expect(time.from.valueOf()).to.equal(new Date("2014-04-10T00:00:00Z").getTime());
  57. expect(time.to.valueOf()).to.equal(new Date("2014-05-20T00:00:00Z").getTime());
  58. });
  59. it('should handle epochs', function() {
  60. ctx.$routeParams.from = '1410337646373';
  61. ctx.$routeParams.to = '1410337665699';
  62. ctx.service.init(_dashboard);
  63. var time = ctx.service.timeRange(true);
  64. expect(time.from.valueOf()).to.equal(1410337646373);
  65. expect(time.to.valueOf()).to.equal(1410337665699);
  66. });
  67. });
  68. describe('setTime', function() {
  69. it('should return disable refresh for absolute times', function() {
  70. _dashboard.refresh = false;
  71. ctx.service.setTime({from: '2011-01-01', to: '2015-01-01' });
  72. expect(_dashboard.refresh).to.be(false);
  73. });
  74. it('should restore refresh after relative time range is set', function() {
  75. _dashboard.refresh = '10s';
  76. ctx.service.setTime({from: moment([2011,1,1]), to: moment([2015,1,1])});
  77. expect(_dashboard.refresh).to.be(false);
  78. ctx.service.setTime({from: '2011-01-01', to: 'now' });
  79. expect(_dashboard.refresh).to.be('10s');
  80. });
  81. it('should keep refresh after relative time range is changed and now delay exists', function() {
  82. _dashboard.refresh = '10s';
  83. ctx.service.setTime({from: 'now-1h', to: 'now-10s' });
  84. expect(_dashboard.refresh).to.be('10s');
  85. });
  86. });
  87. });
  88. });