DebugSection.test.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { mount } from 'enzyme';
  2. import React from 'react';
  3. import { dateTime, TimeRange } from '@grafana/data';
  4. import { setTemplateSrv } from '@grafana/runtime';
  5. import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from '../../../../features/panel/panellinks/link_srv';
  6. import { DebugSection } from './DebugSection';
  7. // We do not need more here and TimeSrv is hard to setup fully.
  8. jest.mock('app/features/dashboard/services/TimeSrv', () => ({
  9. getTimeSrv: () => ({
  10. timeRangeForUrl() {
  11. const from = dateTime().subtract(1, 'h');
  12. const to = dateTime();
  13. return { from, to, raw: { from, to } };
  14. },
  15. }),
  16. }));
  17. describe('DebugSection', () => {
  18. let originalLinkSrv: LinkService;
  19. // This needs to be setup so we can test interpolation in the debugger
  20. beforeAll(() => {
  21. const linkService = new LinkSrv();
  22. originalLinkSrv = getLinkSrv();
  23. setLinkSrv(linkService);
  24. });
  25. beforeEach(() => {
  26. setTemplateSrv({
  27. replace(target, scopedVars, format) {
  28. return target ?? '';
  29. },
  30. getVariables() {
  31. return [];
  32. },
  33. containsTemplate() {
  34. return false;
  35. },
  36. updateTimeRange(timeRange: TimeRange) {},
  37. });
  38. });
  39. afterAll(() => {
  40. setLinkSrv(originalLinkSrv);
  41. });
  42. it('does not render any field if no debug text', () => {
  43. const wrapper = mount(<DebugSection derivedFields={[]} />);
  44. expect(wrapper.find('DebugFieldItem').length).toBe(0);
  45. });
  46. it('does not render any field if no derived fields', () => {
  47. const wrapper = mount(<DebugSection derivedFields={[]} />);
  48. const textarea = wrapper.find('textarea');
  49. (textarea.getDOMNode() as HTMLTextAreaElement).value = 'traceId=1234';
  50. textarea.simulate('change');
  51. expect(wrapper.find('DebugFieldItem').length).toBe(0);
  52. });
  53. it('renders derived fields', () => {
  54. const derivedFields = [
  55. {
  56. matcherRegex: 'traceId=(\\w+)',
  57. name: 'traceIdLink',
  58. url: 'http://localhost/trace/${__value.raw}',
  59. },
  60. {
  61. matcherRegex: 'traceId=(\\w+)',
  62. name: 'traceId',
  63. },
  64. {
  65. matcherRegex: 'traceId=(',
  66. name: 'error',
  67. },
  68. ];
  69. const wrapper = mount(<DebugSection derivedFields={derivedFields} />);
  70. const textarea = wrapper.find('textarea');
  71. (textarea.getDOMNode() as HTMLTextAreaElement).value = 'traceId=1234';
  72. textarea.simulate('change');
  73. expect(wrapper.find('table').length).toBe(1);
  74. // 3 rows + one header
  75. expect(wrapper.find('tr').length).toBe(4);
  76. expect(wrapper.find('tr').at(1).contains('http://localhost/trace/${__value.raw}')).toBeTruthy();
  77. });
  78. });