templateSrv-specs.js 3.6 KB

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