projectContext.spec.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {ProjectContext} from 'sentry/views/projects/projectContext';
  3. jest.unmock('sentry/utils/recreateRoute');
  4. jest.mock('sentry/actionCreators/modal', () => ({
  5. redirectToProject: jest.fn(),
  6. }));
  7. describe('projectContext component', function () {
  8. const routes = [
  9. {path: '/', childRoutes: []},
  10. {name: 'Organizations', path: ':orgId/', childRoutes: []},
  11. {name: 'Projects', path: ':projectId/', childRoutes: []},
  12. ];
  13. const location = {query: {}};
  14. const project = TestStubs.Project();
  15. const org = TestStubs.Organization();
  16. beforeEach(function () {
  17. MockApiClient.clearMockResponses();
  18. [project.slug, 'new-slug'].forEach(slug => {
  19. MockApiClient.addMockResponse({
  20. url: `/projects/${org.slug}/${slug}/members/`,
  21. method: 'GET',
  22. body: [],
  23. });
  24. MockApiClient.addMockResponse({
  25. url: `/projects/${org.slug}/${slug}/environments/`,
  26. method: 'GET',
  27. body: [],
  28. });
  29. });
  30. });
  31. it('displays error on 404s', async function () {
  32. const router = TestStubs.router();
  33. MockApiClient.addMockResponse({
  34. url: `/projects/${org.slug}/${project.slug}/`,
  35. method: 'GET',
  36. statusCode: 404,
  37. });
  38. const projectContext = (
  39. <ProjectContext
  40. api={new MockApiClient()}
  41. params={{orgId: org.slug, projectId: project.slug}}
  42. projects={[]}
  43. routes={routes}
  44. router={router}
  45. location={location}
  46. orgId={org.slug}
  47. projectId={project.slug}
  48. />
  49. );
  50. render(projectContext);
  51. const loading = screen.getByTestId('loading-indicator');
  52. const errorText = await screen.findByText(
  53. 'The project you were looking for was not found.'
  54. );
  55. expect(errorText).toBeInTheDocument();
  56. expect(loading).not.toBeInTheDocument();
  57. });
  58. it('fetches data again if projectId changes', function () {
  59. const router = TestStubs.router();
  60. let fetchMock = MockApiClient.addMockResponse({
  61. url: `/projects/${org.slug}/${project.slug}/`,
  62. method: 'GET',
  63. statusCode: 200,
  64. body: project,
  65. });
  66. const projectContext = (
  67. <ProjectContext
  68. api={new MockApiClient()}
  69. params={{orgId: org.slug, projectId: project.slug}}
  70. projects={[]}
  71. routes={routes}
  72. router={router}
  73. location={location}
  74. orgId={org.slug}
  75. projectId={project.slug}
  76. />
  77. );
  78. const {rerender} = render(projectContext);
  79. expect(fetchMock).toHaveBeenCalledTimes(1);
  80. // Nothing should happen if we update and projectId is the same
  81. rerender(projectContext);
  82. expect(fetchMock).toHaveBeenCalledTimes(1);
  83. fetchMock = MockApiClient.addMockResponse({
  84. url: `/projects/${org.slug}/new-slug/`,
  85. method: 'GET',
  86. statusCode: 200,
  87. body: TestStubs.Project({slug: 'new-slug'}),
  88. });
  89. rerender(
  90. <ProjectContext
  91. api={new MockApiClient()}
  92. params={{orgId: org.slug, projectId: project.slug}}
  93. projects={[]}
  94. routes={routes}
  95. router={router}
  96. location={location}
  97. orgId={org.slug}
  98. projectId="new-slug"
  99. />
  100. );
  101. expect(fetchMock).toHaveBeenCalled();
  102. });
  103. it('fetches data again if projects list changes', function () {
  104. const router = TestStubs.router();
  105. const fetchMock = MockApiClient.addMockResponse({
  106. url: `/projects/${org.slug}/${project.slug}/`,
  107. method: 'GET',
  108. statusCode: 200,
  109. body: project,
  110. });
  111. const projectContext = (
  112. <ProjectContext
  113. api={new MockApiClient()}
  114. params={{orgId: org.slug, projectId: project.slug}}
  115. projects={[]}
  116. routes={routes}
  117. router={router}
  118. location={location}
  119. orgId={org.slug}
  120. projectId={project.slug}
  121. />
  122. );
  123. const {rerender} = render(projectContext);
  124. expect(fetchMock).toHaveBeenCalledTimes(1);
  125. // The project will become active, thus requesting org members
  126. MockApiClient.addMockResponse({
  127. url: `/organizations/${org.slug}/users/`,
  128. method: 'GET',
  129. statusCode: 200,
  130. body: [],
  131. });
  132. rerender(
  133. <ProjectContext
  134. api={new MockApiClient()}
  135. params={{orgId: org.slug, projectId: project.slug}}
  136. projects={[project]}
  137. routes={routes}
  138. router={router}
  139. location={location}
  140. orgId={org.slug}
  141. projectId={project.slug}
  142. />
  143. );
  144. expect(fetchMock).toHaveBeenCalledTimes(2);
  145. });
  146. });