variable_srv_init_specs.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {
  2. describe,
  3. beforeEach,
  4. it,
  5. sinon,
  6. expect,
  7. angularMocks,
  8. } from 'test/lib/common';
  9. import '../all';
  10. import _ from 'lodash';
  11. import helpers from 'test/specs/helpers';
  12. import { Emitter } from 'app/core/core';
  13. describe('VariableSrv init', function() {
  14. var ctx = new helpers.ControllerTestContext();
  15. beforeEach(angularMocks.module('grafana.core'));
  16. beforeEach(angularMocks.module('grafana.controllers'));
  17. beforeEach(angularMocks.module('grafana.services'));
  18. beforeEach(
  19. angularMocks.module(function($compileProvider) {
  20. $compileProvider.preAssignBindingsEnabled(true);
  21. })
  22. );
  23. beforeEach(
  24. ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location'])
  25. );
  26. beforeEach(
  27. angularMocks.inject(($rootScope, $q, $location, $injector) => {
  28. ctx.$q = $q;
  29. ctx.$rootScope = $rootScope;
  30. ctx.$location = $location;
  31. ctx.variableSrv = $injector.get('variableSrv');
  32. ctx.$rootScope.$digest();
  33. })
  34. );
  35. function describeInitScenario(desc, fn) {
  36. describe(desc, function() {
  37. var scenario: any = {
  38. urlParams: {},
  39. setup: setupFn => {
  40. scenario.setupFn = setupFn;
  41. },
  42. };
  43. beforeEach(function() {
  44. scenario.setupFn();
  45. ctx.datasource = {};
  46. ctx.datasource.metricFindQuery = sinon
  47. .stub()
  48. .returns(ctx.$q.when(scenario.queryResult));
  49. ctx.datasourceSrv.get = sinon
  50. .stub()
  51. .returns(ctx.$q.when(ctx.datasource));
  52. ctx.datasourceSrv.getMetricSources = sinon
  53. .stub()
  54. .returns(scenario.metricSources);
  55. ctx.$location.search = sinon.stub().returns(scenario.urlParams);
  56. ctx.dashboard = {
  57. templating: { list: scenario.variables },
  58. events: new Emitter(),
  59. };
  60. ctx.variableSrv.init(ctx.dashboard);
  61. ctx.$rootScope.$digest();
  62. scenario.variables = ctx.variableSrv.variables;
  63. });
  64. fn(scenario);
  65. });
  66. }
  67. ['query', 'interval', 'custom', 'datasource'].forEach(type => {
  68. describeInitScenario(
  69. 'when setting ' + type + ' variable via url',
  70. scenario => {
  71. scenario.setup(() => {
  72. scenario.variables = [
  73. {
  74. name: 'apps',
  75. type: type,
  76. current: { text: 'test', value: 'test' },
  77. options: [{ text: 'test', value: 'test' }],
  78. },
  79. ];
  80. scenario.urlParams['var-apps'] = 'new';
  81. scenario.metricSources = [];
  82. });
  83. it('should update current value', () => {
  84. expect(scenario.variables[0].current.value).to.be('new');
  85. expect(scenario.variables[0].current.text).to.be('new');
  86. });
  87. }
  88. );
  89. });
  90. describe('given dependent variables', () => {
  91. var variableList = [
  92. {
  93. name: 'app',
  94. type: 'query',
  95. query: '',
  96. current: { text: 'app1', value: 'app1' },
  97. options: [{ text: 'app1', value: 'app1' }],
  98. },
  99. {
  100. name: 'server',
  101. type: 'query',
  102. refresh: 1,
  103. query: '$app.*',
  104. current: { text: 'server1', value: 'server1' },
  105. options: [{ text: 'server1', value: 'server1' }],
  106. },
  107. ];
  108. describeInitScenario('when setting parent var from url', scenario => {
  109. scenario.setup(() => {
  110. scenario.variables = _.cloneDeep(variableList);
  111. scenario.urlParams['var-app'] = 'google';
  112. scenario.queryResult = [
  113. { text: 'google-server1' },
  114. { text: 'google-server2' },
  115. ];
  116. });
  117. it('should update child variable', () => {
  118. expect(scenario.variables[1].options.length).to.be(2);
  119. expect(scenario.variables[1].current.text).to.be('google-server1');
  120. });
  121. it('should only update it once', () => {
  122. expect(ctx.datasource.metricFindQuery.callCount).to.be(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).to.be(2);
  147. });
  148. });
  149. describeInitScenario(
  150. 'when template variable is present in url multiple times',
  151. scenario => {
  152. scenario.setup(() => {
  153. scenario.variables = [
  154. {
  155. name: 'apps',
  156. type: 'query',
  157. multi: true,
  158. current: { text: 'val1', value: 'val1' },
  159. options: [
  160. { text: 'val1', value: 'val1' },
  161. { text: 'val2', value: 'val2' },
  162. { text: 'val3', value: 'val3', selected: true },
  163. ],
  164. },
  165. ];
  166. scenario.urlParams['var-apps'] = ['val2', 'val1'];
  167. });
  168. it('should update current value', function() {
  169. var variable = ctx.variableSrv.variables[0];
  170. expect(variable.current.value.length).to.be(2);
  171. expect(variable.current.value[0]).to.be('val2');
  172. expect(variable.current.value[1]).to.be('val1');
  173. expect(variable.current.text).to.be('val2 + val1');
  174. expect(variable.options[0].selected).to.be(true);
  175. expect(variable.options[1].selected).to.be(true);
  176. });
  177. it('should set options that are not in value to selected false', function() {
  178. var variable = ctx.variableSrv.variables[0];
  179. expect(variable.options[2].selected).to.be(false);
  180. });
  181. }
  182. );
  183. });