time_srv_specs.ts 3.9 KB

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