variable_srv_init_specs.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. describe('VariableSrv init', function() {
  7. var ctx = new helpers.ControllerTestContext();
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.controllers'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(
  12. angularMocks.module(function($compileProvider) {
  13. $compileProvider.preAssignBindingsEnabled(true);
  14. })
  15. );
  16. beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location']));
  17. beforeEach(
  18. angularMocks.inject(($rootScope, $q, $location, $injector) => {
  19. ctx.$q = $q;
  20. ctx.$rootScope = $rootScope;
  21. ctx.$location = $location;
  22. ctx.variableSrv = $injector.get('variableSrv');
  23. ctx.$rootScope.$digest();
  24. })
  25. );
  26. function describeInitScenario(desc, fn) {
  27. describe(desc, function() {
  28. var scenario: any = {
  29. urlParams: {},
  30. setup: setupFn => {
  31. scenario.setupFn = setupFn;
  32. },
  33. };
  34. beforeEach(function() {
  35. scenario.setupFn();
  36. ctx.datasource = {};
  37. ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  38. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ctx.datasource));
  39. ctx.datasourceSrv.getMetricSources = sinon.stub().returns(scenario.metricSources);
  40. ctx.$location.search = sinon.stub().returns(scenario.urlParams);
  41. ctx.dashboard = {
  42. templating: { list: scenario.variables },
  43. events: new Emitter(),
  44. };
  45. ctx.variableSrv.init(ctx.dashboard);
  46. ctx.$rootScope.$digest();
  47. scenario.variables = ctx.variableSrv.variables;
  48. });
  49. fn(scenario);
  50. });
  51. }
  52. ['query', 'interval', 'custom', 'datasource'].forEach(type => {
  53. describeInitScenario('when setting ' + type + ' variable via url', scenario => {
  54. scenario.setup(() => {
  55. scenario.variables = [
  56. {
  57. name: 'apps',
  58. type: type,
  59. current: { text: 'test', value: 'test' },
  60. options: [{ text: 'test', value: 'test' }],
  61. },
  62. ];
  63. scenario.urlParams['var-apps'] = 'new';
  64. scenario.metricSources = [];
  65. });
  66. it('should update current value', () => {
  67. expect(scenario.variables[0].current.value).to.be('new');
  68. expect(scenario.variables[0].current.text).to.be('new');
  69. });
  70. });
  71. });
  72. describe('given dependent variables', () => {
  73. var variableList = [
  74. {
  75. name: 'app',
  76. type: 'query',
  77. query: '',
  78. current: { text: 'app1', value: 'app1' },
  79. options: [{ text: 'app1', value: 'app1' }],
  80. },
  81. {
  82. name: 'server',
  83. type: 'query',
  84. refresh: 1,
  85. query: '$app.*',
  86. current: { text: 'server1', value: 'server1' },
  87. options: [{ text: 'server1', value: 'server1' }],
  88. },
  89. ];
  90. describeInitScenario('when setting parent var from url', scenario => {
  91. scenario.setup(() => {
  92. scenario.variables = _.cloneDeep(variableList);
  93. scenario.urlParams['var-app'] = 'google';
  94. scenario.queryResult = [{ text: 'google-server1' }, { text: 'google-server2' }];
  95. });
  96. it('should update child variable', () => {
  97. expect(scenario.variables[1].options.length).to.be(2);
  98. expect(scenario.variables[1].current.text).to.be('google-server1');
  99. });
  100. it('should only update it once', () => {
  101. expect(ctx.datasource.metricFindQuery.callCount).to.be(1);
  102. });
  103. });
  104. });
  105. describeInitScenario('when datasource variable is initialized', scenario => {
  106. scenario.setup(() => {
  107. scenario.variables = [
  108. {
  109. type: 'datasource',
  110. query: 'graphite',
  111. name: 'test',
  112. current: { value: 'backend4_pee', text: 'backend4_pee' },
  113. regex: '/pee$/',
  114. },
  115. ];
  116. scenario.metricSources = [
  117. { name: 'backend1', meta: { id: 'influx' } },
  118. { name: 'backend2_pee', meta: { id: 'graphite' } },
  119. { name: 'backend3', meta: { id: 'graphite' } },
  120. { name: 'backend4_pee', meta: { id: 'graphite' } },
  121. ];
  122. });
  123. it('should update current value', function() {
  124. var variable = ctx.variableSrv.variables[0];
  125. expect(variable.options.length).to.be(2);
  126. });
  127. });
  128. describeInitScenario('when template variable is present in url multiple times', scenario => {
  129. scenario.setup(() => {
  130. scenario.variables = [
  131. {
  132. name: 'apps',
  133. type: 'query',
  134. multi: true,
  135. current: { text: 'val1', value: 'val1' },
  136. options: [
  137. { text: 'val1', value: 'val1' },
  138. { text: 'val2', value: 'val2' },
  139. { text: 'val3', value: 'val3', selected: true },
  140. ],
  141. },
  142. ];
  143. scenario.urlParams['var-apps'] = ['val2', 'val1'];
  144. });
  145. it('should update current value', function() {
  146. var variable = ctx.variableSrv.variables[0];
  147. expect(variable.current.value.length).to.be(2);
  148. expect(variable.current.value[0]).to.be('val2');
  149. expect(variable.current.value[1]).to.be('val1');
  150. expect(variable.current.text).to.be('val2 + val1');
  151. expect(variable.options[0].selected).to.be(true);
  152. expect(variable.options[1].selected).to.be(true);
  153. });
  154. it('should set options that are not in value to selected false', function() {
  155. var variable = ctx.variableSrv.variables[0];
  156. expect(variable.options[2].selected).to.be(false);
  157. });
  158. });
  159. });