eventWaiter.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {render} from 'sentry-test/reactTestingLibrary';
  2. import EventWaiter from 'sentry/utils/eventWaiter';
  3. jest.useFakeTimers();
  4. describe('EventWaiter', function () {
  5. it('waits for the first projet event', async function () {
  6. const org = TestStubs.Organization();
  7. const project = TestStubs.ProjectDetails({
  8. firstEvent: null,
  9. });
  10. // Start with a project *without* a first event
  11. const projectApiMock = MockApiClient.addMockResponse({
  12. url: `/projects/${org.slug}/${project.slug}/`,
  13. method: 'GET',
  14. body: project,
  15. });
  16. const child = jest.fn().mockReturnValue(null);
  17. render(
  18. <EventWaiter
  19. api={new MockApiClient()}
  20. organization={org}
  21. project={project}
  22. eventType="error"
  23. pollInterval={10}
  24. >
  25. {child}
  26. </EventWaiter>
  27. );
  28. expect(child).toHaveBeenCalledWith({firstIssue: null});
  29. // Add the first events and associated responses and tick the timer
  30. project.firstEvent = '2019-05-01T00:00:00.000Z';
  31. const events = [
  32. {
  33. id: 1,
  34. firstSeen: project.firstEvent,
  35. },
  36. {
  37. id: 2,
  38. firstSeen: null,
  39. },
  40. ];
  41. MockApiClient.addMockResponse({
  42. url: `/projects/${org.slug}/${project.slug}/issues/`,
  43. method: 'GET',
  44. body: events,
  45. });
  46. child.mockClear();
  47. // Advanced time for the first setInterval tick to occur
  48. jest.advanceTimersByTime(1);
  49. // We have to await *two* API calls. We could normally do this using tick(),
  50. // however since we have enabled fake timers, we cannot tick.
  51. await Promise.resolve();
  52. await Promise.resolve();
  53. expect(child).toHaveBeenCalledWith({firstIssue: events[0]});
  54. // Check that the polling has stopped
  55. projectApiMock.mockClear();
  56. jest.advanceTimersByTime(10);
  57. expect(projectApiMock).not.toHaveBeenCalled();
  58. });
  59. it('receives a first event of `true` when first even has expired', async function () {
  60. const org = TestStubs.Organization();
  61. const project = TestStubs.ProjectDetails({
  62. firstEvent: '2019-05-01T00:00:00.000Z',
  63. });
  64. MockApiClient.addMockResponse({
  65. url: `/projects/${org.slug}/${project.slug}/`,
  66. method: 'GET',
  67. body: project,
  68. });
  69. // No events to list
  70. MockApiClient.addMockResponse({
  71. url: `/projects/${org.slug}/${project.slug}/issues/`,
  72. method: 'GET',
  73. body: [],
  74. });
  75. const child = jest.fn().mockReturnValue(null);
  76. render(
  77. <EventWaiter
  78. api={new MockApiClient()}
  79. organization={org}
  80. project={project}
  81. eventType="error"
  82. pollInterval={10}
  83. >
  84. {child}
  85. </EventWaiter>
  86. );
  87. // We have to await *two* API calls. We could normally do this using tick(),
  88. // however since we have enabled fake timers, we cannot tick.
  89. await Promise.resolve();
  90. await Promise.resolve();
  91. expect(child).toHaveBeenCalledWith({firstIssue: true});
  92. });
  93. it('does not poll when disabled', function () {
  94. const org = TestStubs.Organization();
  95. const project = TestStubs.ProjectDetails();
  96. const projectApiMock = MockApiClient.addMockResponse({
  97. url: `/projects/${org.slug}/${project.slug}/`,
  98. method: 'GET',
  99. body: project,
  100. });
  101. // No events to list
  102. MockApiClient.addMockResponse({
  103. url: `/projects/${org.slug}/${project.slug}/issues/`,
  104. method: 'GET',
  105. body: [],
  106. });
  107. const child = jest.fn().mockReturnValue(null);
  108. render(
  109. <EventWaiter
  110. api={new MockApiClient()}
  111. organization={org}
  112. project={project}
  113. eventType="error"
  114. pollInterval={10}
  115. disabled
  116. >
  117. {child}
  118. </EventWaiter>
  119. );
  120. expect(child).toHaveBeenCalledWith({firstIssue: null});
  121. // Ensure we do not call it again
  122. projectApiMock.mockClear();
  123. jest.advanceTimersByTime(10);
  124. expect(projectApiMock).not.toHaveBeenCalled();
  125. });
  126. });