templateSrv-specs.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. define([
  2. '../mocks/dashboard-mock',
  3. 'lodash',
  4. 'app/features/templating/templateSrv'
  5. ], function(dashboardMock) {
  6. 'use strict';
  7. describe('templateSrv', function() {
  8. var _templateSrv;
  9. var _dashboard;
  10. beforeEach(module('grafana.services'));
  11. beforeEach(module(function() {
  12. _dashboard = dashboardMock.create();
  13. }));
  14. beforeEach(inject(function(templateSrv) {
  15. _templateSrv = templateSrv;
  16. }));
  17. describe('init', function() {
  18. beforeEach(function() {
  19. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  20. });
  21. it('should initialize template data', function() {
  22. var target = _templateSrv.replace('this.[[test]].filters');
  23. expect(target).to.be('this.oogle.filters');
  24. });
  25. });
  26. describe('replace can pass scoped vars', function() {
  27. beforeEach(function() {
  28. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  29. });
  30. it('should replace $test with scoped value', function() {
  31. var target = _templateSrv.replace('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  32. expect(target).to.be('this.mupp.filters');
  33. });
  34. it('should replace $test with scoped text', function() {
  35. var target = _templateSrv.replaceWithText('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  36. expect(target).to.be('this.asd.filters');
  37. });
  38. });
  39. describe('replace can pass multi / all format', function() {
  40. beforeEach(function() {
  41. _templateSrv.init([{name: 'test', current: {value: ['value1', 'value2'] }}]);
  42. });
  43. it('should replace $test with globbed value', function() {
  44. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  45. expect(target).to.be('this.{value1,value2}.filters');
  46. });
  47. it('should replace $test with piped value', function() {
  48. var target = _templateSrv.replace('this=$test', {}, 'pipe');
  49. expect(target).to.be('this=value1|value2');
  50. });
  51. it('should replace $test with piped value', function() {
  52. var target = _templateSrv.replace('this=$test', {}, 'pipe');
  53. expect(target).to.be('this=value1|value2');
  54. });
  55. });
  56. describe('variable with all option', function() {
  57. beforeEach(function() {
  58. _templateSrv.init([{
  59. name: 'test',
  60. current: {value: '$__all' },
  61. options: [
  62. {value: '$__all'}, {value: 'value1'}, {value: 'value2'}
  63. ]
  64. }]);
  65. });
  66. it('should replace $test with formatted all value', function() {
  67. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  68. expect(target).to.be('this.{value1,value2}.filters');
  69. });
  70. });
  71. describe('variable with all option and custom value', function() {
  72. beforeEach(function() {
  73. _templateSrv.init([{
  74. name: 'test',
  75. current: {value: '$__all' },
  76. allValue: '*',
  77. options: [
  78. {value: 'value1'}, {value: 'value2'}
  79. ]
  80. }]);
  81. });
  82. it('should replace $test with formatted all value', function() {
  83. var target = _templateSrv.replace('this.$test.filters', {}, 'glob');
  84. expect(target).to.be('this.*.filters');
  85. });
  86. it('should not escape custom all value', function() {
  87. var target = _templateSrv.replace('this.$test', {}, 'regex');
  88. expect(target).to.be('this.*');
  89. });
  90. });
  91. describe('lucene format', function() {
  92. it('should properly escape $test with lucene escape sequences', function() {
  93. _templateSrv.init([{name: 'test', current: {value: 'value/4' }}]);
  94. var target = _templateSrv.replace('this:$test', {}, 'lucene');
  95. expect(target).to.be("this:value\\\/4");
  96. });
  97. });
  98. describe('format variable to string values', function() {
  99. it('single value should return value', function() {
  100. var result = _templateSrv.formatValue('test');
  101. expect(result).to.be('test');
  102. });
  103. it('multi value and glob format should render glob string', function() {
  104. var result = _templateSrv.formatValue(['test','test2'], 'glob');
  105. expect(result).to.be('{test,test2}');
  106. });
  107. it('multi value and lucene should render as lucene expr', function() {
  108. var result = _templateSrv.formatValue(['test','test2'], 'lucene');
  109. expect(result).to.be('("test" OR "test2")');
  110. });
  111. it('multi value and regex format should render regex string', function() {
  112. var result = _templateSrv.formatValue(['test.','test2'], 'regex');
  113. expect(result).to.be('(test\\.|test2)');
  114. });
  115. it('multi value and pipe should render pipe string', function() {
  116. var result = _templateSrv.formatValue(['test','test2'], 'pipe');
  117. expect(result).to.be('test|test2');
  118. });
  119. it('slash should be properly escaped in regex format', function() {
  120. var result = _templateSrv.formatValue('Gi3/14', 'regex');
  121. expect(result).to.be('Gi3\\/14');
  122. });
  123. });
  124. describe('can check if variable exists', function() {
  125. beforeEach(function() {
  126. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  127. });
  128. it('should return true if exists', function() {
  129. var result = _templateSrv.variableExists('$test');
  130. expect(result).to.be(true);
  131. });
  132. });
  133. describe('can hightlight variables in string', function() {
  134. beforeEach(function() {
  135. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  136. });
  137. it('should insert html', function() {
  138. var result = _templateSrv.highlightVariablesAsHtml('$test');
  139. expect(result).to.be('<span class="template-variable">$test</span>');
  140. });
  141. it('should insert html anywhere in string', function() {
  142. var result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  143. expect(result).to.be('this <span class="template-variable">$test</span> ok');
  144. });
  145. it('should ignore if variables does not exist', function() {
  146. var result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  147. expect(result).to.be('this $google ok');
  148. });
  149. });
  150. describe('when checking if a string contains a variable', function() {
  151. beforeEach(function() {
  152. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  153. });
  154. it('should find it with $var syntax', function() {
  155. var contains = _templateSrv.containsVariable('this.$test.filters', 'test');
  156. expect(contains).to.be(true);
  157. });
  158. it('should not find it if only part matches with $var syntax', function() {
  159. var contains = _templateSrv.containsVariable('this.$ServerDomain.filters', 'Server');
  160. expect(contains).to.be(false);
  161. });
  162. it('should find it with [[var]] syntax', function() {
  163. var contains = _templateSrv.containsVariable('this.[[test]].filters', 'test');
  164. expect(contains).to.be(true);
  165. });
  166. it('should find it when part of segment', function() {
  167. var contains = _templateSrv.containsVariable('metrics.$env.$group-*', 'group');
  168. expect(contains).to.be(true);
  169. });
  170. it('should find it its the only thing', function() {
  171. var contains = _templateSrv.containsVariable('$env', 'env');
  172. expect(contains).to.be(true);
  173. });
  174. });
  175. describe('updateTemplateData with simple value', function() {
  176. beforeEach(function() {
  177. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  178. });
  179. it('should set current value and update template data', function() {
  180. var target = _templateSrv.replace('this.[[test]].filters');
  181. expect(target).to.be('this.muuuu.filters');
  182. });
  183. });
  184. describe('fillVariableValuesForUrl with multi value', function() {
  185. beforeEach(function() {
  186. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  187. });
  188. it('should set multiple url params', function() {
  189. var params = {};
  190. _templateSrv.fillVariableValuesForUrl(params);
  191. expect(params['var-test']).to.eql(['val1', 'val2']);
  192. });
  193. });
  194. describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
  195. beforeEach(function() {
  196. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  197. });
  198. it('should set multiple url params', function() {
  199. var params = {};
  200. _templateSrv.fillVariableValuesForUrl(params, {'test': {value: 'val1'}});
  201. expect(params['var-test']).to.eql('val1');
  202. });
  203. });
  204. describe('replaceWithText', function() {
  205. beforeEach(function() {
  206. _templateSrv.init([
  207. { name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  208. { name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  209. ]);
  210. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  211. _templateSrv.updateTemplateData();
  212. });
  213. it('should replace with text except for grafanaVariables', function() {
  214. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  215. expect(target).to.be('Server: All, period: 13m');
  216. });
  217. });
  218. });
  219. });