autofixRootCause.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {AutofixRootCauseData} from 'sentry-fixture/autofixRootCauseData';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {AutofixRootCause} from 'sentry/components/events/autofix/autofixRootCause';
  4. describe('AutofixRootCause', function () {
  5. const defaultProps = {
  6. causes: [AutofixRootCauseData()],
  7. groupId: '1',
  8. rootCauseSelection: null,
  9. runId: '101',
  10. repos: [],
  11. };
  12. it('can view a relevant code snippet', function () {
  13. render(<AutofixRootCause {...defaultProps} />);
  14. // Displays all root cause and code context info
  15. expect(screen.getByText('Potential Root Cause')).toBeInTheDocument();
  16. expect(screen.getByText('This is the title of a root cause.')).toBeInTheDocument();
  17. expect(
  18. screen.getByText('This is the description of a root cause.')
  19. ).toBeInTheDocument();
  20. expect(
  21. screen.getByText('Snippet #1: This is the title of a relevant code snippet.')
  22. ).toBeInTheDocument();
  23. expect(
  24. screen.getByText('This is the description of a relevant code snippet.')
  25. ).toBeInTheDocument();
  26. });
  27. it('shows graceful error state when there are no causes', function () {
  28. render(
  29. <AutofixRootCause
  30. {...{
  31. ...defaultProps,
  32. causes: [],
  33. terminationReason: 'The error comes from outside the codebase.',
  34. }}
  35. />
  36. );
  37. // Displays all root cause and code context info
  38. expect(
  39. screen.getByText('No root cause found. The error comes from outside the codebase.')
  40. ).toBeInTheDocument();
  41. });
  42. it('shows hyperlink when matching GitHub repo available', function () {
  43. render(
  44. <AutofixRootCause
  45. {...{
  46. ...defaultProps,
  47. repos: [
  48. {
  49. default_branch: 'main',
  50. external_id: 'id',
  51. name: 'owner/repo',
  52. provider: 'integrations:github',
  53. url: 'https://github.com/test_owner/test_repo',
  54. },
  55. ],
  56. }}
  57. />
  58. );
  59. expect(screen.getByRole('link', {name: 'GitHub'})).toBeInTheDocument();
  60. expect(screen.queryByRole('link', {name: 'GitHub'})).toHaveAttribute(
  61. 'href',
  62. 'https://github.com/test_owner/test_repo/blob/main/src/file.py'
  63. );
  64. });
  65. it('shows no hyperlink when no matching GitHub repo available', function () {
  66. render(
  67. <AutofixRootCause
  68. {...{
  69. ...defaultProps,
  70. repos: [],
  71. }}
  72. />
  73. );
  74. expect(screen.queryByRole('link', {name: 'GitHub'})).not.toBeInTheDocument();
  75. });
  76. it('shows reproduction steps when applicable', async function () {
  77. render(
  78. <AutofixRootCause
  79. {...{
  80. ...defaultProps,
  81. causes: [AutofixRootCauseData()],
  82. }}
  83. />
  84. );
  85. await userEvent.click(
  86. screen.getByRole('button', {
  87. name: 'How to reproduce',
  88. })
  89. );
  90. expect(
  91. screen.getByText('This is the reproduction of a root cause.')
  92. ).toBeInTheDocument();
  93. });
  94. it('does not show reproduction steps when not applicable', function () {
  95. render(
  96. <AutofixRootCause
  97. {...{
  98. ...defaultProps,
  99. causes: [AutofixRootCauseData({reproduction: undefined})],
  100. }}
  101. />
  102. );
  103. expect(
  104. screen.queryByText('This is the reproduction of a root cause.')
  105. ).not.toBeInTheDocument();
  106. });
  107. it('shows unit test inside reproduction card when available', async function () {
  108. render(
  109. <AutofixRootCause
  110. {...{
  111. ...defaultProps,
  112. causes: [
  113. AutofixRootCauseData({
  114. unit_test: {
  115. snippet: 'Test case for root cause',
  116. description: 'This is the description of a unit test.',
  117. file_path: 'src/file.py',
  118. },
  119. }),
  120. ],
  121. }}
  122. />
  123. );
  124. expect(screen.getByText('How to reproduce')).toBeInTheDocument();
  125. await userEvent.click(
  126. screen.getByRole('button', {
  127. name: 'How to reproduce',
  128. })
  129. );
  130. expect(
  131. screen.getByText('This is the description of a unit test.')
  132. ).toBeInTheDocument();
  133. expect(screen.getByText('Test case for root cause')).toBeInTheDocument();
  134. });
  135. it('does not show reproduction or unit test when not available', function () {
  136. render(
  137. <AutofixRootCause
  138. {...{
  139. ...defaultProps,
  140. causes: [AutofixRootCauseData({unit_test: undefined, reproduction: undefined})],
  141. }}
  142. />
  143. );
  144. expect(screen.queryByText('How to reproduce')).not.toBeInTheDocument();
  145. });
  146. });