createSampleEventButton.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import * as Sentry from '@sentry/react';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {trackAnalytics} from 'sentry/utils/analytics';
  6. import {browserHistory} from 'sentry/utils/browserHistory';
  7. import CreateSampleEventButton from 'sentry/views/onboarding/createSampleEventButton';
  8. // biome-ignore lint/correctness/useHookAtTopLevel: not a hook
  9. jest.useFakeTimers();
  10. jest.mock('sentry/utils/analytics');
  11. describe('CreateSampleEventButton', function () {
  12. const org = OrganizationFixture();
  13. const project = ProjectFixture();
  14. const groupID = '123';
  15. const createSampleText = 'Create a sample event';
  16. function renderComponent() {
  17. return render(
  18. <CreateSampleEventButton
  19. source="test"
  20. project={{...project, platform: 'javascript'}}
  21. >
  22. {createSampleText}
  23. </CreateSampleEventButton>,
  24. {organization: org}
  25. );
  26. }
  27. afterEach(function () {
  28. MockApiClient.clearMockResponses();
  29. });
  30. it('creates a sample event', async function () {
  31. renderComponent();
  32. const createRequest = MockApiClient.addMockResponse({
  33. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  34. method: 'POST',
  35. body: {groupID},
  36. });
  37. const sampleButton = await screen.findByRole('button', {name: createSampleText});
  38. await userEvent.click(sampleButton, {delay: null});
  39. // The button should be disabled while creating the event
  40. expect(sampleButton).toBeDisabled();
  41. // We have to await the API calls. We could normally do this using tick(),
  42. // however since we have enabled fake timers to handle the spin-wait on the
  43. // event creation, we cannot use tick.
  44. await Promise.resolve();
  45. expect(createRequest).toHaveBeenCalled();
  46. const latestIssueRequest = MockApiClient.addMockResponse({
  47. url: `/issues/${groupID}/events/latest/`,
  48. body: {},
  49. });
  50. // There is a timeout before we check for the existence of the latest
  51. // event. Wait for it then wait for the request to complete
  52. jest.runAllTimers();
  53. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  54. // Wait for the api request and latestEventAvailable to resolve
  55. expect(sampleButton).toBeEnabled();
  56. expect(browserHistory.push).toHaveBeenCalledWith(
  57. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}&referrer=sample-error`
  58. );
  59. });
  60. it('waits for the latest event to be processed', async function () {
  61. renderComponent();
  62. const createRequest = MockApiClient.addMockResponse({
  63. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  64. method: 'POST',
  65. body: {groupID},
  66. });
  67. await userEvent.click(await screen.findByRole('button', {name: createSampleText}), {
  68. delay: null,
  69. });
  70. await waitFor(() => expect(createRequest).toHaveBeenCalled());
  71. // Start with no latest event
  72. let latestIssueRequest = MockApiClient.addMockResponse({
  73. url: `/issues/${groupID}/events/latest/`,
  74. statusCode: 404,
  75. body: {},
  76. });
  77. // Wait for the timeout once, the first request will 404
  78. jest.runAllTimers();
  79. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  80. // Second request will be successful
  81. MockApiClient.clearMockResponses();
  82. latestIssueRequest = MockApiClient.addMockResponse({
  83. url: `/issues/${groupID}/events/latest/`,
  84. statusCode: 200,
  85. body: {},
  86. });
  87. jest.runAllTimers();
  88. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  89. expect(browserHistory.push).toHaveBeenCalledWith(
  90. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}&referrer=sample-error`
  91. );
  92. expect(trackAnalytics).toHaveBeenCalledWith(
  93. 'sample_event.created',
  94. expect.objectContaining({
  95. organization: expect.objectContaining(org),
  96. project_id: project.id,
  97. interval: 1000,
  98. retries: 1,
  99. source: 'test',
  100. platform: 'javascript',
  101. })
  102. );
  103. expect(Sentry.captureMessage).not.toHaveBeenCalled();
  104. });
  105. });