variable_srv_init.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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('when template variable is present in url multiple times using key/values', scenario => {
  167. scenario.setup(() => {
  168. scenario.variables = [
  169. {
  170. name: 'apps',
  171. type: 'query',
  172. multi: true,
  173. current: { text: 'Val1', value: 'val1' },
  174. options: [
  175. { text: 'Val1', value: 'val1' },
  176. { text: 'Val2', value: 'val2' },
  177. { text: 'Val3', value: 'val3', selected: true },
  178. ],
  179. },
  180. ];
  181. scenario.urlParams['var-apps'] = ['val2', 'val1'];
  182. });
  183. it('should update current value', () => {
  184. const variable = ctx.variableSrv.variables[0];
  185. expect(variable.current.value.length).toBe(2);
  186. expect(variable.current.value[0]).toBe('val2');
  187. expect(variable.current.value[1]).toBe('val1');
  188. expect(variable.current.text).toBe('Val2 + Val1');
  189. expect(variable.options[0].selected).toBe(true);
  190. expect(variable.options[1].selected).toBe(true);
  191. });
  192. it('should set options that are not in value to selected false', () => {
  193. const variable = ctx.variableSrv.variables[0];
  194. expect(variable.options[2].selected).toBe(false);
  195. });
  196. });
  197. });
  198. function getVarMockConstructor(variable, model, ctx) {
  199. switch (model.model.type) {
  200. case 'datasource':
  201. return new variable(model.model, ctx.datasourceSrv, ctx.variableSrv, ctx.templateSrv);
  202. case 'query':
  203. return new variable(model.model, ctx.datasourceSrv, ctx.templateSrv, ctx.variableSrv);
  204. case 'interval':
  205. return new variable(model.model, {}, ctx.templateSrv, ctx.variableSrv);
  206. case 'custom':
  207. return new variable(model.model, ctx.variableSrv);
  208. default:
  209. return new variable(model.model);
  210. }
  211. }