eventWaiter.spec.tsx 4.0 KB

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