createSampleEventButton.spec.jsx 4.1 KB

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