createSampleEventButton.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. );
  22. }
  23. afterEach(function () {
  24. MockApiClient.clearMockResponses();
  25. });
  26. it('creates a sample event', async function () {
  27. renderComponent();
  28. const createRequest = MockApiClient.addMockResponse({
  29. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  30. method: 'POST',
  31. body: {groupID},
  32. });
  33. const sampleButton = await screen.findByRole('button', {name: createSampleText});
  34. userEvent.click(sampleButton);
  35. // The button should be disabled while creating the event
  36. expect(sampleButton).toBeDisabled();
  37. // We have to await the API calls. We could normally do this using tick(),
  38. // however since we have enabled fake timers to handle the spin-wait on the
  39. // event creation, we cannot use tick.
  40. await Promise.resolve();
  41. expect(createRequest).toHaveBeenCalled();
  42. const latestIssueRequest = MockApiClient.addMockResponse({
  43. url: `/issues/${groupID}/events/latest/`,
  44. body: {},
  45. });
  46. // There is a timeout before we check for the existence of the latest
  47. // event. Wait for it then wait for the request to complete
  48. jest.runAllTimers();
  49. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  50. // Wait for the api request and latestEventAvailable to resolve
  51. expect(sampleButton).toBeEnabled();
  52. expect(browserHistory.push).toHaveBeenCalledWith(
  53. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}`
  54. );
  55. });
  56. it('waits for the latest event to be processed', async function () {
  57. renderComponent();
  58. const createRequest = MockApiClient.addMockResponse({
  59. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  60. method: 'POST',
  61. body: {groupID},
  62. });
  63. userEvent.click(await screen.findByRole('button', {name: createSampleText}));
  64. await waitFor(() => expect(createRequest).toHaveBeenCalled());
  65. // Start with no latest event
  66. let latestIssueRequest = MockApiClient.addMockResponse({
  67. url: `/issues/${groupID}/events/latest/`,
  68. statusCode: 404,
  69. body: {},
  70. });
  71. // Wait for the timeout once, the first request will 404
  72. jest.runAllTimers();
  73. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  74. // Second request will be successful
  75. MockApiClient.clearMockResponses();
  76. latestIssueRequest = MockApiClient.addMockResponse({
  77. url: `/issues/${groupID}/events/latest/`,
  78. statusCode: 200,
  79. body: {},
  80. });
  81. jest.runAllTimers();
  82. await waitFor(() => expect(latestIssueRequest).toHaveBeenCalled());
  83. expect(browserHistory.push).toHaveBeenCalledWith(
  84. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}`
  85. );
  86. expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
  87. 'sample_event.created',
  88. expect.objectContaining({
  89. organization: org,
  90. project_id: project.id,
  91. interval: 1000,
  92. retries: 1,
  93. source: 'test',
  94. platform: 'javascript',
  95. })
  96. );
  97. expect(Sentry.captureMessage).not.toHaveBeenCalled();
  98. });
  99. });