templateSrv-specs.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. define([
  2. 'mocks/dashboard-mock',
  3. 'lodash',
  4. 'services/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. });
  77. });