scale.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { ArrayVector, DataFrame, FieldType } from '@grafana/data';
  2. import { getScaledDimension, validateScaleConfig } from './scale';
  3. describe('scale dimensions', () => {
  4. it('should validate empty input', () => {
  5. const out = validateScaleConfig({} as any, {
  6. min: 5,
  7. max: 10,
  8. });
  9. expect(out).toMatchInlineSnapshot(`
  10. Object {
  11. "fixed": 7.5,
  12. "max": 10,
  13. "min": 5,
  14. }
  15. `);
  16. });
  17. it('should assert min<max', () => {
  18. const out = validateScaleConfig(
  19. {
  20. max: -3,
  21. min: 7,
  22. fixed: 100,
  23. },
  24. {
  25. min: 5,
  26. max: 10,
  27. }
  28. );
  29. expect(out).toMatchInlineSnapshot(`
  30. Object {
  31. "fixed": 10,
  32. "max": 7,
  33. "min": 5,
  34. }
  35. `);
  36. });
  37. it('should support negative min values', () => {
  38. const values = [-20, -10, -5, 0, 5, 10, 20];
  39. const frame: DataFrame = {
  40. name: 'a',
  41. length: values.length,
  42. fields: [
  43. { name: 'time', type: FieldType.number, values: new ArrayVector(values), config: {} },
  44. {
  45. name: 'hello',
  46. type: FieldType.number,
  47. values: new ArrayVector(values),
  48. config: {
  49. min: -10,
  50. max: 10,
  51. },
  52. },
  53. ],
  54. };
  55. const supplier = getScaledDimension(frame, {
  56. min: -1,
  57. max: 1,
  58. field: 'hello',
  59. fixed: 0,
  60. });
  61. const scaled = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
  62. expect(scaled).toEqual([-1, -1, -0.5, 0, 0.5, 1, 1]);
  63. });
  64. });