projectContext.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Organization} from 'sentry-fixture/organization';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {ProjectContext} from 'sentry/views/projects/projectContext';
  4. jest.unmock('sentry/utils/recreateRoute');
  5. jest.mock('sentry/actionCreators/modal', () => ({
  6. redirectToProject: jest.fn(),
  7. }));
  8. describe('projectContext component', function () {
  9. const project = TestStubs.Project();
  10. const org = Organization();
  11. beforeEach(function () {
  12. MockApiClient.clearMockResponses();
  13. [project.slug, 'new-slug'].forEach(slug => {
  14. MockApiClient.addMockResponse({
  15. url: `/projects/${org.slug}/${slug}/members/`,
  16. method: 'GET',
  17. body: [],
  18. });
  19. MockApiClient.addMockResponse({
  20. url: `/projects/${org.slug}/${slug}/environments/`,
  21. method: 'GET',
  22. body: [],
  23. });
  24. });
  25. });
  26. it('displays error on 404s', async function () {
  27. MockApiClient.addMockResponse({
  28. url: `/projects/${org.slug}/${project.slug}/`,
  29. method: 'GET',
  30. statusCode: 404,
  31. });
  32. const projectContext = (
  33. <ProjectContext
  34. api={new MockApiClient()}
  35. loadingProjects={false}
  36. projects={[]}
  37. organization={org}
  38. projectSlug={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. projectSlug={project.slug}
  65. >
  66. {null}
  67. </ProjectContext>
  68. );
  69. const {rerender} = render(projectContext);
  70. expect(fetchMock).toHaveBeenCalledTimes(1);
  71. // Nothing should happen if we update and projectId is the same
  72. rerender(projectContext);
  73. expect(fetchMock).toHaveBeenCalledTimes(1);
  74. fetchMock = MockApiClient.addMockResponse({
  75. url: `/projects/${org.slug}/new-slug/`,
  76. method: 'GET',
  77. statusCode: 200,
  78. body: TestStubs.Project({slug: 'new-slug'}),
  79. });
  80. rerender(
  81. <ProjectContext
  82. api={new MockApiClient()}
  83. projects={[]}
  84. loadingProjects={false}
  85. organization={org}
  86. projectSlug="new-slug"
  87. >
  88. {null}
  89. </ProjectContext>
  90. );
  91. expect(fetchMock).toHaveBeenCalled();
  92. });
  93. it('fetches data again if projects list changes', function () {
  94. const fetchMock = MockApiClient.addMockResponse({
  95. url: `/projects/${org.slug}/${project.slug}/`,
  96. method: 'GET',
  97. statusCode: 200,
  98. body: project,
  99. });
  100. const projectContext = (
  101. <ProjectContext
  102. api={new MockApiClient()}
  103. loadingProjects={false}
  104. projects={[]}
  105. organization={org}
  106. projectSlug={project.slug}
  107. >
  108. {null}
  109. </ProjectContext>
  110. );
  111. const {rerender} = render(projectContext);
  112. expect(fetchMock).toHaveBeenCalledTimes(1);
  113. // The project will become active, thus requesting org members
  114. MockApiClient.addMockResponse({
  115. url: `/organizations/${org.slug}/users/`,
  116. method: 'GET',
  117. statusCode: 200,
  118. body: [],
  119. });
  120. rerender(
  121. <ProjectContext
  122. organization={org}
  123. loadingProjects={false}
  124. api={new MockApiClient()}
  125. projects={[project]}
  126. projectSlug={project.slug}
  127. >
  128. {null}
  129. </ProjectContext>
  130. );
  131. expect(fetchMock).toHaveBeenCalledTimes(2);
  132. });
  133. });