createSampleEventButton.spec.tsx 4.0 KB

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