template_srv.jest.ts 13 KB

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