time_srv_specs.ts 4.0 KB

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