IssueList.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {IssueList} from 'sentry/components/issueList';
  3. import useApi from 'sentry/utils/useApi';
  4. jest.mock('sentry/utils/useApi');
  5. describe('IssueList', () => {
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. MockApiClient.clearMockResponses();
  9. });
  10. it('renders loading state', () => {
  11. const api = new MockApiClient();
  12. // @ts-ignore useApi is mocked
  13. useApi.mockReturnValue(api);
  14. jest.spyOn(api, 'request').mockImplementation(() => {
  15. return new Promise(_ => {
  16. return null;
  17. });
  18. });
  19. render(
  20. <IssueList
  21. location={TestStubs.location()}
  22. endpoint="endpoint"
  23. params={{}}
  24. router={TestStubs.router()}
  25. routes={[]}
  26. />
  27. );
  28. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  29. });
  30. it('renders error state', () => {
  31. const api = new MockApiClient();
  32. // @ts-ignore useApi is mocked
  33. useApi.mockReturnValue(api);
  34. MockApiClient.addMockResponse({
  35. url: 'endpoint',
  36. method: 'GET',
  37. statusCode: 400,
  38. });
  39. render(
  40. <IssueList
  41. location={TestStubs.location()}
  42. endpoint="endpoint"
  43. params={{}}
  44. router={TestStubs.router()}
  45. routes={[]}
  46. />
  47. );
  48. expect(screen.getByText('There was an error loading data.')).toBeInTheDocument();
  49. });
  50. it('refetches data on click from error state', async () => {
  51. const api = new MockApiClient();
  52. // @ts-ignore useApi is mocked
  53. useApi.mockReturnValue(api);
  54. MockApiClient.addMockResponse({
  55. url: 'endpoint',
  56. method: 'GET',
  57. statusCode: 400,
  58. });
  59. const spy = jest.spyOn(api, 'request');
  60. render(
  61. <IssueList
  62. location={TestStubs.location()}
  63. endpoint="endpoint"
  64. params={{}}
  65. router={TestStubs.router()}
  66. routes={[]}
  67. />
  68. );
  69. expect(
  70. await screen.findByText('There was an error loading data.')
  71. ).toBeInTheDocument();
  72. userEvent.click(screen.getByText('Retry'));
  73. expect(spy).toHaveBeenCalledTimes(2);
  74. });
  75. it('renders issue list', async () => {
  76. const api = new MockApiClient();
  77. const spy = jest.spyOn(api, 'request');
  78. // @ts-ignore useApi is mocked
  79. useApi.mockReturnValue(api);
  80. MockApiClient.addMockResponse({
  81. url: 'endpoint',
  82. method: 'GET',
  83. statusCode: 200,
  84. body: [TestStubs.Group({id: '1', culprit: 'Stubbed Issue'})],
  85. });
  86. render(
  87. <IssueList
  88. query={{limit: '5'}}
  89. location={TestStubs.location({
  90. query: {
  91. cursor: '10',
  92. },
  93. })}
  94. endpoint="endpoint"
  95. params={{}}
  96. router={TestStubs.router()}
  97. routes={[]}
  98. />
  99. );
  100. expect(await screen.findByText(/Stubbed Issue/)).toBeInTheDocument();
  101. // @ts-ignore
  102. expect(spy.mock.calls[0][1].query).toEqual({
  103. cursor: '10',
  104. limit: '5',
  105. });
  106. });
  107. it('renders custom empty state', () => {
  108. const api = new MockApiClient();
  109. const CustomEmptyState = jest.fn().mockImplementation(() => <div>Empty State</div>);
  110. // @ts-ignore useApi is mocked
  111. useApi.mockReturnValue(api);
  112. MockApiClient.addMockResponse({
  113. url: 'endpoint',
  114. method: 'GET',
  115. statusCode: 200,
  116. body: [],
  117. });
  118. render(
  119. <IssueList
  120. location={TestStubs.location()}
  121. endpoint="endpoint"
  122. params={{}}
  123. router={TestStubs.router()}
  124. routes={[]}
  125. renderEmpty={CustomEmptyState}
  126. />
  127. );
  128. expect(screen.getByText('Empty State')).toBeInTheDocument();
  129. expect(CustomEmptyState).toHaveBeenCalled();
  130. });
  131. it('renders empty state', () => {
  132. const api = new MockApiClient();
  133. MockApiClient.addMockResponse({
  134. url: 'endpoint',
  135. method: 'GET',
  136. statusCode: 200,
  137. body: [],
  138. });
  139. // @ts-ignore useApi is mocked
  140. useApi.mockReturnValue(api);
  141. render(
  142. <IssueList
  143. location={TestStubs.location()}
  144. endpoint="endpoint"
  145. params={{}}
  146. router={TestStubs.router()}
  147. routes={[]}
  148. />
  149. );
  150. expect(screen.getByText(/Nothing to show here, move along/)).toBeInTheDocument();
  151. });
  152. });