templateSrv-specs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. _templateSrv.updateTemplateData();
  93. });
  94. it('should find it with $var syntax', function() {
  95. var contains = _templateSrv.containsVariable('this.$test.filters', 'test');
  96. expect(contains).to.be(true);
  97. });
  98. it('should find it with [[var]] syntax', function() {
  99. var contains = _templateSrv.containsVariable('this.[[test]].filters', 'test');
  100. expect(contains).to.be(true);
  101. });
  102. });
  103. describe('updateTemplateData with simple value', function() {
  104. beforeEach(function() {
  105. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  106. _templateSrv.updateTemplateData();
  107. });
  108. it('should set current value and update template data', function() {
  109. var target = _templateSrv.replace('this.[[test]].filters');
  110. expect(target).to.be('this.muuuu.filters');
  111. });
  112. });
  113. describe('replaceWithText', function() {
  114. beforeEach(function() {
  115. _templateSrv.init([
  116. { name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  117. { name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  118. ]);
  119. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  120. _templateSrv.updateTemplateData();
  121. });
  122. it('should replace with text except for grafanaVariables', function() {
  123. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  124. expect(target).to.be('Server: All, period: 13m');
  125. });
  126. });
  127. });
  128. });