projectContext.spec.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  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. const wrapper = mountWithTheme(projectContext);
  51. await tick();
  52. wrapper.update();
  53. expect(wrapper.state('error')).toBe(true);
  54. expect(wrapper.state('loading')).toBe(false);
  55. expect(wrapper.state('errorType')).toBe('PROJECT_NOT_FOUND');
  56. });
  57. it('fetches data again if projectId changes', function () {
  58. const router = TestStubs.router();
  59. let fetchMock = MockApiClient.addMockResponse({
  60. url: `/projects/${org.slug}/${project.slug}/`,
  61. method: 'GET',
  62. statusCode: 200,
  63. body: project,
  64. });
  65. const projectContext = (
  66. <ProjectContext
  67. api={new MockApiClient()}
  68. params={{orgId: org.slug, projectId: project.slug}}
  69. projects={[]}
  70. routes={routes}
  71. router={router}
  72. location={location}
  73. orgId={org.slug}
  74. projectId={project.slug}
  75. />
  76. );
  77. const wrapper = mountWithTheme(projectContext);
  78. expect(fetchMock).toHaveBeenCalledTimes(1);
  79. // Nothing should happen if we update and projectId is the same
  80. wrapper.update();
  81. expect(fetchMock).toHaveBeenCalledTimes(1);
  82. fetchMock = MockApiClient.addMockResponse({
  83. url: `/projects/${org.slug}/new-slug/`,
  84. method: 'GET',
  85. statusCode: 200,
  86. body: TestStubs.Project({slug: 'new-slug'}),
  87. });
  88. wrapper.setProps({
  89. projectId: 'new-slug',
  90. });
  91. wrapper.update();
  92. expect(fetchMock).toHaveBeenCalled();
  93. });
  94. it('fetches data again if projects list changes', function () {
  95. const router = TestStubs.router();
  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. params={{orgId: org.slug, projectId: project.slug}}
  106. projects={[]}
  107. routes={routes}
  108. router={router}
  109. location={location}
  110. orgId={org.slug}
  111. projectId={project.slug}
  112. />
  113. );
  114. const wrapper = mountWithTheme(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. wrapper.setProps({projects: [project]});
  124. wrapper.update();
  125. expect(fetchMock).toHaveBeenCalledTimes(2);
  126. });
  127. });