projectContext.spec.tsx 3.9 KB

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