histogram.jest.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { convertValuesToHistogram, getSeriesValues } from "../histogram";
  2. describe("Graph Histogam Converter", function() {
  3. describe("Values to histogram converter", () => {
  4. let values;
  5. let bucketSize = 10;
  6. beforeEach(() => {
  7. values = [1, 2, 10, 11, 17, 20, 29];
  8. });
  9. it("Should convert to series-like array", () => {
  10. bucketSize = 10;
  11. let expected = [[0, 2], [10, 3], [20, 2]];
  12. let histogram = convertValuesToHistogram(values, bucketSize);
  13. expect(histogram).toMatchObject(expected);
  14. });
  15. it("Should not add empty buckets", () => {
  16. bucketSize = 5;
  17. let expected = [[0, 2], [10, 2], [15, 1], [20, 1], [25, 1]];
  18. let histogram = convertValuesToHistogram(values, bucketSize);
  19. expect(histogram).toMatchObject(expected);
  20. });
  21. });
  22. describe("Series to values converter", () => {
  23. let data;
  24. beforeEach(() => {
  25. data = [
  26. {
  27. datapoints: [
  28. [1, 0],
  29. [2, 0],
  30. [10, 0],
  31. [11, 0],
  32. [17, 0],
  33. [20, 0],
  34. [29, 0]
  35. ]
  36. }
  37. ];
  38. });
  39. it("Should convert to values array", () => {
  40. let expected = [1, 2, 10, 11, 17, 20, 29];
  41. let values = getSeriesValues(data);
  42. expect(values).toMatchObject(expected);
  43. });
  44. it("Should skip null values", () => {
  45. data[0].datapoints.push([null, 0]);
  46. let expected = [1, 2, 10, 11, 17, 20, 29];
  47. let values = getSeriesValues(data);
  48. expect(values).toMatchObject(expected);
  49. });
  50. });
  51. });