linkSuppliers.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { applyFieldOverrides, createTheme, DataFrameView, dateTime, FieldDisplay, toDataFrame } from '@grafana/data';
  2. import { TemplateSrv } from '../../templating/template_srv';
  3. import { getFieldLinksSupplier } from './linkSuppliers';
  4. import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from './link_srv';
  5. // We do not need more here and TimeSrv is hard to setup fully.
  6. jest.mock('app/features/dashboard/services/TimeSrv', () => ({
  7. getTimeSrv: () => ({
  8. timeRangeForUrl() {
  9. const from = dateTime().subtract(1, 'h');
  10. const to = dateTime();
  11. return { from, to, raw: { from, to } };
  12. },
  13. }),
  14. }));
  15. describe('getFieldLinksSupplier', () => {
  16. let originalLinkSrv: LinkService;
  17. let templateSrv = new TemplateSrv();
  18. beforeAll(() => {
  19. const linkService = new LinkSrv();
  20. originalLinkSrv = getLinkSrv();
  21. setLinkSrv(linkService);
  22. });
  23. afterAll(() => {
  24. setLinkSrv(originalLinkSrv);
  25. });
  26. it('links to items on the row', () => {
  27. const data = applyFieldOverrides({
  28. data: [
  29. toDataFrame({
  30. name: 'Hello Templates',
  31. refId: 'ZZZ',
  32. fields: [
  33. { name: 'Time', values: [1, 2, 3] },
  34. {
  35. name: 'Power',
  36. values: [100.2000001, 200, 300],
  37. config: {
  38. unit: 'kW',
  39. decimals: 3,
  40. displayName: 'TheTitle',
  41. },
  42. },
  43. {
  44. name: 'Last',
  45. values: ['a', 'b', 'c'],
  46. config: {
  47. links: [
  48. {
  49. title: 'By Name',
  50. url: 'http://go/${__data.fields.Power}',
  51. },
  52. {
  53. title: 'By Index',
  54. url: 'http://go/${__data.fields[1]}',
  55. },
  56. {
  57. title: 'By Title',
  58. url: 'http://go/${__data.fields[TheTitle]}',
  59. },
  60. {
  61. title: 'Numeric Value',
  62. url: 'http://go/${__data.fields.Power.numeric}',
  63. },
  64. {
  65. title: 'Text (no suffix)',
  66. url: 'http://go/${__data.fields.Power.text}',
  67. },
  68. {
  69. title: 'Unknown Field',
  70. url: 'http://go/${__data.fields.XYZ}',
  71. },
  72. {
  73. title: 'Data Frame name',
  74. url: 'http://go/${__data.name}',
  75. },
  76. {
  77. title: 'Data Frame refId',
  78. url: 'http://go/${__data.refId}',
  79. },
  80. ],
  81. },
  82. },
  83. ],
  84. }),
  85. ],
  86. fieldConfig: {
  87. defaults: {},
  88. overrides: [],
  89. },
  90. replaceVariables: (val: string) => val,
  91. timeZone: 'utc',
  92. theme: createTheme(),
  93. })[0];
  94. const rowIndex = 0;
  95. const colIndex = data.fields.length - 1;
  96. const field = data.fields[colIndex];
  97. const fieldDisp: FieldDisplay = {
  98. name: 'hello',
  99. field: field.config,
  100. view: new DataFrameView(data),
  101. rowIndex,
  102. colIndex,
  103. display: field.display!(field.values.get(rowIndex)),
  104. hasLinks: true,
  105. };
  106. const supplier = getFieldLinksSupplier(fieldDisp);
  107. const links = supplier?.getLinks(templateSrv.replace.bind(templateSrv)).map((m) => {
  108. return {
  109. title: m.title,
  110. href: m.href,
  111. };
  112. });
  113. expect(links).toMatchInlineSnapshot(`
  114. Array [
  115. Object {
  116. "href": "http://go/100.200 kW",
  117. "title": "By Name",
  118. },
  119. Object {
  120. "href": "http://go/100.200 kW",
  121. "title": "By Index",
  122. },
  123. Object {
  124. "href": "http://go/100.200 kW",
  125. "title": "By Title",
  126. },
  127. Object {
  128. "href": "http://go/100.2000001",
  129. "title": "Numeric Value",
  130. },
  131. Object {
  132. "href": "http://go/100.200",
  133. "title": "Text (no suffix)",
  134. },
  135. Object {
  136. "href": "http://go/\${__data.fields.XYZ}",
  137. "title": "Unknown Field",
  138. },
  139. Object {
  140. "href": "http://go/Hello Templates",
  141. "title": "Data Frame name",
  142. },
  143. Object {
  144. "href": "http://go/ZZZ",
  145. "title": "Data Frame refId",
  146. },
  147. ]
  148. `);
  149. });
  150. });