variable_srv_init.jest.ts 7.4 KB

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