templateValuesSrv-specs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. define([
  2. '../mocks/dashboard-mock',
  3. './helpers',
  4. 'moment',
  5. 'app/features/templating/templateValuesSrv'
  6. ], function(dashboardMock, helpers, moment) {
  7. 'use strict';
  8. describe('templateValuesSrv', function() {
  9. var ctx = new helpers.ServiceTestContext();
  10. beforeEach(module('grafana.services'));
  11. beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv', '$location']));
  12. beforeEach(ctx.createService('templateValuesSrv'));
  13. describe('update interval variable options', function() {
  14. var variable = { type: 'interval', query: 'auto,1s,2h,5h,1d', name: 'test' };
  15. beforeEach(function() {
  16. ctx.service.updateOptions(variable);
  17. });
  18. it('should update options array', function() {
  19. expect(variable.options.length).to.be(5);
  20. expect(variable.options[1].text).to.be('1s');
  21. expect(variable.options[1].value).to.be('1s');
  22. });
  23. });
  24. describe('when template variable is present in url', function() {
  25. var variable = {
  26. name: 'apps',
  27. current: {text: "test", value: "test"},
  28. options: [{text: "test", value: "test"}]
  29. };
  30. beforeEach(function(done) {
  31. var dashboard = { templating: { list: [variable] } };
  32. var urlParams = {};
  33. urlParams["var-apps"] = "new";
  34. ctx.$location.search = sinon.stub().returns(urlParams);
  35. ctx.service.init(dashboard).then(function() { done(); });
  36. ctx.$rootScope.$digest();
  37. });
  38. it('should update current value', function() {
  39. expect(variable.current.value).to.be("new");
  40. expect(variable.current.text).to.be("new");
  41. });
  42. });
  43. describe('when template variable is present in url multiple times', function() {
  44. var variable = {
  45. name: 'apps',
  46. multi: true,
  47. current: {text: "val1", value: "val1"},
  48. options: [{text: "val1", value: "val1"}, {text: 'val2', value: 'val2'}, {text: 'val3', value: 'val3', selected: true}]
  49. };
  50. beforeEach(function(done) {
  51. var dashboard = { templating: { list: [variable] } };
  52. var urlParams = {};
  53. urlParams["var-apps"] = ["val2", "val1"];
  54. ctx.$location.search = sinon.stub().returns(urlParams);
  55. ctx.service.init(dashboard).then(function() { done(); });
  56. ctx.$rootScope.$digest();
  57. });
  58. it('should update current value', function() {
  59. expect(variable.current.value.length).to.be(2);
  60. expect(variable.current.value[0]).to.be("val2");
  61. expect(variable.current.value[1]).to.be("val1");
  62. expect(variable.current.text).to.be("val2 + val1");
  63. expect(variable.options[0].selected).to.be(true);
  64. expect(variable.options[1].selected).to.be(true);
  65. });
  66. it('should set options that are not in value to selected false', function() {
  67. expect(variable.options[2].selected).to.be(false);
  68. });
  69. });
  70. function describeUpdateVariable(desc, fn) {
  71. describe(desc, function() {
  72. var scenario = {};
  73. scenario.setup = function(setupFn) {
  74. scenario.setupFn = setupFn;
  75. };
  76. beforeEach(function() {
  77. scenario.setupFn();
  78. var ds = {};
  79. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  80. ctx.datasourceSrv.get = sinon.stub().returns(ctx.$q.when(ds));
  81. ctx.service.updateOptions(scenario.variable);
  82. ctx.$rootScope.$digest();
  83. });
  84. fn(scenario);
  85. });
  86. }
  87. describeUpdateVariable('interval variable without auto', function(scenario) {
  88. scenario.setup(function() {
  89. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  90. });
  91. it('should update options array', function() {
  92. expect(scenario.variable.options.length).to.be(4);
  93. expect(scenario.variable.options[0].text).to.be('1s');
  94. expect(scenario.variable.options[0].value).to.be('1s');
  95. });
  96. });
  97. describeUpdateVariable('query variable with empty current object and refresh', function(scenario) {
  98. scenario.setup(function() {
  99. scenario.variable = { type: 'query', query: '', name: 'test', current: {} };
  100. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  101. });
  102. it('should set current value to first option', function() {
  103. expect(scenario.variable.options.length).to.be(2);
  104. expect(scenario.variable.current.value).to.be('backend1');
  105. });
  106. });
  107. describeUpdateVariable('interval variable without auto', function(scenario) {
  108. scenario.setup(function() {
  109. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };
  110. });
  111. it('should update options array', function() {
  112. expect(scenario.variable.options.length).to.be(4);
  113. expect(scenario.variable.options[0].text).to.be('1s');
  114. expect(scenario.variable.options[0].value).to.be('1s');
  115. });
  116. });
  117. describeUpdateVariable('interval variable with auto', function(scenario) {
  118. scenario.setup(function() {
  119. scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test', auto: true, auto_count: 10 };
  120. var range = {
  121. from: moment(new Date()).subtract(7, 'days').toDate(),
  122. to: new Date()
  123. };
  124. ctx.timeSrv.timeRange = sinon.stub().returns(range);
  125. ctx.templateSrv.setGrafanaVariable = sinon.spy();
  126. });
  127. it('should update options array', function() {
  128. expect(scenario.variable.options.length).to.be(5);
  129. expect(scenario.variable.options[0].text).to.be('auto');
  130. expect(scenario.variable.options[0].value).to.be('$__auto_interval');
  131. });
  132. it('should set $__auto_interval', function() {
  133. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  134. expect(call.args[0]).to.be('$__auto_interval');
  135. expect(call.args[1]).to.be('12h');
  136. });
  137. });
  138. describeUpdateVariable('update custom variable', function(scenario) {
  139. scenario.setup(function() {
  140. scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'};
  141. });
  142. it('should update options array', function() {
  143. expect(scenario.variable.options.length).to.be(3);
  144. expect(scenario.variable.options[0].text).to.be('hej');
  145. expect(scenario.variable.options[1].value).to.be('hop');
  146. });
  147. it('should set $__auto_interval', function() {
  148. var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
  149. expect(call.args[0]).to.be('$__auto_interval');
  150. expect(call.args[1]).to.be('12h');
  151. });
  152. });
  153. describeUpdateVariable('basic query variable', function(scenario) {
  154. scenario.setup(function() {
  155. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  156. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  157. });
  158. it('should update options array', function() {
  159. expect(scenario.variable.options.length).to.be(2);
  160. expect(scenario.variable.options[0].text).to.be('backend1');
  161. expect(scenario.variable.options[0].value).to.be('backend1');
  162. expect(scenario.variable.options[1].value).to.be('backend2');
  163. });
  164. it('should select first option as value', function() {
  165. expect(scenario.variable.current.value).to.be('backend1');
  166. });
  167. });
  168. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  169. scenario.setup(function() {
  170. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  171. scenario.variable.current = { value: 'backend2', text: 'backend2'};
  172. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  173. });
  174. it('should keep variable value', function() {
  175. expect(scenario.variable.current.text).to.be('backend2');
  176. });
  177. });
  178. describeUpdateVariable('and regex pattern exists', function(scenario) {
  179. scenario.setup(function() {
  180. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  181. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  182. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  183. });
  184. it('should extract and use match group', function() {
  185. expect(scenario.variable.options[0].value).to.be('backend_01');
  186. });
  187. });
  188. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  189. scenario.setup(function() {
  190. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  191. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  192. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  193. });
  194. it('should not add non matching items, None option should be added instead', function() {
  195. expect(scenario.variable.options.length).to.be(1);
  196. expect(scenario.variable.options[0].isNone).to.be(true);
  197. });
  198. });
  199. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  200. scenario.setup(function() {
  201. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  202. scenario.variable.regex = 'backend_01';
  203. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  204. });
  205. it('should return matches options', function() {
  206. expect(scenario.variable.options.length).to.be(1);
  207. });
  208. });
  209. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  210. scenario.setup(function() {
  211. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  212. scenario.variable.regex = 'backend_01';
  213. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  214. });
  215. it('should return matches options', function() {
  216. expect(scenario.variable.options.length).to.be(1);
  217. });
  218. });
  219. describeUpdateVariable('with include All', function(scenario) {
  220. scenario.setup(function() {
  221. scenario.variable = {type: 'query', query: 'apps.*', name: 'test', includeAll: true};
  222. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  223. });
  224. it('should add All option', function() {
  225. expect(scenario.variable.options[0].text).to.be('All');
  226. expect(scenario.variable.options[0].value).to.be('$__all');
  227. });
  228. });
  229. describeUpdateVariable('with include all and custom value', function(scenario) {
  230. scenario.setup(function() {
  231. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allValue: '*' };
  232. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  233. });
  234. it('should add All option with custom value', function() {
  235. expect(scenario.variable.options[0].value).to.be('$__all');
  236. });
  237. });
  238. });
  239. });