timeSrv-specs.js 3.3 KB

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