createSampleEventButton.spec.tsx 4.0 KB

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