variable_srv_init_specs.ts 5.4 KB

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