rangeutil_specs.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'
  2. import * as rangeUtil from 'app/core/utils/rangeutil';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. describe("rangeUtil", () => {
  6. describe("Can get range grouped list of ranges", () => {
  7. it('when custom settings should return default range list', () => {
  8. var groups = rangeUtil.getRelativeTimesList({time_options: []}, 'Last 5 minutes');
  9. expect(_.keys(groups).length).to.be(4)
  10. expect(groups[3][0].active).to.be(true)
  11. });
  12. });
  13. describe("Can get range text described", () => {
  14. it('should handle simple old expression with only amount and unit', () => {
  15. var info = rangeUtil.describeTextRange('5m');
  16. expect(info.display).to.be('Last 5 minutes')
  17. });
  18. it('should have singular when amount is 1', () => {
  19. var info = rangeUtil.describeTextRange('1h');
  20. expect(info.display).to.be('Last 1 hour')
  21. });
  22. it('should handle non default amount', () => {
  23. var info = rangeUtil.describeTextRange('13h');
  24. expect(info.display).to.be('Last 13 hours')
  25. expect(info.from).to.be('now-13h')
  26. });
  27. it('should handle now/d', () => {
  28. var info = rangeUtil.describeTextRange('now/d');
  29. expect(info.display).to.be('Today so far');
  30. });
  31. it('should handle now/w', () => {
  32. var info = rangeUtil.describeTextRange('now/w');
  33. expect(info.display).to.be('This week so far');
  34. });
  35. });
  36. describe("Can get date range described", () => {
  37. it('Date range with simple ranges', () => {
  38. var text = rangeUtil.describeTimeRange({from: 'now-1h', to: 'now'});
  39. expect(text).to.be('Last 1 hour')
  40. });
  41. it('Date range with rounding ranges', () => {
  42. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now'});
  43. expect(text).to.be('now/d+6h to now')
  44. });
  45. it('Date range with absolute to now', () => {
  46. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now'});
  47. expect(text).to.be('Nov 10, 2014 02:03:04 to a few seconds ago')
  48. });
  49. it('Date range with absolute to relative', () => {
  50. var text = rangeUtil.describeTimeRange({from: moment([2014,10,10,2,3,4]), to: 'now-1d'});
  51. expect(text).to.be('Nov 10, 2014 02:03:04 to a day ago')
  52. });
  53. it('Date range with relative to absolute', () => {
  54. var text = rangeUtil.describeTimeRange({from: 'now-7d', to: moment([2014,10,10,2,3,4])});
  55. expect(text).to.be('7 days ago to Nov 10, 2014 02:03:04')
  56. });
  57. it('Date range with non matching default ranges', () => {
  58. var text = rangeUtil.describeTimeRange({from: 'now-13h', to: 'now'});
  59. expect(text).to.be('Last 13 hours')
  60. });
  61. it('Date range with from and to both are in now-* format', () => {
  62. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now-3h'});
  63. expect(text).to.be('now-6h to now-3h')
  64. });
  65. it('Date range with from and to both are either in now-* or now/* format', () => {
  66. var text = rangeUtil.describeTimeRange({from: 'now/d+6h', to: 'now-3h'});
  67. expect(text).to.be('now/d+6h to now-3h')
  68. });
  69. it('Date range with from and to both are either in now-* or now+* format', () => {
  70. var text = rangeUtil.describeTimeRange({from: 'now-6h', to: 'now+1h'});
  71. expect(text).to.be('now-6h to now+1h')
  72. });
  73. });
  74. });