configFromQuery.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { toDataFrame, FieldType, ReducerID } from '@grafana/data';
  2. import { FieldConfigHandlerKey } from '../fieldToConfigMapping/fieldToConfigMapping';
  3. import { extractConfigFromQuery, ConfigFromQueryTransformOptions } from './configFromQuery';
  4. describe('config from data', () => {
  5. const config = toDataFrame({
  6. fields: [
  7. { name: 'Time', type: FieldType.time, values: [1, 2] },
  8. { name: 'Max', type: FieldType.number, values: [1, 10, 50] },
  9. { name: 'Min', type: FieldType.number, values: [1, 10, 5] },
  10. { name: 'Names', type: FieldType.string, values: ['first-name', 'middle', 'last-name'] },
  11. ],
  12. refId: 'A',
  13. });
  14. const seriesA = toDataFrame({
  15. fields: [
  16. { name: 'Time', type: FieldType.time, values: [1, 2, 3] },
  17. {
  18. name: 'Value',
  19. type: FieldType.number,
  20. values: [2, 3, 4],
  21. config: { displayName: 'SeriesA' },
  22. },
  23. ],
  24. });
  25. it('Select and apply with two frames and default mappings and reducer', () => {
  26. const options: ConfigFromQueryTransformOptions = {
  27. configRefId: 'A',
  28. mappings: [],
  29. };
  30. const results = extractConfigFromQuery(options, [config, seriesA]);
  31. expect(results.length).toBe(1);
  32. expect(results[0].fields[1].config.max).toBe(50);
  33. expect(results[0].fields[1].config.min).toBe(5);
  34. });
  35. it('Can apply to config frame if there is only one frame', () => {
  36. const options: ConfigFromQueryTransformOptions = {
  37. configRefId: 'A',
  38. mappings: [],
  39. };
  40. const results = extractConfigFromQuery(options, [config]);
  41. expect(results.length).toBe(1);
  42. expect(results[0].fields[1].name).toBe('Max');
  43. expect(results[0].fields[1].config.max).toBe(50);
  44. });
  45. it('With ignore mappings', () => {
  46. const options: ConfigFromQueryTransformOptions = {
  47. configRefId: 'A',
  48. mappings: [{ fieldName: 'Min', handlerKey: FieldConfigHandlerKey.Ignore }],
  49. };
  50. const results = extractConfigFromQuery(options, [config, seriesA]);
  51. expect(results.length).toBe(1);
  52. expect(results[0].fields[1].config.min).toEqual(undefined);
  53. expect(results[0].fields[1].config.max).toEqual(50);
  54. });
  55. it('With custom mappings', () => {
  56. const options: ConfigFromQueryTransformOptions = {
  57. configRefId: 'A',
  58. mappings: [{ fieldName: 'Min', handlerKey: 'decimals' }],
  59. };
  60. const results = extractConfigFromQuery(options, [config, seriesA]);
  61. expect(results.length).toBe(1);
  62. expect(results[0].fields[1].config.decimals).toBe(5);
  63. });
  64. it('With custom reducer', () => {
  65. const options: ConfigFromQueryTransformOptions = {
  66. configRefId: 'A',
  67. mappings: [{ fieldName: 'Max', handlerKey: 'max', reducerId: ReducerID.min }],
  68. };
  69. const results = extractConfigFromQuery(options, [config, seriesA]);
  70. expect(results.length).toBe(1);
  71. expect(results[0].fields[1].config.max).toBe(1);
  72. });
  73. it('With custom matcher and displayName mapping', () => {
  74. const options: ConfigFromQueryTransformOptions = {
  75. configRefId: 'A',
  76. mappings: [{ fieldName: 'Names', handlerKey: 'displayName', reducerId: ReducerID.first }],
  77. applyTo: { id: 'byName', options: 'Value' },
  78. };
  79. const results = extractConfigFromQuery(options, [config, seriesA]);
  80. expect(results.length).toBe(1);
  81. expect(results[0].fields[1].config.displayName).toBe('first-name');
  82. });
  83. });
  84. describe('value mapping from data', () => {
  85. const config = toDataFrame({
  86. fields: [
  87. { name: 'value', type: FieldType.number, values: [1, 2, 3] },
  88. { name: 'text', type: FieldType.string, values: ['one', 'two', 'three'] },
  89. { name: 'color', type: FieldType.string, values: ['red', 'blue', 'green'] },
  90. ],
  91. refId: 'config',
  92. });
  93. const seriesA = toDataFrame({
  94. fields: [
  95. { name: 'Time', type: FieldType.time, values: [1, 2, 3] },
  96. {
  97. name: 'Value',
  98. type: FieldType.number,
  99. values: [1, 2, 3],
  100. config: {},
  101. },
  102. ],
  103. });
  104. it('Should take all field values and map to value mappings', () => {
  105. const options: ConfigFromQueryTransformOptions = {
  106. configRefId: 'config',
  107. mappings: [
  108. { fieldName: 'value', handlerKey: 'mappings.value' },
  109. { fieldName: 'color', handlerKey: 'mappings.color' },
  110. { fieldName: 'text', handlerKey: 'mappings.text' },
  111. ],
  112. };
  113. const results = extractConfigFromQuery(options, [config, seriesA]);
  114. expect(results[0].fields[1].config.mappings).toMatchInlineSnapshot(`
  115. Array [
  116. Object {
  117. "options": Object {
  118. "1": Object {
  119. "color": "red",
  120. "index": 0,
  121. "text": "one",
  122. },
  123. "2": Object {
  124. "color": "blue",
  125. "index": 1,
  126. "text": "two",
  127. },
  128. "3": Object {
  129. "color": "green",
  130. "index": 2,
  131. "text": "three",
  132. },
  133. },
  134. "type": "value",
  135. },
  136. ]
  137. `);
  138. });
  139. });