autofixChanges.analytics.spec.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {AutofixCodebaseChangeData} from 'sentry-fixture/autofixCodebaseChangeData';
  2. import {AutofixStepFixture} from 'sentry-fixture/autofixStep';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import {Button} from 'sentry/components/button';
  5. import {AutofixChanges} from 'sentry/components/events/autofix/autofixChanges';
  6. import {
  7. type AutofixChangesStep,
  8. AutofixStepType,
  9. } from 'sentry/components/events/autofix/types';
  10. jest.mock('sentry/components/button', () => ({
  11. Button: jest.fn(props => {
  12. // Forward the click handler while allowing us to inspect props
  13. return <button onClick={props.onClick}>{props.children}</button>;
  14. }),
  15. LinkButton: jest.fn(props => {
  16. return <a href={props.href}>{props.children}</a>;
  17. }),
  18. }));
  19. const mockButton = Button as jest.MockedFunction<typeof Button>;
  20. describe('AutofixChanges', () => {
  21. const defaultProps = {
  22. groupId: '123',
  23. runId: '456',
  24. step: AutofixStepFixture({
  25. type: AutofixStepType.CHANGES,
  26. changes: [AutofixCodebaseChangeData()],
  27. }) as AutofixChangesStep,
  28. };
  29. beforeEach(() => {
  30. MockApiClient.clearMockResponses();
  31. mockButton.mockClear();
  32. });
  33. it('passes correct analytics props for Create PR button when write access is enabled', async () => {
  34. MockApiClient.addMockResponse({
  35. url: '/issues/123/autofix/setup/?check_write_access=true',
  36. method: 'GET',
  37. body: {
  38. genAIConsent: {ok: true},
  39. integration: {ok: true},
  40. githubWriteIntegration: {
  41. repos: [{ok: true, owner: 'owner', name: 'hello-world', id: 100}],
  42. },
  43. },
  44. });
  45. render(<AutofixChanges {...defaultProps} />);
  46. await userEvent.click(screen.getByRole('button', {name: 'Draft PR'}));
  47. const createPRButtonCall = mockButton.mock.calls.find(
  48. call => call[0]?.analyticsEventKey === 'autofix.create_pr_clicked'
  49. );
  50. expect(createPRButtonCall?.[0]).toEqual(
  51. expect.objectContaining({
  52. analyticsEventKey: 'autofix.create_pr_clicked',
  53. analyticsEventName: 'Autofix: Create PR Clicked',
  54. analyticsParams: {group_id: '123'},
  55. })
  56. );
  57. });
  58. it('passes correct analytics props for Create PR Setup button when write access is not enabled', () => {
  59. MockApiClient.addMockResponse({
  60. url: '/issues/123/autofix/setup/?check_write_access=true',
  61. method: 'GET',
  62. body: {
  63. genAIConsent: {ok: true},
  64. integration: {ok: true},
  65. githubWriteIntegration: {
  66. repos: [{ok: false, owner: 'owner', name: 'hello-world', id: 100}],
  67. },
  68. },
  69. });
  70. render(<AutofixChanges {...defaultProps} />);
  71. // Find the last call to Button that matches our Setup button
  72. const setupButtonCall = mockButton.mock.calls.find(
  73. call => call[0].children === 'Draft PR'
  74. );
  75. expect(setupButtonCall?.[0]).toEqual(
  76. expect.objectContaining({
  77. analyticsEventKey: 'autofix.create_pr_setup_clicked',
  78. analyticsEventName: 'Autofix: Create PR Setup Clicked',
  79. analyticsParams: {
  80. group_id: '123',
  81. },
  82. })
  83. );
  84. });
  85. it('passes correct analytics props for Create Branch button when write access is enabled', async () => {
  86. MockApiClient.addMockResponse({
  87. url: '/issues/123/autofix/setup/?check_write_access=true',
  88. method: 'GET',
  89. body: {
  90. genAIConsent: {ok: true},
  91. integration: {ok: true},
  92. githubWriteIntegration: {
  93. repos: [{ok: true, owner: 'owner', name: 'hello-world', id: 100}],
  94. },
  95. },
  96. });
  97. render(<AutofixChanges {...defaultProps} />);
  98. await userEvent.click(screen.getByRole('button', {name: 'Check Out Locally'}));
  99. const createBranchButtonCall = mockButton.mock.calls.find(
  100. call => call[0]?.analyticsEventKey === 'autofix.push_to_branch_clicked'
  101. );
  102. expect(createBranchButtonCall?.[0]).toEqual(
  103. expect.objectContaining({
  104. analyticsEventKey: 'autofix.push_to_branch_clicked',
  105. analyticsEventName: 'Autofix: Push to Branch Clicked',
  106. analyticsParams: {group_id: '123'},
  107. })
  108. );
  109. });
  110. it('passes correct analytics props for Create Branch Setup button when write access is not enabled', () => {
  111. MockApiClient.addMockResponse({
  112. url: '/issues/123/autofix/setup/?check_write_access=true',
  113. method: 'GET',
  114. body: {
  115. genAIConsent: {ok: true},
  116. integration: {ok: true},
  117. githubWriteIntegration: {
  118. repos: [{ok: false, owner: 'owner', name: 'hello-world', id: 100}],
  119. },
  120. },
  121. });
  122. render(<AutofixChanges {...defaultProps} />);
  123. const setupButtonCall = mockButton.mock.calls.find(
  124. call => call[0].children === 'Check Out Locally'
  125. );
  126. expect(setupButtonCall?.[0]).toEqual(
  127. expect.objectContaining({
  128. analyticsEventKey: 'autofix.create_branch_setup_clicked',
  129. analyticsEventName: 'Autofix: Create Branch Setup Clicked',
  130. analyticsParams: {
  131. group_id: '123',
  132. },
  133. })
  134. );
  135. });
  136. it('passes correct analytics props for Add Tests button', () => {
  137. MockApiClient.addMockResponse({
  138. url: '/issues/123/autofix/setup/?check_write_access=true',
  139. method: 'GET',
  140. body: {
  141. genAIConsent: {ok: true},
  142. integration: {ok: true},
  143. githubWriteIntegration: {
  144. repos: [{ok: true, owner: 'owner', name: 'hello-world', id: 100}],
  145. },
  146. },
  147. });
  148. render(<AutofixChanges {...defaultProps} />);
  149. screen.getByText('Add Tests').click();
  150. const addTestsButtonCall = mockButton.mock.calls.find(
  151. call => call[0]?.analyticsEventKey === 'autofix.add_tests_clicked'
  152. );
  153. expect(addTestsButtonCall?.[0]).toEqual(
  154. expect.objectContaining({
  155. analyticsEventKey: 'autofix.add_tests_clicked',
  156. analyticsEventName: 'Autofix: Add Tests Clicked',
  157. analyticsParams: {group_id: '123'},
  158. })
  159. );
  160. });
  161. });