PromLink.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { PanelData } from '@grafana/data';
  4. import { PrometheusDatasource } from '../datasource';
  5. import { PromQuery } from '../types';
  6. import PromLink from './PromLink';
  7. jest.mock('@grafana/data', () => ({
  8. ...(jest.requireActual('@grafana/data') as any),
  9. rangeUtil: {
  10. intervalToSeconds: jest.fn(() => 15),
  11. },
  12. }));
  13. const getPanelData = (panelDataOverrides?: Partial<PanelData>) => {
  14. const panelData = {
  15. request: {
  16. scopedVars: [{ __interval: { text: '15s', value: '15s' } }],
  17. targets: [
  18. { refId: 'A', datasource: 'prom1' },
  19. { refId: 'B', datasource: 'prom2' },
  20. ],
  21. range: {
  22. to: {
  23. utc: () => ({
  24. format: jest.fn(),
  25. }),
  26. },
  27. },
  28. },
  29. };
  30. return Object.assign(panelData, panelDataOverrides) as PanelData;
  31. };
  32. const getDataSource = (datasourceOverrides?: Partial<PrometheusDatasource>) => {
  33. const datasource = {
  34. getPrometheusTime: () => 123,
  35. createQuery: () => ({ expr: 'up', step: 15 }),
  36. directUrl: 'prom1',
  37. getRateIntervalScopedVariable: jest.fn(() => ({ __rate_interval: { text: '60s', value: '60s' } })),
  38. };
  39. return Object.assign(datasource, datasourceOverrides) as unknown as PrometheusDatasource;
  40. };
  41. const getDataSourceWithCustomQueryParameters = (datasourceOverrides?: Partial<PrometheusDatasource>) => {
  42. const datasource = {
  43. getPrometheusTime: () => 124,
  44. createQuery: () => ({ expr: 'up', step: 20 }),
  45. directUrl: 'prom3',
  46. getRateIntervalScopedVariable: jest.fn(() => ({ __rate_interval: { text: '60s', value: '60s' } })),
  47. customQueryParameters: new URLSearchParams('g0.foo=1'),
  48. };
  49. return Object.assign(datasource, datasourceOverrides) as unknown as PrometheusDatasource;
  50. };
  51. describe('PromLink', () => {
  52. it('should show correct link for 1 component', async () => {
  53. render(
  54. <div>
  55. <PromLink datasource={getDataSource()} panelData={getPanelData()} query={{} as PromQuery} />
  56. </div>
  57. );
  58. expect(screen.getByText('Prometheus')).toHaveAttribute(
  59. 'href',
  60. 'prom1/graph?g0.expr=up&g0.range_input=0s&g0.end_input=undefined&g0.step_input=15&g0.tab=0'
  61. );
  62. });
  63. it('should show different link when there are 2 components with the same panel data', () => {
  64. render(
  65. <div>
  66. <PromLink datasource={getDataSource()} panelData={getPanelData()} query={{} as PromQuery} />
  67. <PromLink datasource={getDataSource({ directUrl: 'prom2' })} panelData={getPanelData()} query={{} as any} />
  68. </div>
  69. );
  70. const promLinkButtons = screen.getAllByText('Prometheus');
  71. expect(promLinkButtons[0]).toHaveAttribute(
  72. 'href',
  73. 'prom1/graph?g0.expr=up&g0.range_input=0s&g0.end_input=undefined&g0.step_input=15&g0.tab=0'
  74. );
  75. expect(promLinkButtons[1]).toHaveAttribute(
  76. 'href',
  77. 'prom2/graph?g0.expr=up&g0.range_input=0s&g0.end_input=undefined&g0.step_input=15&g0.tab=0'
  78. );
  79. });
  80. it('should create sanitized link', async () => {
  81. render(
  82. <div>
  83. <PromLink
  84. datasource={getDataSource({ directUrl: "javascript:300?1:2;alert('Hello');//" })}
  85. panelData={getPanelData()}
  86. query={{} as PromQuery}
  87. />
  88. </div>
  89. );
  90. expect(screen.getByText('Prometheus')).toHaveAttribute('href', 'about:blank');
  91. });
  92. it('should add custom query parameters when it is configured', async () => {
  93. render(
  94. <div>
  95. <PromLink
  96. datasource={getDataSourceWithCustomQueryParameters()}
  97. panelData={getPanelData()}
  98. query={{} as PromQuery}
  99. />
  100. </div>
  101. );
  102. expect(screen.getByText('Prometheus')).toHaveAttribute(
  103. 'href',
  104. 'prom3/graph?g0.foo=1&g0.expr=up&g0.range_input=0s&g0.end_input=undefined&g0.step_input=20&g0.tab=0'
  105. );
  106. });
  107. });