createSampleEventButton.spec.tsx 3.9 KB

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