template_srv.jest.ts 9.8 KB

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