histogram.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = [1, 2, 10, 11, 17, 20, 29];
  8. });
  9. it('Should convert to series-like array', () => {
  10. bucketSize = 10;
  11. const expected = [[0, 2], [10, 3], [20, 2]];
  12. const histogram = convertValuesToHistogram(values, bucketSize, 1, 29);
  13. expect(histogram).toMatchObject(expected);
  14. });
  15. it('Should not add empty buckets', () => {
  16. bucketSize = 5;
  17. const expected = [[0, 2], [5, 0], [10, 2], [15, 1], [20, 1], [25, 1]];
  18. const histogram = convertValuesToHistogram(values, bucketSize, 1, 29);
  19. expect(histogram).toMatchObject(expected);
  20. });
  21. });
  22. describe('Series to values converter', () => {
  23. let data: any;
  24. beforeEach(() => {
  25. data = [
  26. {
  27. datapoints: [[1, 0], [2, 0], [10, 0], [11, 0], [17, 0], [20, 0], [29, 0]],
  28. },
  29. ];
  30. });
  31. it('Should convert to values array', () => {
  32. const expected = [1, 2, 10, 11, 17, 20, 29];
  33. const values = getSeriesValues(data);
  34. expect(values).toMatchObject(expected);
  35. });
  36. it('Should skip null values', () => {
  37. data[0].datapoints.push([null, 0]);
  38. const expected = [1, 2, 10, 11, 17, 20, 29];
  39. const values = getSeriesValues(data);
  40. expect(values).toMatchObject(expected);
  41. });
  42. });
  43. });