variable_srv_init.test.ts 8.2 KB

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