template_srv_specs.ts 8.4 KB

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