time_srv_specs.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { describe, beforeEach, it, expect, sinon, 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. getTimezone: sinon.stub().returns('browser'),
  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({
  62. from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
  63. to: 'now',
  64. });
  65. _dashboard.time.from = 'now-6h';
  66. ctx.service.init(_dashboard);
  67. expect(ctx.service.time.from).to.equal('now-6h');
  68. expect(ctx.service.time.to).to.equal('now');
  69. });
  70. });
  71. describe('setTime', function() {
  72. it('should return disable refresh if refresh is disabled for any range', function() {
  73. _dashboard.refresh = false;
  74. ctx.service.setTime({ from: '2011-01-01', to: '2015-01-01' });
  75. expect(_dashboard.refresh).to.be(false);
  76. });
  77. it('should restore refresh for absolute time range', function() {
  78. _dashboard.refresh = '30s';
  79. ctx.service.setTime({ from: '2011-01-01', to: '2015-01-01' });
  80. expect(_dashboard.refresh).to.be('30s');
  81. });
  82. it('should restore refresh after relative time range is set', function() {
  83. _dashboard.refresh = '10s';
  84. ctx.service.setTime({
  85. from: moment([2011, 1, 1]),
  86. to: moment([2015, 1, 1]),
  87. });
  88. expect(_dashboard.refresh).to.be(false);
  89. ctx.service.setTime({ from: '2011-01-01', to: 'now' });
  90. expect(_dashboard.refresh).to.be('10s');
  91. });
  92. it('should keep refresh after relative time range is changed and now delay exists', function() {
  93. _dashboard.refresh = '10s';
  94. ctx.service.setTime({ from: 'now-1h', to: 'now-10s' });
  95. expect(_dashboard.refresh).to.be('10s');
  96. });
  97. });
  98. });