timeSrv-specs.js 3.5 KB

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