createSampleEventButton.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {browserHistory} from 'react-router';
  2. import * as Sentry from '@sentry/react';
  3. import {mountWithTheme} from 'sentry-test/enzyme';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {trackAnalyticsEvent} 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, project, routerContext} = initializeOrg();
  11. const groupID = '123';
  12. const wrapper = mountWithTheme(
  13. <CreateSampleEventButton
  14. source="test"
  15. project={{...project, platform: 'javascript'}}
  16. />,
  17. routerContext
  18. );
  19. beforeEach(function () {
  20. MockApiClient.clearMockResponses();
  21. });
  22. it('creates a sample event', async function () {
  23. const createRequest = MockApiClient.addMockResponse({
  24. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  25. method: 'POST',
  26. body: {groupID},
  27. });
  28. wrapper.find('[data-test-id="create-sample-event"]').first().simulate('click');
  29. // The button should be disabled while creating the event
  30. expect(
  31. wrapper.find('[data-test-id="create-sample-event"]').first().prop('disabled')
  32. ).toBe(true);
  33. // We have to await the API calls. We could normally do this using tick(),
  34. // however since we have enabled fake timers to handle the spin-wait on the
  35. // event creation, we cannot use tick.
  36. await Promise.resolve();
  37. expect(createRequest).toHaveBeenCalled();
  38. const latestIssueRequest = MockApiClient.addMockResponse({
  39. url: `/issues/${groupID}/events/latest/`,
  40. body: {},
  41. });
  42. // There is a timeout before we check for the existence of the latest
  43. // event. Wait for it then wait for the request to complete
  44. jest.runAllTimers();
  45. await Promise.resolve();
  46. expect(latestIssueRequest).toHaveBeenCalled();
  47. // Wait for the api request and latestEventAvailable to resolve
  48. await Promise.resolve();
  49. await Promise.resolve();
  50. await Promise.resolve();
  51. await Promise.resolve();
  52. wrapper.update();
  53. expect(
  54. wrapper.find('[data-test-id="create-sample-event"]').first().prop('disabled')
  55. ).toBe(false);
  56. expect(browserHistory.push).toHaveBeenCalledWith(
  57. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}`
  58. );
  59. });
  60. it('waits for the latest event to be processed', async function () {
  61. const createRequest = MockApiClient.addMockResponse({
  62. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  63. method: 'POST',
  64. body: {groupID},
  65. });
  66. wrapper.find('[data-test-id="create-sample-event"]').first().simulate('click');
  67. await Promise.resolve();
  68. 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 Promise.resolve();
  78. expect(latestIssueRequest).toHaveBeenCalled();
  79. await Promise.resolve();
  80. // Second request will be successful
  81. MockApiClient.clearMockResponses();
  82. latestIssueRequest = MockApiClient.addMockResponse({
  83. url: `/issues/${groupID}/events/latest/`,
  84. statusCode: 200,
  85. body: {},
  86. });
  87. jest.runAllTimers();
  88. await Promise.resolve();
  89. expect(latestIssueRequest).toHaveBeenCalled();
  90. await Promise.resolve();
  91. // wait for latestEventAvailable to resolve
  92. await Promise.resolve();
  93. expect(browserHistory.push).toHaveBeenCalledWith(
  94. `/organizations/${org.slug}/issues/${groupID}/?project=${project.id}`
  95. );
  96. expect(trackAnalyticsEvent).toHaveBeenCalledWith(
  97. expect.objectContaining({
  98. eventKey: 'sample_event.created',
  99. eventName: 'Sample Event Created',
  100. organization_id: org.id,
  101. project_id: project.id,
  102. interval: 1000,
  103. retries: 1,
  104. source: 'test',
  105. platform: 'javascript',
  106. })
  107. );
  108. expect(Sentry.captureMessage).not.toHaveBeenCalled();
  109. });
  110. });