getVisualizationOptions.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { EventBusSrv, FieldType, getDefaultTimeRange, LoadingState, toDataFrame } from '@grafana/data';
  2. import { getStandardEditorContext } from './getVisualizationOptions';
  3. describe('getStandardEditorContext', () => {
  4. it('defaults the series data to an empty array', () => {
  5. const editorContext = getStandardEditorContext({
  6. data: undefined,
  7. replaceVariables: jest.fn(),
  8. options: {},
  9. eventBus: new EventBusSrv(),
  10. instanceState: {},
  11. });
  12. expect(editorContext.data).toEqual([]);
  13. });
  14. it('returns suggestions for empty data', () => {
  15. const editorContext = getStandardEditorContext({
  16. data: undefined,
  17. replaceVariables: jest.fn(),
  18. options: {},
  19. eventBus: new EventBusSrv(),
  20. instanceState: {},
  21. });
  22. expect(editorContext.getSuggestions).toBeDefined();
  23. expect(editorContext.getSuggestions?.()).toEqual([
  24. {
  25. documentation: 'Name of the series',
  26. label: 'Name',
  27. origin: 'series',
  28. value: '__series.name',
  29. },
  30. {
  31. documentation: 'Field name of the clicked datapoint (in ms epoch)',
  32. label: 'Name',
  33. origin: 'field',
  34. value: '__field.name',
  35. },
  36. {
  37. documentation: 'Adds current variables',
  38. label: 'All variables',
  39. origin: 'template',
  40. value: '__all_variables',
  41. },
  42. {
  43. documentation: 'Adds current time range',
  44. label: 'Time range',
  45. origin: 'built-in',
  46. value: '__url_time_range',
  47. },
  48. {
  49. documentation: "Adds current time range's from value",
  50. label: 'Time range: from',
  51. origin: 'built-in',
  52. value: '__from',
  53. },
  54. {
  55. documentation: "Adds current time range's to value",
  56. label: 'Time range: to',
  57. origin: 'built-in',
  58. value: '__to',
  59. },
  60. ]);
  61. });
  62. it('returns suggestions for non-empty data', () => {
  63. const series = [
  64. toDataFrame({
  65. fields: [
  66. { name: 'time', type: FieldType.time },
  67. { name: 'score', type: FieldType.number },
  68. ],
  69. }),
  70. ];
  71. const panelData = {
  72. series,
  73. timeRange: getDefaultTimeRange(),
  74. state: LoadingState.Done,
  75. };
  76. const editorContext = getStandardEditorContext({
  77. data: panelData,
  78. replaceVariables: jest.fn(),
  79. options: {},
  80. eventBus: new EventBusSrv(),
  81. instanceState: {},
  82. });
  83. expect(editorContext.getSuggestions).toBeDefined();
  84. expect(editorContext.getSuggestions?.()).toEqual([
  85. {
  86. documentation: 'Name of the series',
  87. label: 'Name',
  88. origin: 'series',
  89. value: '__series.name',
  90. },
  91. {
  92. documentation: 'Field name of the clicked datapoint (in ms epoch)',
  93. label: 'Name',
  94. origin: 'field',
  95. value: '__field.name',
  96. },
  97. {
  98. documentation: 'Formatted value for time on the same row',
  99. label: 'time',
  100. origin: 'fields',
  101. value: '__data.fields.time',
  102. },
  103. {
  104. documentation: 'Formatted value for score on the same row',
  105. label: 'score',
  106. origin: 'fields',
  107. value: '__data.fields.score',
  108. },
  109. {
  110. documentation: 'Enter the field order',
  111. label: 'Select by index',
  112. origin: 'fields',
  113. value: '__data.fields[0]',
  114. },
  115. {
  116. documentation: 'the numeric field value',
  117. label: 'Show numeric value',
  118. origin: 'fields',
  119. value: '__data.fields.score.numeric',
  120. },
  121. {
  122. documentation: 'the text value',
  123. label: 'Show text value',
  124. origin: 'fields',
  125. value: '__data.fields.score.text',
  126. },
  127. {
  128. documentation: 'Adds current variables',
  129. label: 'All variables',
  130. origin: 'template',
  131. value: '__all_variables',
  132. },
  133. {
  134. documentation: 'Adds current time range',
  135. label: 'Time range',
  136. origin: 'built-in',
  137. value: '__url_time_range',
  138. },
  139. {
  140. documentation: "Adds current time range's from value",
  141. label: 'Time range: from',
  142. origin: 'built-in',
  143. value: '__from',
  144. },
  145. {
  146. documentation: "Adds current time range's to value",
  147. label: 'Time range: to',
  148. origin: 'built-in',
  149. value: '__to',
  150. },
  151. ]);
  152. });
  153. });