template_srv_specs.ts 8.9 KB

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