variable_srv_init_specs.ts 5.3 KB

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