templateSrv-specs.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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('can check if variable exists', function() {
  40. beforeEach(function() {
  41. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  42. });
  43. it('should return true if exists', function() {
  44. var result = _templateSrv.variableExists('$test');
  45. expect(result).to.be(true);
  46. });
  47. });
  48. describe('can hightlight variables in string', function() {
  49. beforeEach(function() {
  50. _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
  51. });
  52. it('should insert html', function() {
  53. var result = _templateSrv.highlightVariablesAsHtml('$test');
  54. expect(result).to.be('<span class="template-variable">$test</span>');
  55. });
  56. it('should insert html anywhere in string', function() {
  57. var result = _templateSrv.highlightVariablesAsHtml('this $test ok');
  58. expect(result).to.be('this <span class="template-variable">$test</span> ok');
  59. });
  60. it('should ignore if variables does not exist', function() {
  61. var result = _templateSrv.highlightVariablesAsHtml('this $google ok');
  62. expect(result).to.be('this $google ok');
  63. });
  64. });
  65. describe('when checking if a string contains a variable', function() {
  66. beforeEach(function() {
  67. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  68. _templateSrv.updateTemplateData();
  69. });
  70. it('should find it with $var syntax', function() {
  71. var contains = _templateSrv.containsVariable('this.$test.filters', 'test');
  72. expect(contains).to.be(true);
  73. });
  74. it('should find it with [[var]] syntax', function() {
  75. var contains = _templateSrv.containsVariable('this.[[test]].filters', 'test');
  76. expect(contains).to.be(true);
  77. });
  78. });
  79. describe('updateTemplateData with simple value', function() {
  80. beforeEach(function() {
  81. _templateSrv.init([{ name: 'test', current: { value: 'muuuu' } }]);
  82. _templateSrv.updateTemplateData();
  83. });
  84. it('should set current value and update template data', function() {
  85. var target = _templateSrv.replace('this.[[test]].filters');
  86. expect(target).to.be('this.muuuu.filters');
  87. });
  88. });
  89. describe('replaceWithText', function() {
  90. beforeEach(function() {
  91. _templateSrv.init([
  92. { name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
  93. { name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
  94. ]);
  95. _templateSrv.setGrafanaVariable('$__auto_interval', '13m');
  96. _templateSrv.updateTemplateData();
  97. });
  98. it('should replace with text except for grafanaVariables', function() {
  99. var target = _templateSrv.replaceWithText('Server: $server, period: $period');
  100. expect(target).to.be('Server: All, period: 13m');
  101. });
  102. });
  103. });
  104. });