projectContext.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. projectSlug={project.slug}
  38. >
  39. {null}
  40. </ProjectContext>
  41. );
  42. render(projectContext);
  43. const loading = screen.getByTestId('loading-indicator');
  44. const errorText = await screen.findByText(
  45. 'The project you were looking for was not found.'
  46. );
  47. expect(errorText).toBeInTheDocument();
  48. expect(loading).not.toBeInTheDocument();
  49. });
  50. it('fetches data again if projectId changes', function () {
  51. let fetchMock = MockApiClient.addMockResponse({
  52. url: `/projects/${org.slug}/${project.slug}/`,
  53. method: 'GET',
  54. statusCode: 200,
  55. body: project,
  56. });
  57. const projectContext = (
  58. <ProjectContext
  59. api={new MockApiClient()}
  60. projects={[]}
  61. loadingProjects={false}
  62. organization={org}
  63. projectSlug={project.slug}
  64. >
  65. {null}
  66. </ProjectContext>
  67. );
  68. const {rerender} = render(projectContext);
  69. expect(fetchMock).toHaveBeenCalledTimes(1);
  70. // Nothing should happen if we update and projectId is the same
  71. rerender(projectContext);
  72. expect(fetchMock).toHaveBeenCalledTimes(1);
  73. fetchMock = MockApiClient.addMockResponse({
  74. url: `/projects/${org.slug}/new-slug/`,
  75. method: 'GET',
  76. statusCode: 200,
  77. body: TestStubs.Project({slug: 'new-slug'}),
  78. });
  79. rerender(
  80. <ProjectContext
  81. api={new MockApiClient()}
  82. projects={[]}
  83. loadingProjects={false}
  84. organization={org}
  85. projectSlug="new-slug"
  86. >
  87. {null}
  88. </ProjectContext>
  89. );
  90. expect(fetchMock).toHaveBeenCalled();
  91. });
  92. it('fetches data again if projects list changes', function () {
  93. const fetchMock = MockApiClient.addMockResponse({
  94. url: `/projects/${org.slug}/${project.slug}/`,
  95. method: 'GET',
  96. statusCode: 200,
  97. body: project,
  98. });
  99. const projectContext = (
  100. <ProjectContext
  101. api={new MockApiClient()}
  102. loadingProjects={false}
  103. projects={[]}
  104. organization={org}
  105. projectSlug={project.slug}
  106. >
  107. {null}
  108. </ProjectContext>
  109. );
  110. const {rerender} = render(projectContext);
  111. expect(fetchMock).toHaveBeenCalledTimes(1);
  112. // The project will become active, thus requesting org members
  113. MockApiClient.addMockResponse({
  114. url: `/organizations/${org.slug}/users/`,
  115. method: 'GET',
  116. statusCode: 200,
  117. body: [],
  118. });
  119. rerender(
  120. <ProjectContext
  121. organization={org}
  122. loadingProjects={false}
  123. api={new MockApiClient()}
  124. projects={[project]}
  125. projectSlug={project.slug}
  126. >
  127. {null}
  128. </ProjectContext>
  129. );
  130. expect(fetchMock).toHaveBeenCalledTimes(2);
  131. });
  132. });