variable_srv_init_specs.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. });
  53. it('should update current value', () => {
  54. expect(scenario.variables[0].current.value).to.be("new");
  55. expect(scenario.variables[0].current.text).to.be("new");
  56. });
  57. });
  58. });
  59. describe('given dependent variables', () => {
  60. var variableList = [
  61. {
  62. name: 'app',
  63. type: 'query',
  64. query: '',
  65. current: {text: "app1", value: "app1"},
  66. options: [{text: "app1", value: "app1"}]
  67. },
  68. {
  69. name: 'server',
  70. type: 'query',
  71. refresh: 1,
  72. query: '$app.*',
  73. current: {text: "server1", value: "server1"},
  74. options: [{text: "server1", value: "server1"}]
  75. },
  76. ];
  77. describeInitScenario('when setting parent var from url', scenario => {
  78. scenario.setup(() => {
  79. scenario.variables = _.cloneDeep(variableList);
  80. scenario.urlParams["var-app"] = "google";
  81. scenario.queryResult = [{text: 'google-server1'}, {text: 'google-server2'}];
  82. });
  83. it('should update child variable', () => {
  84. expect(scenario.variables[1].options.length).to.be(2);
  85. expect(scenario.variables[1].current.text).to.be("google-server1");
  86. });
  87. it('should only update it once', () => {
  88. expect(ctx.datasource.metricFindQuery.callCount).to.be(1);
  89. });
  90. });
  91. });
  92. describeInitScenario('when template variable is present in url multiple times', scenario => {
  93. scenario.setup(() => {
  94. scenario.variables = [{
  95. name: 'apps',
  96. type: 'query',
  97. multi: true,
  98. current: {text: "val1", value: "val1"},
  99. options: [{text: "val1", value: "val1"}, {text: 'val2', value: 'val2'}, {text: 'val3', value: 'val3', selected: true}]
  100. }];
  101. scenario.urlParams["var-apps"] = ["val2", "val1"];
  102. });
  103. it('should update current value', function() {
  104. var variable = ctx.variableSrv.variables[0];
  105. expect(variable.current.value.length).to.be(2);
  106. expect(variable.current.value[0]).to.be("val2");
  107. expect(variable.current.value[1]).to.be("val1");
  108. expect(variable.current.text).to.be("val2 + val1");
  109. expect(variable.options[0].selected).to.be(true);
  110. expect(variable.options[1].selected).to.be(true);
  111. });
  112. it('should set options that are not in value to selected false', function() {
  113. var variable = ctx.variableSrv.variables[0];
  114. expect(variable.options[2].selected).to.be(false);
  115. });
  116. });
  117. });