organizationContextContainer.spec.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import {Organization} from 'sentry-fixture/organization';
  2. import {Team} from 'sentry-fixture/team';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import * as OrganizationActionCreator from 'sentry/actionCreators/organization';
  6. import * as openSudo from 'sentry/actionCreators/sudoModal';
  7. import ConfigStore from 'sentry/stores/configStore';
  8. import OrganizationStore from 'sentry/stores/organizationStore';
  9. import ProjectsStore from 'sentry/stores/projectsStore';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import {OrganizationLegacyContext} from 'sentry/views/organizationContextContainer';
  13. jest.mock('sentry/stores/configStore', () => ({
  14. get: jest.fn(),
  15. }));
  16. describe('OrganizationContextContainer', function () {
  17. const {organization, projects, routerProps} = initializeOrg();
  18. const teams = [Team()];
  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={TestStubs.location({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. });
  66. afterEach(function () {
  67. OrganizationStore.reset();
  68. jest.restoreAllMocks();
  69. });
  70. it('renders and fetches org, projects, and teams', async function () {
  71. renderComponent();
  72. await waitFor(() => expect(getOrgMock).toHaveBeenCalled());
  73. expect(getProjectsMock).toHaveBeenCalled();
  74. expect(getTeamsMock).toHaveBeenCalled();
  75. expect(screen.queryByRole('loading-indicator')).not.toBeInTheDocument();
  76. expect(screen.getByText(organization.slug)).toBeInTheDocument();
  77. expect(
  78. screen.queryByText('The organization you were looking for was not found.')
  79. ).not.toBeInTheDocument();
  80. expect(TeamStore.loadInitialData).toHaveBeenCalledWith(teams);
  81. expect(ProjectsStore.loadInitialData).toHaveBeenCalledWith(projects);
  82. expect(OrganizationActionCreator.fetchOrganizationDetails).toHaveBeenCalledWith(
  83. api,
  84. 'org-slug',
  85. true,
  86. true
  87. );
  88. });
  89. it('fetches new org when router params change', async function () {
  90. const newOrg = Organization({slug: 'new-slug'});
  91. const {rerender} = renderComponent();
  92. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  93. const mock = MockApiClient.addMockResponse({
  94. url: '/organizations/new-slug/',
  95. body: newOrg,
  96. });
  97. const projectsMock = MockApiClient.addMockResponse({
  98. url: '/organizations/new-slug/projects/',
  99. body: projects,
  100. });
  101. const teamsMock = MockApiClient.addMockResponse({
  102. url: '/organizations/new-slug/teams/',
  103. body: teams,
  104. });
  105. // Re-render with new org slug
  106. rerender(makeComponent({params: {orgId: newOrg.slug}}));
  107. // Loads new org
  108. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  109. // Renders new org
  110. expect(await screen.findByText(newOrg.slug)).toBeInTheDocument();
  111. expect(mock).toHaveBeenLastCalledWith('/organizations/new-slug/', expect.anything());
  112. expect(projectsMock).toHaveBeenCalled();
  113. expect(teamsMock).toHaveBeenCalled();
  114. });
  115. it('shows loading error for non-superusers on 403s', async function () {
  116. getOrgMock = MockApiClient.addMockResponse({
  117. url: '/organizations/org-slug/',
  118. statusCode: 403,
  119. });
  120. jest.spyOn(console, 'error').mockImplementation(jest.fn()); // eslint-disable-line no-console
  121. renderComponent();
  122. expect(
  123. await screen.findByText('There was an error loading data.')
  124. ).toBeInTheDocument();
  125. // eslint-disable-next-line no-console
  126. expect(console.error).toHaveBeenCalled();
  127. });
  128. it('opens sudo modal for superusers on 403s', async function () {
  129. const openSudoSpy = jest.spyOn(openSudo, 'openSudo');
  130. jest
  131. .mocked(ConfigStore.get)
  132. .mockImplementation(() => TestStubs.Config({isSuperuser: true}));
  133. getOrgMock = MockApiClient.addMockResponse({
  134. url: '/organizations/org-slug/',
  135. statusCode: 403,
  136. });
  137. renderComponent();
  138. await waitFor(() => expect(openSudoSpy).toHaveBeenCalled());
  139. });
  140. it('uses last organization from ConfigStore', function () {
  141. getOrgMock = MockApiClient.addMockResponse({
  142. url: '/organizations/last-org/',
  143. body: organization,
  144. });
  145. MockApiClient.addMockResponse({
  146. url: '/organizations/last-org/projects/',
  147. body: projects,
  148. });
  149. MockApiClient.addMockResponse({
  150. url: '/organizations/last-org/teams/',
  151. body: teams,
  152. });
  153. // mocking `.get('lastOrganization')`
  154. jest.mocked(ConfigStore.get).mockImplementation(() => 'last-org');
  155. renderComponent({useLastOrganization: true, params: {orgId: ''}});
  156. expect(getOrgMock).toHaveBeenLastCalledWith(
  157. '/organizations/last-org/',
  158. expect.anything()
  159. );
  160. });
  161. it('uses last organization from `organizations` prop', async function () {
  162. MockApiClient.addMockResponse({
  163. url: '/organizations/foo/environments/',
  164. body: TestStubs.Environments(),
  165. });
  166. getOrgMock = MockApiClient.addMockResponse({
  167. url: '/organizations/foo/',
  168. body: organization,
  169. });
  170. getProjectsMock = MockApiClient.addMockResponse({
  171. url: '/organizations/foo/projects/',
  172. body: projects,
  173. });
  174. getTeamsMock = MockApiClient.addMockResponse({
  175. url: '/organizations/foo/teams/',
  176. body: teams,
  177. });
  178. jest.mocked(ConfigStore.get).mockImplementation(() => '');
  179. const {rerender} = renderComponent({
  180. params: {orgId: ''},
  181. useLastOrganization: true,
  182. organizationsLoading: true,
  183. organizations: [],
  184. });
  185. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  186. rerender(
  187. makeComponent({
  188. params: {orgId: ''},
  189. useLastOrganization: true,
  190. organizationsLoading: false,
  191. organizations: [Organization({slug: 'foo'}), Organization({slug: 'bar'})],
  192. })
  193. );
  194. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  195. expect(getOrgMock).toHaveBeenCalled();
  196. expect(getProjectsMock).toHaveBeenCalled();
  197. expect(getTeamsMock).toHaveBeenCalled();
  198. });
  199. it('uses last organization when no orgId in URL - and fetches org details once', async function () {
  200. jest.mocked(ConfigStore.get).mockImplementation(() => 'my-last-org');
  201. getOrgMock = MockApiClient.addMockResponse({
  202. url: '/organizations/my-last-org/',
  203. body: Organization({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. const {rerender} = renderComponent({
  214. params: {orgId: ''},
  215. useLastOrganization: true,
  216. organizations: [],
  217. });
  218. expect(await screen.findByText('my-last-org')).toBeInTheDocument();
  219. expect(getOrgMock).toHaveBeenCalledTimes(1);
  220. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  221. // org details fetch
  222. rerender(
  223. makeComponent({
  224. params: {orgId: ''},
  225. useLastOrganization: true,
  226. organizationsLoading: false,
  227. organizations: [Organization({slug: 'foo'}), Organization({slug: 'bar'})],
  228. })
  229. );
  230. expect(getOrgMock).toHaveBeenCalledTimes(1);
  231. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  232. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  233. });
  234. it('fetches org details only once if organizations loading store changes', async function () {
  235. const {rerender} = renderComponent({
  236. params: {orgId: 'org-slug'},
  237. organizationsLoading: true,
  238. organizations: [],
  239. });
  240. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  241. expect(getOrgMock).toHaveBeenCalledTimes(1);
  242. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  243. // org details fetch
  244. rerender(
  245. makeComponent({
  246. params: {orgId: 'org-slug'},
  247. organizationsLoading: false,
  248. organizations: [Organization({slug: 'foo'}), Organization({slug: 'bar'})],
  249. })
  250. );
  251. expect(getOrgMock).toHaveBeenCalledTimes(1);
  252. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  253. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  254. });
  255. });