variable_srv_init.jest.ts 7.4 KB

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