rangeutil.jest.ts 3.9 KB

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