createSampleEventButton.spec.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {browserHistory} from 'react-router';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import React from 'react';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import CreateSampleEventButton from 'app/views/onboarding/createSampleEventButton';
  6. jest.useFakeTimers();
  7. describe('CreateSampleEventButton', function() {
  8. const {org, project, routerContext} = initializeOrg();
  9. const groupID = '123';
  10. const wrapper = mountWithTheme(
  11. <CreateSampleEventButton source="test" project={project} />,
  12. routerContext
  13. );
  14. beforeEach(function() {
  15. MockApiClient.clearMockResponses();
  16. });
  17. it('creates a sample event', async function() {
  18. const createRequest = MockApiClient.addMockResponse({
  19. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  20. method: 'POST',
  21. body: {groupID},
  22. });
  23. wrapper
  24. .find('[data-test-id="create-sample-event"]')
  25. .first()
  26. .simulate('click');
  27. // The button should be disabled while creating the event
  28. expect(
  29. wrapper
  30. .find('[data-test-id="create-sample-event"]')
  31. .first()
  32. .prop('disabled')
  33. ).toBe(true);
  34. // We have to await the API calls. We could norally 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 existance 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. wrapper.update();
  52. expect(
  53. wrapper
  54. .find('[data-test-id="create-sample-event"]')
  55. .first()
  56. .prop('disabled')
  57. ).toBe(false);
  58. expect(browserHistory.push).toHaveBeenCalledWith(
  59. `/organizations/${org.slug}/issues/${groupID}/`
  60. );
  61. });
  62. it('waits for the latest event to be processed', async function() {
  63. const createRequest = MockApiClient.addMockResponse({
  64. url: `/projects/${org.slug}/${project.slug}/create-sample/`,
  65. method: 'POST',
  66. body: {groupID},
  67. });
  68. wrapper
  69. .find('[data-test-id="create-sample-event"]')
  70. .first()
  71. .simulate('click');
  72. await Promise.resolve();
  73. expect(createRequest).toHaveBeenCalled();
  74. // Start with no latest event
  75. let latestIssueRequest = MockApiClient.addMockResponse({
  76. url: `/issues/${groupID}/events/latest/`,
  77. statusCode: 404,
  78. body: {},
  79. });
  80. // Wait for the timeout once, the first request will 404
  81. jest.runAllTimers();
  82. await Promise.resolve();
  83. expect(latestIssueRequest).toHaveBeenCalled();
  84. await Promise.resolve();
  85. // Second request will be successful
  86. MockApiClient.clearMockResponses();
  87. latestIssueRequest = MockApiClient.addMockResponse({
  88. url: `/issues/${groupID}/events/latest/`,
  89. statusCode: 200,
  90. body: {},
  91. });
  92. jest.runAllTimers();
  93. await Promise.resolve();
  94. expect(latestIssueRequest).toHaveBeenCalled();
  95. await Promise.resolve();
  96. // wait for latestEventAvailable to resolve
  97. await Promise.resolve();
  98. expect(browserHistory.push).toHaveBeenCalledWith(
  99. `/organizations/${org.slug}/issues/${groupID}/`
  100. );
  101. });
  102. });