eventWaiter.spec.tsx 4.1 KB

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