organizationContextContainer.spec.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import {EnvironmentsFixture} from 'sentry-fixture/environments';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {TeamFixture} from 'sentry-fixture/team';
  5. import {UserFixture} from 'sentry-fixture/user';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  8. import * as OrganizationActionCreator from 'sentry/actionCreators/organization';
  9. import * as openSudo from 'sentry/actionCreators/sudoModal';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import OrganizationStore from 'sentry/stores/organizationStore';
  12. import ProjectsStore from 'sentry/stores/projectsStore';
  13. import TeamStore from 'sentry/stores/teamStore';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import OrganizationLegacyContext from 'sentry/views/organizationContextContainer';
  16. describe('OrganizationContextContainer', function () {
  17. const {organization, projects, routerProps} = initializeOrg();
  18. const teams = [TeamFixture()];
  19. const api = new MockApiClient();
  20. let getOrgMock: jest.Mock;
  21. let getProjectsMock: jest.Mock;
  22. let getTeamsMock: jest.Mock;
  23. function DisplayOrg() {
  24. const contextOrg = useOrganization();
  25. return <div>{contextOrg.slug}</div>;
  26. }
  27. type Props = Partial<React.ComponentProps<typeof OrganizationLegacyContext>>;
  28. function makeComponent(props?: Props) {
  29. return (
  30. <OrganizationLegacyContext
  31. {...routerProps}
  32. api={api}
  33. params={{orgId: 'org-slug'}}
  34. location={LocationFixture({query: {}})}
  35. useLastOrganization={false}
  36. organizationsLoading={false}
  37. organizations={[]}
  38. includeSidebar={false}
  39. {...props}
  40. >
  41. <DisplayOrg />
  42. </OrganizationLegacyContext>
  43. );
  44. }
  45. function renderComponent(props?: Props) {
  46. return render(makeComponent(props));
  47. }
  48. beforeEach(function () {
  49. MockApiClient.clearMockResponses();
  50. getOrgMock = MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/',
  52. body: organization,
  53. });
  54. getProjectsMock = MockApiClient.addMockResponse({
  55. url: '/organizations/org-slug/projects/',
  56. body: projects,
  57. });
  58. getTeamsMock = MockApiClient.addMockResponse({
  59. url: '/organizations/org-slug/teams/',
  60. body: teams,
  61. });
  62. jest.spyOn(TeamStore, 'loadInitialData');
  63. jest.spyOn(ProjectsStore, 'loadInitialData');
  64. jest.spyOn(OrganizationActionCreator, 'fetchOrganizationDetails');
  65. ConfigStore.init();
  66. });
  67. afterEach(function () {
  68. OrganizationStore.reset();
  69. jest.restoreAllMocks();
  70. });
  71. it('renders and fetches org, projects, and teams', async function () {
  72. renderComponent();
  73. await waitFor(() => expect(getOrgMock).toHaveBeenCalled());
  74. expect(getProjectsMock).toHaveBeenCalled();
  75. expect(getTeamsMock).toHaveBeenCalled();
  76. expect(screen.queryByRole('loading-indicator')).not.toBeInTheDocument();
  77. expect(screen.getByText(organization.slug)).toBeInTheDocument();
  78. expect(
  79. screen.queryByText('The organization you were looking for was not found.')
  80. ).not.toBeInTheDocument();
  81. expect(TeamStore.loadInitialData).toHaveBeenCalledWith(teams);
  82. expect(ProjectsStore.loadInitialData).toHaveBeenCalledWith(projects);
  83. expect(OrganizationActionCreator.fetchOrganizationDetails).toHaveBeenCalledWith(
  84. api,
  85. 'org-slug',
  86. true,
  87. true
  88. );
  89. });
  90. it('fetches new org when router params change', async function () {
  91. const newOrg = OrganizationFixture({slug: 'new-slug'});
  92. const {rerender} = renderComponent();
  93. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  94. const mock = MockApiClient.addMockResponse({
  95. url: '/organizations/new-slug/',
  96. body: newOrg,
  97. });
  98. const projectsMock = MockApiClient.addMockResponse({
  99. url: '/organizations/new-slug/projects/',
  100. body: projects,
  101. });
  102. const teamsMock = MockApiClient.addMockResponse({
  103. url: '/organizations/new-slug/teams/',
  104. body: teams,
  105. });
  106. // Re-render with new org slug
  107. rerender(makeComponent({params: {orgId: newOrg.slug}}));
  108. // Loads new org
  109. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  110. // Renders new org
  111. expect(await screen.findByText(newOrg.slug)).toBeInTheDocument();
  112. expect(mock).toHaveBeenLastCalledWith('/organizations/new-slug/', expect.anything());
  113. expect(projectsMock).toHaveBeenCalled();
  114. expect(teamsMock).toHaveBeenCalled();
  115. });
  116. it('shows loading error for non-superusers on 403s', async function () {
  117. getOrgMock = MockApiClient.addMockResponse({
  118. url: '/organizations/org-slug/',
  119. statusCode: 403,
  120. });
  121. jest.spyOn(console, 'error').mockImplementation(jest.fn()); // eslint-disable-line no-console
  122. renderComponent();
  123. expect(
  124. await screen.findByText('There was an error loading data.')
  125. ).toBeInTheDocument();
  126. // eslint-disable-next-line no-console
  127. expect(console.error).toHaveBeenCalled();
  128. });
  129. it('opens sudo modal for superusers on 403s', async function () {
  130. const openSudoSpy = jest.spyOn(openSudo, 'openSudo');
  131. ConfigStore.set('user', UserFixture({isSuperuser: true}));
  132. getOrgMock = MockApiClient.addMockResponse({
  133. url: '/organizations/org-slug/',
  134. statusCode: 403,
  135. });
  136. renderComponent();
  137. await waitFor(() => expect(openSudoSpy).toHaveBeenCalled());
  138. });
  139. it('uses last organization from ConfigStore', function () {
  140. getOrgMock = MockApiClient.addMockResponse({
  141. url: '/organizations/last-org/',
  142. body: organization,
  143. });
  144. MockApiClient.addMockResponse({
  145. url: '/organizations/last-org/projects/',
  146. body: projects,
  147. });
  148. MockApiClient.addMockResponse({
  149. url: '/organizations/last-org/teams/',
  150. body: teams,
  151. });
  152. ConfigStore.set('lastOrganization', 'last-org');
  153. renderComponent({useLastOrganization: true, params: {orgId: ''}});
  154. expect(getOrgMock).toHaveBeenLastCalledWith(
  155. '/organizations/last-org/',
  156. expect.anything()
  157. );
  158. });
  159. it('uses last organization from `organizations` prop', async function () {
  160. MockApiClient.addMockResponse({
  161. url: '/organizations/foo/environments/',
  162. body: EnvironmentsFixture(),
  163. });
  164. getOrgMock = MockApiClient.addMockResponse({
  165. url: '/organizations/foo/',
  166. body: organization,
  167. });
  168. getProjectsMock = MockApiClient.addMockResponse({
  169. url: '/organizations/foo/projects/',
  170. body: projects,
  171. });
  172. getTeamsMock = MockApiClient.addMockResponse({
  173. url: '/organizations/foo/teams/',
  174. body: teams,
  175. });
  176. ConfigStore.set('lastOrganization', '');
  177. const {rerender} = renderComponent({
  178. params: {orgId: ''},
  179. useLastOrganization: true,
  180. organizationsLoading: true,
  181. organizations: [],
  182. });
  183. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  184. rerender(
  185. makeComponent({
  186. params: {orgId: ''},
  187. useLastOrganization: true,
  188. organizationsLoading: false,
  189. organizations: [
  190. OrganizationFixture({slug: 'foo'}),
  191. OrganizationFixture({slug: 'bar'}),
  192. ],
  193. })
  194. );
  195. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  196. expect(getOrgMock).toHaveBeenCalled();
  197. expect(getProjectsMock).toHaveBeenCalled();
  198. expect(getTeamsMock).toHaveBeenCalled();
  199. });
  200. it('uses last organization when no orgId in URL - and fetches org details once', async function () {
  201. getOrgMock = MockApiClient.addMockResponse({
  202. url: '/organizations/my-last-org/',
  203. body: OrganizationFixture({slug: 'my-last-org'}),
  204. });
  205. getProjectsMock = MockApiClient.addMockResponse({
  206. url: '/organizations/my-last-org/projects/',
  207. body: projects,
  208. });
  209. getTeamsMock = MockApiClient.addMockResponse({
  210. url: '/organizations/my-last-org/teams/',
  211. body: teams,
  212. });
  213. ConfigStore.set('lastOrganization', 'my-last-org');
  214. const {rerender} = renderComponent({
  215. params: {orgId: ''},
  216. useLastOrganization: true,
  217. organizations: [],
  218. });
  219. expect(await screen.findByText('my-last-org')).toBeInTheDocument();
  220. expect(getOrgMock).toHaveBeenCalledTimes(1);
  221. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  222. // org details fetch
  223. rerender(
  224. makeComponent({
  225. params: {orgId: ''},
  226. useLastOrganization: true,
  227. organizationsLoading: false,
  228. organizations: [
  229. OrganizationFixture({slug: 'foo'}),
  230. OrganizationFixture({slug: 'bar'}),
  231. ],
  232. })
  233. );
  234. expect(getOrgMock).toHaveBeenCalledTimes(1);
  235. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  236. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  237. });
  238. it('fetches org details only once if organizations loading store changes', async function () {
  239. const {rerender} = renderComponent({
  240. params: {orgId: 'org-slug'},
  241. organizationsLoading: true,
  242. organizations: [],
  243. });
  244. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  245. expect(getOrgMock).toHaveBeenCalledTimes(1);
  246. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  247. // org details fetch
  248. rerender(
  249. makeComponent({
  250. params: {orgId: 'org-slug'},
  251. organizationsLoading: false,
  252. organizations: [
  253. OrganizationFixture({slug: 'foo'}),
  254. OrganizationFixture({slug: 'bar'}),
  255. ],
  256. })
  257. );
  258. expect(getOrgMock).toHaveBeenCalledTimes(1);
  259. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  260. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  261. });
  262. });