variable_srv_init.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import '../all';
  2. import _ from 'lodash';
  3. import { VariableSrv } from '../variable_srv';
  4. import { DashboardModel } from '../../dashboard/dashboard_model';
  5. import $q from 'q';
  6. describe('VariableSrv init', function(this: any) {
  7. const templateSrv = {
  8. init: vars => {
  9. this.variables = vars;
  10. },
  11. variableInitialized: () => {},
  12. updateTemplateData: () => {},
  13. replace: str =>
  14. str.replace(this.regex, match => {
  15. return match;
  16. }),
  17. };
  18. const $injector = {} as any;
  19. const $rootscope = {
  20. $on: () => {},
  21. };
  22. let ctx = {} as any;
  23. function describeInitScenario(desc, fn) {
  24. describe(desc, () => {
  25. const scenario: any = {
  26. urlParams: {},
  27. setup: setupFn => {
  28. scenario.setupFn = setupFn;
  29. },
  30. };
  31. beforeEach(async () => {
  32. scenario.setupFn();
  33. ctx = {
  34. datasource: {
  35. metricFindQuery: jest.fn(() => Promise.resolve(scenario.queryResult)),
  36. },
  37. datasourceSrv: {
  38. get: () => Promise.resolve(ctx.datasource),
  39. getMetricSources: () => scenario.metricSources,
  40. },
  41. templateSrv,
  42. };
  43. ctx.variableSrv = new VariableSrv($rootscope, $q, {}, $injector, templateSrv);
  44. $injector.instantiate = (variable, model) => {
  45. return getVarMockConstructor(variable, model, ctx);
  46. };
  47. ctx.variableSrv.datasource = ctx.datasource;
  48. ctx.variableSrv.datasourceSrv = ctx.datasourceSrv;
  49. ctx.variableSrv.$location.search = () => scenario.urlParams;
  50. ctx.variableSrv.dashboard = new DashboardModel({
  51. templating: { list: scenario.variables },
  52. });
  53. await ctx.variableSrv.init(ctx.variableSrv.dashboard);
  54. scenario.variables = ctx.variableSrv.variables;
  55. });
  56. fn(scenario);
  57. });
  58. }
  59. ['query', 'interval', 'custom', 'datasource'].forEach(type => {
  60. describeInitScenario('when setting ' + type + ' variable via url', scenario => {
  61. scenario.setup(() => {
  62. scenario.variables = [
  63. {
  64. name: 'apps',
  65. type: type,
  66. current: { text: 'Test', value: 'test' },
  67. options: [{ text: 'Test', value: 'test' }],
  68. },
  69. ];
  70. scenario.urlParams['var-apps'] = 'new';
  71. scenario.metricSources = [];
  72. });
  73. it('should update current value', () => {
  74. expect(scenario.variables[0].current.value).toBe('new');
  75. expect(scenario.variables[0].current.text).toBe('new');
  76. });
  77. });
  78. });
  79. describe('given dependent variables', () => {
  80. const variableList = [
  81. {
  82. name: 'app',
  83. type: 'query',
  84. query: '',
  85. current: { text: 'app1', value: 'app1' },
  86. options: [{ text: 'app1', value: 'app1' }],
  87. },
  88. {
  89. name: 'server',
  90. type: 'query',
  91. refresh: 1,
  92. query: '$app.*',
  93. current: { text: 'server1', value: 'server1' },
  94. options: [{ text: 'server1', value: 'server1' }],
  95. },
  96. ];
  97. describeInitScenario('when setting parent const from url', scenario => {
  98. scenario.setup(() => {
  99. scenario.variables = _.cloneDeep(variableList);
  100. scenario.urlParams['var-app'] = 'google';
  101. scenario.queryResult = [{ text: 'google-server1' }, { text: 'google-server2' }];
  102. });
  103. it('should update child variable', () => {
  104. expect(scenario.variables[1].options.length).toBe(2);
  105. expect(scenario.variables[1].current.text).toBe('google-server1');
  106. });
  107. it('should only update it once', () => {
  108. expect(ctx.variableSrv.datasource.metricFindQuery).toHaveBeenCalledTimes(1);
  109. });
  110. });
  111. });
  112. describeInitScenario('when datasource variable is initialized', scenario => {
  113. scenario.setup(() => {
  114. scenario.variables = [
  115. {
  116. type: 'datasource',
  117. query: 'graphite',
  118. name: 'test',
  119. current: { value: 'backend4_pee', text: 'backend4_pee' },
  120. regex: '/pee$/',
  121. },
  122. ];
  123. scenario.metricSources = [
  124. { name: 'backend1', meta: { id: 'influx' } },
  125. { name: 'backend2_pee', meta: { id: 'graphite' } },
  126. { name: 'backend3', meta: { id: 'graphite' } },
  127. { name: 'backend4_pee', meta: { id: 'graphite' } },
  128. ];
  129. });
  130. it('should update current value', () => {
  131. const variable = ctx.variableSrv.variables[0];
  132. expect(variable.options.length).toBe(2);
  133. });
  134. });
  135. describeInitScenario('when template variable is present in url multiple times', scenario => {
  136. scenario.setup(() => {
  137. scenario.variables = [
  138. {
  139. name: 'apps',
  140. type: 'query',
  141. multi: true,
  142. current: { text: 'Val1', value: 'val1' },
  143. options: [
  144. { text: 'Val1', value: 'val1' },
  145. { text: 'Val2', value: 'val2' },
  146. { text: 'Val3', value: 'val3', selected: true },
  147. ],
  148. },
  149. ];
  150. scenario.urlParams['var-apps'] = ['val2', 'val1'];
  151. });
  152. it('should update current value', () => {
  153. const variable = ctx.variableSrv.variables[0];
  154. expect(variable.current.value.length).toBe(2);
  155. expect(variable.current.value[0]).toBe('val2');
  156. expect(variable.current.value[1]).toBe('val1');
  157. expect(variable.current.text).toBe('Val2 + Val1');
  158. expect(variable.options[0].selected).toBe(true);
  159. expect(variable.options[1].selected).toBe(true);
  160. });
  161. it('should set options that are not in value to selected false', () => {
  162. const variable = ctx.variableSrv.variables[0];
  163. expect(variable.options[2].selected).toBe(false);
  164. });
  165. });
  166. describeInitScenario(
  167. 'when template variable is present in url multiple times and variables have no text',
  168. scenario => {
  169. scenario.setup(() => {
  170. scenario.variables = [
  171. {
  172. name: 'apps',
  173. type: 'query',
  174. multi: true,
  175. },
  176. ];
  177. scenario.urlParams['var-apps'] = ['val1', 'val2'];
  178. });
  179. it('should display concatenated values in text', () => {
  180. const variable = ctx.variableSrv.variables[0];
  181. expect(variable.current.value.length).toBe(2);
  182. expect(variable.current.value[0]).toBe('val1');
  183. expect(variable.current.value[1]).toBe('val2');
  184. expect(variable.current.text).toBe('val1 + val2');
  185. });
  186. }
  187. );
  188. describeInitScenario('when template variable is present in url multiple times using key/values', scenario => {
  189. scenario.setup(() => {
  190. scenario.variables = [
  191. {
  192. name: 'apps',
  193. type: 'query',
  194. multi: true,
  195. current: { text: 'Val1', value: 'val1' },
  196. options: [
  197. { text: 'Val1', value: 'val1' },
  198. { text: 'Val2', value: 'val2' },
  199. { text: 'Val3', value: 'val3', selected: true },
  200. ],
  201. },
  202. ];
  203. scenario.urlParams['var-apps'] = ['val2', 'val1'];
  204. });
  205. it('should update current value', () => {
  206. const variable = ctx.variableSrv.variables[0];
  207. expect(variable.current.value.length).toBe(2);
  208. expect(variable.current.value[0]).toBe('val2');
  209. expect(variable.current.value[1]).toBe('val1');
  210. expect(variable.current.text).toBe('Val2 + Val1');
  211. expect(variable.options[0].selected).toBe(true);
  212. expect(variable.options[1].selected).toBe(true);
  213. });
  214. it('should set options that are not in value to selected false', () => {
  215. const variable = ctx.variableSrv.variables[0];
  216. expect(variable.options[2].selected).toBe(false);
  217. });
  218. });
  219. });
  220. function getVarMockConstructor(variable, model, ctx) {
  221. switch (model.model.type) {
  222. case 'datasource':
  223. return new variable(model.model, ctx.datasourceSrv, ctx.variableSrv, ctx.templateSrv);
  224. case 'query':
  225. return new variable(model.model, ctx.datasourceSrv, ctx.templateSrv, ctx.variableSrv);
  226. case 'interval':
  227. return new variable(model.model, {}, ctx.templateSrv, ctx.variableSrv);
  228. case 'custom':
  229. return new variable(model.model, ctx.variableSrv);
  230. default:
  231. return new variable(model.model);
  232. }
  233. }