organizationContextContainer.spec.tsx 9.4 KB

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