link_srv.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { LinkSrv } from '../link_srv';
  2. import { DataLinkBuiltInVars } from '@grafana/ui';
  3. import _ from 'lodash';
  4. import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
  5. import { TemplateSrv } from 'app/features/templating/template_srv';
  6. import { advanceTo } from 'jest-date-mock';
  7. jest.mock('angular', () => {
  8. const AngularJSMock = require('test/mocks/angular');
  9. return new AngularJSMock();
  10. });
  11. const dataPointMock = {
  12. seriesName: 'A-series',
  13. datapoint: [1000000001, 1],
  14. };
  15. describe('linkSrv', () => {
  16. let linkSrv: LinkSrv;
  17. function initLinkSrv() {
  18. const rootScope = {
  19. $on: jest.fn(),
  20. onAppEvent: jest.fn(),
  21. appEvent: jest.fn(),
  22. };
  23. const timer = {
  24. register: jest.fn(),
  25. cancel: jest.fn(),
  26. cancelAll: jest.fn(),
  27. };
  28. const location = {
  29. search: jest.fn(() => ({})),
  30. };
  31. const _dashboard: any = {
  32. time: { from: 'now-6h', to: 'now' },
  33. getTimezone: jest.fn(() => 'browser'),
  34. };
  35. const timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, {} as any);
  36. timeSrv.init(_dashboard);
  37. timeSrv.setTime({ from: 'now-1h', to: 'now' });
  38. _dashboard.refresh = false;
  39. const _templateSrv = new TemplateSrv();
  40. _templateSrv.init([
  41. {
  42. type: 'query',
  43. name: 'test1',
  44. current: { value: 'val1' },
  45. getValueForUrl: function() {
  46. return this.current.value;
  47. },
  48. },
  49. {
  50. type: 'query',
  51. name: 'test2',
  52. current: { value: 'val2' },
  53. getValueForUrl: function() {
  54. return this.current.value;
  55. },
  56. },
  57. ]);
  58. linkSrv = new LinkSrv(_templateSrv, timeSrv);
  59. }
  60. beforeEach(() => {
  61. initLinkSrv();
  62. advanceTo(1000000000);
  63. });
  64. describe('built in variables', () => {
  65. it('should add time range to url if $__url_time_range variable present', () => {
  66. expect(
  67. linkSrv.getDataLinkUIModel(
  68. {
  69. title: 'Any title',
  70. url: `/d/1?$${DataLinkBuiltInVars.keepTime}`,
  71. },
  72. {},
  73. {}
  74. ).href
  75. ).toEqual('/d/1?from=now-1h&to=now');
  76. });
  77. it('should add all variables to url if $__all_variables variable present', () => {
  78. expect(
  79. linkSrv.getDataLinkUIModel(
  80. {
  81. title: 'Any title',
  82. url: `/d/1?$${DataLinkBuiltInVars.includeVars}`,
  83. },
  84. {},
  85. {}
  86. ).href
  87. ).toEqual('/d/1?var-test1=val1&var-test2=val2');
  88. });
  89. it('should interpolate series name', () => {
  90. expect(
  91. linkSrv.getDataLinkUIModel(
  92. {
  93. title: 'Any title',
  94. url: `/d/1?var-test=$\{${DataLinkBuiltInVars.seriesName}}`,
  95. },
  96. {
  97. __series: {
  98. value: {
  99. name: 'A-series',
  100. },
  101. text: 'A-series',
  102. },
  103. },
  104. {}
  105. ).href
  106. ).toEqual('/d/1?var-test=A-series');
  107. });
  108. it('should interpolate value time', () => {
  109. expect(
  110. linkSrv.getDataLinkUIModel(
  111. {
  112. title: 'Any title',
  113. url: `/d/1?time=$\{${DataLinkBuiltInVars.valueTime}}`,
  114. },
  115. {
  116. __value: {
  117. value: { time: dataPointMock.datapoint[0] },
  118. text: 'Value',
  119. },
  120. },
  121. {}
  122. ).href
  123. ).toEqual('/d/1?time=1000000001');
  124. });
  125. });
  126. });