template_srv.jest.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { TemplateSrv } from "../template_srv";
  2. describe("templateSrv", function() {
  3. var _templateSrv;
  4. function initTemplateSrv(variables) {
  5. _templateSrv = new TemplateSrv();
  6. _templateSrv.init(variables);
  7. }
  8. describe("init", function() {
  9. beforeEach(function() {
  10. initTemplateSrv([
  11. { type: "query", name: "test", current: { value: "oogle" } }
  12. ]);
  13. });
  14. it("should initialize template data", function() {
  15. var target = _templateSrv.replace("this.[[test]].filters");
  16. expect(target).toBe("this.oogle.filters");
  17. });
  18. });
  19. describe("replace can pass scoped vars", function() {
  20. beforeEach(function() {
  21. initTemplateSrv([
  22. { type: "query", name: "test", current: { value: "oogle" } }
  23. ]);
  24. });
  25. it("should replace $test with scoped value", function() {
  26. var target = _templateSrv.replace("this.$test.filters", {
  27. test: { value: "mupp", text: "asd" }
  28. });
  29. expect(target).toBe("this.mupp.filters");
  30. });
  31. it("should replace $test with scoped text", function() {
  32. var target = _templateSrv.replaceWithText("this.$test.filters", {
  33. test: { value: "mupp", text: "asd" }
  34. });
  35. expect(target).toBe("this.asd.filters");
  36. });
  37. });
  38. describe("getAdhocFilters", function() {
  39. beforeEach(function() {
  40. initTemplateSrv([
  41. {
  42. type: "datasource",
  43. name: "ds",
  44. current: { value: "logstash", text: "logstash" }
  45. },
  46. { type: "adhoc", name: "test", datasource: "oogle", filters: [1] },
  47. { type: "adhoc", name: "test2", datasource: "$ds", filters: [2] }
  48. ]);
  49. });
  50. it("should return filters if datasourceName match", function() {
  51. var filters = _templateSrv.getAdhocFilters("oogle");
  52. expect(filters).toMatchObject([1]);
  53. });
  54. it("should return empty array if datasourceName does not match", function() {
  55. var filters = _templateSrv.getAdhocFilters("oogleasdasd");
  56. expect(filters).toMatchObject([]);
  57. });
  58. it("should return filters when datasourceName match via data source variable", function() {
  59. var filters = _templateSrv.getAdhocFilters("logstash");
  60. expect(filters).toMatchObject([2]);
  61. });
  62. });
  63. describe("replace can pass multi / all format", function() {
  64. beforeEach(function() {
  65. initTemplateSrv([
  66. {
  67. type: "query",
  68. name: "test",
  69. current: { value: ["value1", "value2"] }
  70. }
  71. ]);
  72. });
  73. it("should replace $test with globbed value", function() {
  74. var target = _templateSrv.replace("this.$test.filters", {}, "glob");
  75. expect(target).toBe("this.{value1,value2}.filters");
  76. });
  77. it("should replace $test with piped value", function() {
  78. var target = _templateSrv.replace("this=$test", {}, "pipe");
  79. expect(target).toBe("this=value1|value2");
  80. });
  81. it("should replace $test with piped value", function() {
  82. var target = _templateSrv.replace("this=$test", {}, "pipe");
  83. expect(target).toBe("this=value1|value2");
  84. });
  85. });
  86. describe("variable with all option", function() {
  87. beforeEach(function() {
  88. initTemplateSrv([
  89. {
  90. type: "query",
  91. name: "test",
  92. current: { value: "$__all" },
  93. options: [
  94. { value: "$__all" },
  95. { value: "value1" },
  96. { value: "value2" }
  97. ]
  98. }
  99. ]);
  100. });
  101. it("should replace $test with formatted all value", function() {
  102. var target = _templateSrv.replace("this.$test.filters", {}, "glob");
  103. expect(target).toBe("this.{value1,value2}.filters");
  104. });
  105. });
  106. describe("variable with all option and custom value", function() {
  107. beforeEach(function() {
  108. initTemplateSrv([
  109. {
  110. type: "query",
  111. name: "test",
  112. current: { value: "$__all" },
  113. allValue: "*",
  114. options: [{ value: "value1" }, { value: "value2" }]
  115. }
  116. ]);
  117. });
  118. it("should replace $test with formatted all value", function() {
  119. var target = _templateSrv.replace("this.$test.filters", {}, "glob");
  120. expect(target).toBe("this.*.filters");
  121. });
  122. it("should not escape custom all value", function() {
  123. var target = _templateSrv.replace("this.$test", {}, "regex");
  124. expect(target).toBe("this.*");
  125. });
  126. });
  127. describe("lucene format", function() {
  128. it("should properly escape $test with lucene escape sequences", function() {
  129. initTemplateSrv([
  130. { type: "query", name: "test", current: { value: "value/4" } }
  131. ]);
  132. var target = _templateSrv.replace("this:$test", {}, "lucene");
  133. expect(target).toBe("this:value\\/4");
  134. });
  135. });
  136. describe("format variable to string values", function() {
  137. it("single value should return value", function() {
  138. var result = _templateSrv.formatValue("test");
  139. expect(result).toBe("test");
  140. });
  141. it("multi value and glob format should render glob string", function() {
  142. var result = _templateSrv.formatValue(["test", "test2"], "glob");
  143. expect(result).toBe("{test,test2}");
  144. });
  145. it("multi value and lucene should render as lucene expr", function() {
  146. var result = _templateSrv.formatValue(["test", "test2"], "lucene");
  147. expect(result).toBe('("test" OR "test2")');
  148. });
  149. it("multi value and regex format should render regex string", function() {
  150. var result = _templateSrv.formatValue(["test.", "test2"], "regex");
  151. expect(result).toBe("(test\\.|test2)");
  152. });
  153. it("multi value and pipe should render pipe string", function() {
  154. var result = _templateSrv.formatValue(["test", "test2"], "pipe");
  155. expect(result).toBe("test|test2");
  156. });
  157. it("multi value and distributed should render distributed string", function() {
  158. var result = _templateSrv.formatValue(["test", "test2"], "distributed", {
  159. name: "build"
  160. });
  161. expect(result).toBe("test,build=test2");
  162. });
  163. it("multi value and distributed should render when not string", function() {
  164. var result = _templateSrv.formatValue(["test"], "distributed", {
  165. name: "build"
  166. });
  167. expect(result).toBe("test");
  168. });
  169. it("slash should be properly escaped in regex format", function() {
  170. var result = _templateSrv.formatValue("Gi3/14", "regex");
  171. expect(result).toBe("Gi3\\/14");
  172. });
  173. });
  174. describe("can check if variable exists", function() {
  175. beforeEach(function() {
  176. initTemplateSrv([
  177. { type: "query", name: "test", current: { value: "oogle" } }
  178. ]);
  179. });
  180. it("should return true if exists", function() {
  181. var result = _templateSrv.variableExists("$test");
  182. expect(result).toBe(true);
  183. });
  184. });
  185. describe("can hightlight variables in string", function() {
  186. beforeEach(function() {
  187. initTemplateSrv([
  188. { type: "query", name: "test", current: { value: "oogle" } }
  189. ]);
  190. });
  191. it("should insert html", function() {
  192. var result = _templateSrv.highlightVariablesAsHtml("$test");
  193. expect(result).toBe('<span class="template-variable">$test</span>');
  194. });
  195. it("should insert html anywhere in string", function() {
  196. var result = _templateSrv.highlightVariablesAsHtml("this $test ok");
  197. expect(result).toBe(
  198. 'this <span class="template-variable">$test</span> ok'
  199. );
  200. });
  201. it("should ignore if variables does not exist", function() {
  202. var result = _templateSrv.highlightVariablesAsHtml("this $google ok");
  203. expect(result).toBe("this $google ok");
  204. });
  205. });
  206. describe("updateTemplateData with simple value", function() {
  207. beforeEach(function() {
  208. initTemplateSrv([
  209. { type: "query", name: "test", current: { value: "muuuu" } }
  210. ]);
  211. });
  212. it("should set current value and update template data", function() {
  213. var target = _templateSrv.replace("this.[[test]].filters");
  214. expect(target).toBe("this.muuuu.filters");
  215. });
  216. });
  217. describe("fillVariableValuesForUrl with multi value", function() {
  218. beforeEach(function() {
  219. initTemplateSrv([
  220. {
  221. type: "query",
  222. name: "test",
  223. current: { value: ["val1", "val2"] },
  224. getValueForUrl: function() {
  225. return this.current.value;
  226. }
  227. }
  228. ]);
  229. });
  230. it("should set multiple url params", function() {
  231. var params = {};
  232. _templateSrv.fillVariableValuesForUrl(params);
  233. expect(params["var-test"]).toMatchObject(["val1", "val2"]);
  234. });
  235. });
  236. describe("fillVariableValuesForUrl with multi value and scopedVars", function() {
  237. beforeEach(function() {
  238. initTemplateSrv([
  239. { type: "query", name: "test", current: { value: ["val1", "val2"] } }
  240. ]);
  241. });
  242. it("should set scoped value as url params", function() {
  243. var params = {};
  244. _templateSrv.fillVariableValuesForUrl(params, {
  245. test: { value: "val1" }
  246. });
  247. expect(params["var-test"]).toBe("val1");
  248. });
  249. });
  250. describe("replaceWithText", function() {
  251. beforeEach(function() {
  252. initTemplateSrv([
  253. {
  254. type: "query",
  255. name: "server",
  256. current: { value: "{asd,asd2}", text: "All" }
  257. },
  258. {
  259. type: "interval",
  260. name: "period",
  261. current: { value: "$__auto_interval_interval", text: "auto" }
  262. }
  263. ]);
  264. _templateSrv.setGrafanaVariable("$__auto_interval_interval", "13m");
  265. _templateSrv.updateTemplateData();
  266. });
  267. it("should replace with text except for grafanaVariables", function() {
  268. var target = _templateSrv.replaceWithText(
  269. "Server: $server, period: $period"
  270. );
  271. expect(target).toBe("Server: All, period: 13m");
  272. });
  273. });
  274. describe("built in interval variables", function() {
  275. beforeEach(function() {
  276. initTemplateSrv([]);
  277. });
  278. it("should replace $__interval_ms with interval milliseconds", function() {
  279. var target = _templateSrv.replace("10 * $__interval_ms", {
  280. __interval_ms: { text: "100", value: "100" }
  281. });
  282. expect(target).toBe("10 * 100");
  283. });
  284. });
  285. });