variable_srv_init.test.ts 7.2 KB

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