histogram.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { convertValuesToHistogram, getSeriesValues } from '../histogram';
  2. describe('Graph Histogam Converter', () => {
  3. describe('Values to histogram converter', () => {
  4. let values: any;
  5. let bucketSize = 10;
  6. beforeEach(() => {
  7. values = [29, 1, 2, 10, 11, 17, 20];
  8. });
  9. it('Should convert to series-like array', () => {
  10. bucketSize = 10;
  11. const expected = [
  12. [0, 2],
  13. [10, 3],
  14. [20, 2],
  15. ];
  16. const histogram = convertValuesToHistogram(values, bucketSize, 1, 30);
  17. expect(histogram).toMatchObject(expected);
  18. });
  19. it('Should not add empty buckets', () => {
  20. bucketSize = 5;
  21. const expected = [
  22. [0, 2],
  23. [5, 0],
  24. [10, 2],
  25. [15, 1],
  26. [20, 1],
  27. [25, 1],
  28. ];
  29. const histogram = convertValuesToHistogram(values, bucketSize, 1, 30);
  30. expect(histogram).toMatchObject(expected);
  31. });
  32. });
  33. describe('Buckets to have correct decimals', () => {
  34. it('Should convert to series-like array', () => {
  35. const expected = [[1.7000000000000002, 1]];
  36. const histogram = convertValuesToHistogram([1.715000033378601], 0.05, 1.7, 1.8);
  37. expect(histogram).toMatchObject(expected);
  38. });
  39. });
  40. describe('Series to values converter', () => {
  41. let data: any;
  42. beforeEach(() => {
  43. data = [
  44. {
  45. datapoints: [
  46. [1, 0],
  47. [2, 0],
  48. [10, 0],
  49. [11, 0],
  50. [17, 0],
  51. [20, 0],
  52. [29, 0],
  53. ],
  54. },
  55. ];
  56. });
  57. it('Should convert to values array', () => {
  58. const expected = [1, 2, 10, 11, 17, 20, 29];
  59. const values = getSeriesValues(data);
  60. expect(values).toMatchObject(expected);
  61. });
  62. it('Should skip null values', () => {
  63. data[0].datapoints.push([null, 0]);
  64. const expected = [1, 2, 10, 11, 17, 20, 29];
  65. const values = getSeriesValues(data);
  66. expect(values).toMatchObject(expected);
  67. });
  68. });
  69. });