templateSrv-specs.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. define([
  2. 'mocks/dashboard-mock',
  3. 'lodash',
  4. '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 regex format should render regex string', function() {
  54. var result = _templateSrv.renderVariableValue({
  55. multiFormat: 'regex values',
  56. current: {
  57. value: ['test','test2'],
  58. }
  59. });
  60. expect(result).to.be('(test|test2)');
  61. });
  62. });
  63. describe('can check if variable exists', function() {
  64. beforeEach(function() {
  65. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  66. });
  67. it('should return true if exists', function() {
  68. var result = _templateSrv.variableExists('$test');
  69. expect(result).to.be(true);
  70. });
  71. });
  72. describe('can hightlight variables in string', function() {
  73. beforeEach(function() {
  74. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  75. });
  76. it('should insert html', function() {
  77. var result = _templateSrv.highlightVariablesAsHtml('$test');
  78. expect(result).to.be('<span class="template-variable">$test</span>');
  79. });
  80. it('should insert html anywhere in string', function() {
  81. var result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  82. expect(result).to.be('this <span class="template-variable">$test</span> ok');
  83. });
  84. it('should ignore if variables does not exist', function() {
  85. var result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  86. expect(result).to.be('this $google ok');
  87. });
  88. });
  89. describe('when checking if a string contains a variable', function() {
  90. beforeEach(function() {
  91. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  92. });
  93. it('should find it with $var syntax', function() {
  94. var contains = _templateSrv.containsVariable('this.$test.filters', 'test');
  95. expect(contains).to.be(true);
  96. });
  97. it('should find it with [[var]] syntax', function() {
  98. var contains = _templateSrv.containsVariable('this.[[test]].filters', 'test');
  99. expect(contains).to.be(true);
  100. });
  101. });
  102. describe('updateTemplateData with simple value', function() {
  103. beforeEach(function() {
  104. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  105. });
  106. it('should set current value and update template data', function() {
  107. var target = _templateSrv.replace('this.[[test]].filters');
  108. expect(target).to.be('this.muuuu.filters');
  109. });
  110. });
  111. describe('fillVariableValuesForUrl with multi value', function() {
  112. beforeEach(function() {
  113. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  114. });
  115. it('should set multiple url params', function() {
  116. var params = {};
  117. _templateSrv.fillVariableValuesForUrl(params);
  118. expect(params['var-test']).to.eql(['val1', 'val2']);
  119. });
  120. });
  121. describe('fillVariableValuesForUrl with multi value and scopedVars', function() {
  122. beforeEach(function() {
  123. _templateSrv.init([{ name: 'test', current: { value: ['val1', 'val2'] }}]);
  124. });
  125. it('should set multiple url params', function() {
  126. var params = {};
  127. _templateSrv.fillVariableValuesForUrl(params, {'test': {value: 'val1'}});
  128. expect(params['var-test']).to.eql('val1');
  129. });
  130. });
  131. describe('replaceWithText', function() {
  132. beforeEach(function() {
  133. _templateSrv.init([
  134. { name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  135. { name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  136. ]);
  137. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  138. _templateSrv.updateTemplateData();
  139. });
  140. it('should replace with text except for grafanaVariables', function() {
  141. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  142. expect(target).to.be('Server: All, period: 13m');
  143. });
  144. });
  145. });
  146. });