createSampleEventButton.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  5. import CreateSampleEventButton from 'sentry/views/onboarding/createSampleEventButton';
  6. jest.useFakeTimers();
  7. jest.mock('sentry/utils/analytics/trackAdvancedAnalyticsEvent');
  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. userEvent.click(sampleButton);
  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. userEvent.click(await screen.findByRole('button', {name: createSampleText}));
  65. await waitFor(() => expect(createRequest).toHaveBeenCalled());
  66. // Start with no latest event
  67. let latestIssueRequest = MockApiClient.addMockResponse({
  68. url: `/issues/${groupID}/events/latest/`,
  69. statusCode: 404,
  70. body: {},
  71. });
  72. // Wait for the timeout once, the first request will 404
  73. jest.runAllTimers();
  74. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  75. // Second request will be successful
  76. MockApiClient.clearMockResponses();
  77. latestIssueRequest = MockApiClient.addMockResponse({
  78. url: `/issues/${groupID}/events/latest/`,
  79. statusCode: 200,
  80. body: {},
  81. });
  82. jest.runAllTimers();
  83. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  84. expect(browserHistory.push).toHaveBeenCalledWith(
  85. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}&referrer=sample-error`
  86. );
  87. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  88. 'sample_event.created',
  89. expect.objectContaining({
  90. organization: org,
  91. project_id: project.id,
  92. interval: 1000,
  93. retries: 1,
  94. source: 'test',
  95. platform: 'javascript',
  96. })
  97. );
  98. expect(Sentry.captureMessage).not.toHaveBeenCalled();
  99. });
  100. });