utils.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { createTheme, FieldType, MutableDataFrame, toDataFrame } from '@grafana/data';
  2. import { prepareGraphableFields } from './utils';
  3. describe('prepare timeseries graph', () => {
  4. it('errors with no time fields', () => {
  5. const input = [
  6. toDataFrame({
  7. fields: [
  8. { name: 'a', values: [1, 2, 3] },
  9. { name: 'b', values: ['a', 'b', 'c'] },
  10. ],
  11. }),
  12. ];
  13. const frames = prepareGraphableFields(input, createTheme());
  14. expect(frames).toBeNull();
  15. });
  16. it('requires a number or boolean value', () => {
  17. const input = [
  18. toDataFrame({
  19. fields: [
  20. { name: 'a', type: FieldType.time, values: [1, 2, 3] },
  21. { name: 'b', values: ['a', 'b', 'c'] },
  22. ],
  23. }),
  24. ];
  25. const frames = prepareGraphableFields(input, createTheme());
  26. expect(frames).toBeNull();
  27. });
  28. it('will graph numbers and boolean values', () => {
  29. const input = [
  30. toDataFrame({
  31. fields: [
  32. { name: 'a', type: FieldType.time, values: [1, 2, 3] },
  33. { name: 'b', values: ['a', 'b', 'c'] },
  34. { name: 'c', values: [true, false, true] },
  35. { name: 'd', values: [100, 200, 300] },
  36. ],
  37. }),
  38. ];
  39. const frames = prepareGraphableFields(input, createTheme());
  40. const out = frames![0];
  41. expect(out.fields.map((f) => f.name)).toEqual(['a', 'c', 'd']);
  42. const field = out.fields.find((f) => f.name === 'c');
  43. expect(field?.display).toBeDefined();
  44. expect(field!.display!(1)).toMatchInlineSnapshot(`
  45. Object {
  46. "color": "#808080",
  47. "numeric": 1,
  48. "percent": 1,
  49. "prefix": undefined,
  50. "suffix": undefined,
  51. "text": "True",
  52. }
  53. `);
  54. });
  55. it('will convert NaN and Infinty to nulls', () => {
  56. const df = new MutableDataFrame({
  57. fields: [
  58. { name: 'time', type: FieldType.time, values: [995, 9996, 9997, 9998, 9999] },
  59. { name: 'a', values: [-10, NaN, 10, -Infinity, +Infinity] },
  60. ],
  61. });
  62. const frames = prepareGraphableFields([df], createTheme());
  63. const field = frames![0].fields.find((f) => f.name === 'a');
  64. expect(field!.values.toArray()).toMatchInlineSnapshot(`
  65. Array [
  66. -10,
  67. null,
  68. 10,
  69. null,
  70. null,
  71. ]
  72. `);
  73. });
  74. it('will insert nulls given an interval value', () => {
  75. const df = new MutableDataFrame({
  76. fields: [
  77. { name: 'time', type: FieldType.time, config: { interval: 1 }, values: [1, 3, 6] },
  78. { name: 'a', values: [1, 2, 3] },
  79. ],
  80. });
  81. const frames = prepareGraphableFields([df], createTheme());
  82. const field = frames![0].fields.find((f) => f.name === 'a');
  83. expect(field!.values.toArray()).toMatchInlineSnapshot(`
  84. Array [
  85. 1,
  86. null,
  87. 2,
  88. null,
  89. null,
  90. 3,
  91. ]
  92. `);
  93. expect(frames![0].length).toEqual(6);
  94. });
  95. it('will insert and convert nulls to a configure "no value" value', () => {
  96. const df = new MutableDataFrame({
  97. fields: [
  98. { name: 'time', type: FieldType.time, config: { interval: 1 }, values: [1, 3, 6] },
  99. { name: 'a', config: { noValue: '20' }, values: [1, 2, 3] },
  100. ],
  101. });
  102. const frames = prepareGraphableFields([df], createTheme());
  103. const field = frames![0].fields.find((f) => f.name === 'a');
  104. expect(field!.values.toArray()).toMatchInlineSnapshot(`
  105. Array [
  106. 1,
  107. 20,
  108. 2,
  109. 20,
  110. 20,
  111. 3,
  112. ]
  113. `);
  114. expect(frames![0].length).toEqual(6);
  115. });
  116. });