scalar.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { ArrayVector, DataFrame, FieldType } from '@grafana/data';
  2. import { getScalarDimension } from './scalar';
  3. import { ScalarDimensionMode } from '.';
  4. describe('scalar dimensions', () => {
  5. it('handles string field', () => {
  6. const values = ['-720', '10', '540', '90', '-210'];
  7. const frame: DataFrame = {
  8. name: 'a',
  9. length: values.length,
  10. fields: [
  11. {
  12. name: 'test',
  13. type: FieldType.number,
  14. values: new ArrayVector(values),
  15. config: {
  16. min: -720,
  17. max: 540,
  18. },
  19. },
  20. ],
  21. };
  22. const supplier = getScalarDimension(frame, {
  23. min: -360,
  24. max: 360,
  25. field: 'test',
  26. fixed: 0,
  27. mode: ScalarDimensionMode.Clamped,
  28. });
  29. const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
  30. expect(clamped).toEqual([0, 0, 0, 0, 0]);
  31. });
  32. it('clamps out of range values', () => {
  33. const values = [-720, 10, 540, 90, -210];
  34. const frame: DataFrame = {
  35. name: 'a',
  36. length: values.length,
  37. fields: [
  38. {
  39. name: 'test',
  40. type: FieldType.number,
  41. values: new ArrayVector(values),
  42. config: {
  43. min: -720,
  44. max: 540,
  45. },
  46. },
  47. ],
  48. };
  49. const supplier = getScalarDimension(frame, {
  50. min: -360,
  51. max: 360,
  52. field: 'test',
  53. fixed: 0,
  54. mode: ScalarDimensionMode.Clamped,
  55. });
  56. const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
  57. expect(clamped).toEqual([-360, 10, 360, 90, -210]);
  58. });
  59. it('keeps remainder after divisible by max', () => {
  60. const values = [-721, 10, 540, 390, -210];
  61. const frame: DataFrame = {
  62. name: 'a',
  63. length: values.length,
  64. fields: [
  65. {
  66. name: 'test',
  67. type: FieldType.number,
  68. values: new ArrayVector(values),
  69. config: {
  70. min: -721,
  71. max: 540,
  72. },
  73. },
  74. ],
  75. };
  76. const supplier = getScalarDimension(frame, {
  77. min: -360,
  78. max: 360,
  79. field: 'test',
  80. fixed: 0,
  81. mode: ScalarDimensionMode.Mod,
  82. });
  83. const remainder = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
  84. expect(remainder).toEqual([-1, 10, 180, 30, -210]);
  85. });
  86. });