guard.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { VariableSupportType } from '@grafana/data';
  2. import { LegacyVariableQueryEditor } from './editor/LegacyVariableQueryEditor';
  3. import { StandardVariableQueryEditor } from './editor/getVariableQueryEditor';
  4. import {
  5. hasCustomVariableSupport,
  6. hasDatasourceVariableSupport,
  7. hasLegacyVariableSupport,
  8. hasStandardVariableSupport,
  9. isLegacyQueryEditor,
  10. isQueryEditor,
  11. } from './guard';
  12. describe('type guards', () => {
  13. describe('hasLegacyVariableSupport', () => {
  14. describe('when called with a legacy data source', () => {
  15. it('should return true', () => {
  16. const datasource: any = { metricFindQuery: () => undefined };
  17. expect(hasLegacyVariableSupport(datasource)).toBe(true);
  18. });
  19. });
  20. describe('when called with data source without metricFindQuery function', () => {
  21. it('should return false', () => {
  22. const datasource: any = {};
  23. expect(hasLegacyVariableSupport(datasource)).toBe(false);
  24. });
  25. });
  26. describe('when called with a legacy data source with variable support', () => {
  27. it('should return false', () => {
  28. const datasource: any = { metricFindQuery: () => undefined, variables: {} };
  29. expect(hasLegacyVariableSupport(datasource)).toBe(false);
  30. });
  31. });
  32. });
  33. describe('hasStandardVariableSupport', () => {
  34. describe('when called with a data source with standard variable support', () => {
  35. it('should return true', () => {
  36. const datasource: any = {
  37. metricFindQuery: () => undefined,
  38. variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
  39. };
  40. expect(hasStandardVariableSupport(datasource)).toBe(true);
  41. });
  42. describe('and with a custom query', () => {
  43. it('should return true', () => {
  44. const datasource: any = {
  45. metricFindQuery: () => undefined,
  46. variables: {
  47. getType: () => VariableSupportType.Standard,
  48. toDataQuery: () => undefined,
  49. query: () => undefined,
  50. },
  51. };
  52. expect(hasStandardVariableSupport(datasource)).toBe(true);
  53. });
  54. });
  55. });
  56. describe('when called with a data source with partial standard variable support', () => {
  57. it('should return false', () => {
  58. const datasource: any = {
  59. metricFindQuery: () => undefined,
  60. variables: { getType: () => VariableSupportType.Standard, query: () => undefined },
  61. };
  62. expect(hasStandardVariableSupport(datasource)).toBe(false);
  63. });
  64. });
  65. describe('when called with a data source without standard variable support', () => {
  66. it('should return false', () => {
  67. const datasource: any = { metricFindQuery: () => undefined };
  68. expect(hasStandardVariableSupport(datasource)).toBe(false);
  69. });
  70. });
  71. });
  72. describe('hasCustomVariableSupport', () => {
  73. describe('when called with a data source with custom variable support', () => {
  74. it('should return true', () => {
  75. const datasource: any = {
  76. metricFindQuery: () => undefined,
  77. variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} },
  78. };
  79. expect(hasCustomVariableSupport(datasource)).toBe(true);
  80. });
  81. });
  82. describe('when called with a data source with custom variable support but without editor', () => {
  83. it('should return false', () => {
  84. const datasource: any = {
  85. metricFindQuery: () => undefined,
  86. variables: { getType: () => VariableSupportType.Custom, query: () => undefined },
  87. };
  88. expect(hasCustomVariableSupport(datasource)).toBe(false);
  89. });
  90. });
  91. describe('when called with a data source with custom variable support but without query', () => {
  92. it('should return false', () => {
  93. const datasource: any = {
  94. metricFindQuery: () => undefined,
  95. variables: { getType: () => VariableSupportType.Custom, editor: {} },
  96. };
  97. expect(hasCustomVariableSupport(datasource)).toBe(false);
  98. });
  99. });
  100. describe('when called with a data source without custom variable support', () => {
  101. it('should return false', () => {
  102. const datasource: any = { metricFindQuery: () => undefined };
  103. expect(hasCustomVariableSupport(datasource)).toBe(false);
  104. });
  105. });
  106. });
  107. describe('hasDatasourceVariableSupport', () => {
  108. describe('when called with a data source with datasource variable support', () => {
  109. it('should return true', () => {
  110. const datasource: any = {
  111. metricFindQuery: () => undefined,
  112. variables: { getType: () => VariableSupportType.Datasource },
  113. };
  114. expect(hasDatasourceVariableSupport(datasource)).toBe(true);
  115. });
  116. });
  117. describe('when called with a data source without datasource variable support', () => {
  118. it('should return false', () => {
  119. const datasource: any = { metricFindQuery: () => undefined };
  120. expect(hasDatasourceVariableSupport(datasource)).toBe(false);
  121. });
  122. });
  123. });
  124. });
  125. describe('isLegacyQueryEditor', () => {
  126. describe('happy cases', () => {
  127. describe('when called with a legacy query editor but without a legacy data source', () => {
  128. it('then is should return true', () => {
  129. const component: any = LegacyVariableQueryEditor;
  130. const datasource: any = {};
  131. expect(isLegacyQueryEditor(component, datasource)).toBe(true);
  132. });
  133. });
  134. describe('when called with a legacy data source but without a legacy query editor', () => {
  135. it('then is should return true', () => {
  136. const component: any = StandardVariableQueryEditor;
  137. const datasource: any = { metricFindQuery: () => undefined };
  138. expect(isLegacyQueryEditor(component, datasource)).toBe(true);
  139. });
  140. });
  141. });
  142. describe('negative cases', () => {
  143. describe('when called without component', () => {
  144. it('then is should return false', () => {
  145. const component: any = null;
  146. const datasource: any = { metricFindQuery: () => undefined };
  147. expect(isLegacyQueryEditor(component, datasource)).toBe(false);
  148. });
  149. });
  150. describe('when called without a legacy query editor and without a legacy data source', () => {
  151. it('then is should return false', () => {
  152. const component: any = StandardVariableQueryEditor;
  153. const datasource: any = {};
  154. expect(isLegacyQueryEditor(component, datasource)).toBe(false);
  155. });
  156. });
  157. });
  158. });
  159. describe('isQueryEditor', () => {
  160. describe('happy cases', () => {
  161. describe('when called without a legacy editor and with a data source with standard variable support', () => {
  162. it('then is should return true', () => {
  163. const component: any = StandardVariableQueryEditor;
  164. const datasource: any = {
  165. variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
  166. };
  167. expect(isQueryEditor(component, datasource)).toBe(true);
  168. });
  169. });
  170. describe('when called without a legacy editor and with a data source with custom variable support', () => {
  171. it('then is should return true', () => {
  172. const component: any = StandardVariableQueryEditor;
  173. const datasource: any = {
  174. variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} },
  175. };
  176. expect(isQueryEditor(component, datasource)).toBe(true);
  177. });
  178. });
  179. describe('when called without a legacy editor and with a data source with datasource variable support', () => {
  180. it('then is should return true', () => {
  181. const component: any = StandardVariableQueryEditor;
  182. const datasource: any = { variables: { getType: () => VariableSupportType.Datasource } };
  183. expect(isQueryEditor(component, datasource)).toBe(true);
  184. });
  185. });
  186. });
  187. describe('negative cases', () => {
  188. describe('when called without component', () => {
  189. it('then is should return false', () => {
  190. const component: any = null;
  191. const datasource: any = { metricFindQuery: () => undefined };
  192. expect(isQueryEditor(component, datasource)).toBe(false);
  193. });
  194. });
  195. describe('when called with a legacy query editor', () => {
  196. it('then is should return false', () => {
  197. const component: any = LegacyVariableQueryEditor;
  198. const datasource: any = { variables: { getType: () => VariableSupportType.Datasource } };
  199. expect(isQueryEditor(component, datasource)).toBe(false);
  200. });
  201. });
  202. describe('when called without a legacy query editor but with a legacy data source', () => {
  203. it('then is should return false', () => {
  204. const component: any = StandardVariableQueryEditor;
  205. const datasource: any = { metricFindQuery: () => undefined };
  206. expect(isQueryEditor(component, datasource)).toBe(false);
  207. });
  208. });
  209. });
  210. });