template_srv.jest.ts 9.4 KB

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