templateSrv-specs.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. define([
  2. '../mocks/dashboard-mock',
  3. 'lodash',
  4. 'app/features/templating/templateSrv'
  5. ], function(dashboardMock) {
  6. 'use strict';
  7. describe('templateSrv', function() {
  8. var _templateSrv;
  9. var _dashboard;
  10. beforeEach(module('grafana.services'));
  11. beforeEach(module(function() {
  12. _dashboard = dashboardMock.create();
  13. }));
  14. beforeEach(inject(function(templateSrv) {
  15. _templateSrv = templateSrv;
  16. }));
  17. describe('init', function() {
  18. beforeEach(function() {
  19. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  20. });
  21. it('should initialize template data', function() {
  22. var target = _templateSrv.replace('this.[[test]].filters');
  23. expect(target).to.be('this.oogle.filters');
  24. });
  25. });
  26. describe('replace can pass scoped vars', function() {
  27. beforeEach(function() {
  28. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  29. });
  30. it('should replace $test with scoped value', function() {
  31. var target = _templateSrv.replace('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  32. expect(target).to.be('this.mupp.filters');
  33. });
  34. it('should replace $test with scoped text', function() {
  35. var target = _templateSrv.replaceWithText('this.$test.filters', {'test': {value: 'mupp', text: 'asd'}});
  36. expect(target).to.be('this.asd.filters');
  37. });
  38. });
  39. describe('render variable to string values', function() {
  40. it('single value should return value', function() {
  41. var result = _templateSrv.renderVariableValue({current: {value: 'test'}});
  42. expect(result).to.be('test');
  43. });
  44. it('multi value and glob format should render glob string', function() {
  45. var result = _templateSrv.renderVariableValue({
  46. multiFormat: 'glob',
  47. current: {
  48. value: ['test','test2'],
  49. }
  50. });
  51. expect(result).to.be('{test,test2}');
  52. });
  53. it('multi value and lucene should render as lucene expr', function() {
  54. var result = _templateSrv.renderVariableValue({
  55. multiFormat: 'lucene',
  56. current: {
  57. value: ['test','test2'],
  58. }
  59. });
  60. expect(result).to.be('(\\\"test\\\" OR \\\"test2\\\")');
  61. });
  62. it('multi value and regex format should render regex string', function() {
  63. var result = _templateSrv.renderVariableValue({
  64. multiFormat: 'regex values',
  65. current: {
  66. value: ['test','test2'],
  67. }
  68. });
  69. expect(result).to.be('(test|test2)');
  70. });
  71. it('multi value and pipe should render pipe string', function() {
  72. var result = _templateSrv.renderVariableValue({
  73. multiFormat: 'pipe',
  74. current: {
  75. value: ['test','test2'],
  76. }
  77. });
  78. expect(result).to.be('test|test2');
  79. });
  80. });
  81. describe('can check if variable exists', function() {
  82. beforeEach(function() {
  83. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  84. });
  85. it('should return true if exists', function() {
  86. var result = _templateSrv.variableExists('$test');
  87. expect(result).to.be(true);
  88. });
  89. });
  90. describe('can hightlight variables in string', function() {
  91. beforeEach(function() {
  92. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  93. });
  94. it('should insert html', function() {
  95. var result = _templateSrv.highlightVariablesAsHtml('$test');
  96. expect(result).to.be('<span class="template-variable">$test</span>');
  97. });
  98. it('should insert html anywhere in string', function() {
  99. var result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  100. expect(result).to.be('this <span class="template-variable">$test</span> ok');
  101. });
  102. it('should ignore if variables does not exist', function() {
  103. var result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  104. expect(result).to.be('this $google ok');
  105. });
  106. });
  107. describe('when checking if a string contains a variable', function() {
  108. beforeEach(function() {
  109. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  110. });
  111. it('should find it with $var syntax', function() {
  112. var contains = _templateSrv.containsVariable('this.$test.filters', 'test');
  113. expect(contains).to.be(true);
  114. });
  115. it('should find it with [[var]] syntax', function() {
  116. var contains = _templateSrv.containsVariable('this.[[test]].filters', 'test');
  117. expect(contains).to.be(true);
  118. });
  119. });
  120. describe('updateTemplateData with simple value', function() {
  121. beforeEach(function() {
  122. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  123. });
  124. it('should set current value and update template data', function() {
  125. var target = _templateSrv.replace('this.[[test]].filters');
  126. expect(target).to.be('this.muuuu.filters');
  127. });
  128. });
  129. describe('fillVariableValuesForUrl with multi value', function() {
  130. beforeEach(function() {
  131. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  132. });
  133. it('should set multiple url params', function() {
  134. var params = {};
  135. _templateSrv.fillVariableValuesForUrl(params);
  136. expect(params['var-test']).to.eql(['val1', 'val2']);
  137. });
  138. });
  139. describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
  140. beforeEach(function() {
  141. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  142. });
  143. it('should set multiple url params', function() {
  144. var params = {};
  145. _templateSrv.fillVariableValuesForUrl(params, {'test': {value: 'val1'}});
  146. expect(params['var-test']).to.eql('val1');
  147. });
  148. });
  149. describe('replaceWithText', function() {
  150. beforeEach(function() {
  151. _templateSrv.init([
  152. { name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  153. { name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  154. ]);
  155. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  156. _templateSrv.updateTemplateData();
  157. });
  158. it('should replace with text except for grafanaVariables', function() {
  159. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  160. expect(target).to.be('Server: All, period: 13m');
  161. });
  162. });
  163. });
  164. });