organizationContextContainer.spec.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import LocationFixture from 'sentry-fixture/locationFixture';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {Team} from 'sentry-fixture/team';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import * as OrganizationActionCreator from 'sentry/actionCreators/organization';
  7. import * as openSudo from 'sentry/actionCreators/sudoModal';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import OrganizationStore from 'sentry/stores/organizationStore';
  10. import ProjectsStore from 'sentry/stores/projectsStore';
  11. import TeamStore from 'sentry/stores/teamStore';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import {OrganizationLegacyContext} from 'sentry/views/organizationContextContainer';
  14. jest.mock('sentry/stores/configStore', () => ({
  15. get: jest.fn(),
  16. }));
  17. describe('OrganizationContextContainer', function () {
  18. const {organization, projects, routerProps} = initializeOrg();
  19. const teams = [Team()];
  20. const api = new MockApiClient();
  21. let getOrgMock: jest.Mock;
  22. let getProjectsMock: jest.Mock;
  23. let getTeamsMock: jest.Mock;
  24. function DisplayOrg() {
  25. const contextOrg = useOrganization();
  26. return <div>{contextOrg.slug}</div>;
  27. }
  28. type Props = Partial<React.ComponentProps<typeof OrganizationLegacyContext>>;
  29. function makeComponent(props?: Props) {
  30. return (
  31. <OrganizationLegacyContext
  32. {...routerProps}
  33. api={api}
  34. params={{orgId: 'org-slug'}}
  35. location={LocationFixture({query: {}})}
  36. useLastOrganization={false}
  37. organizationsLoading={false}
  38. organizations={[]}
  39. includeSidebar={false}
  40. {...props}
  41. >
  42. <DisplayOrg />
  43. </OrganizationLegacyContext>
  44. );
  45. }
  46. function renderComponent(props?: Props) {
  47. return render(makeComponent(props));
  48. }
  49. beforeEach(function () {
  50. MockApiClient.clearMockResponses();
  51. getOrgMock = MockApiClient.addMockResponse({
  52. url: '/organizations/org-slug/',
  53. body: organization,
  54. });
  55. getProjectsMock = MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/projects/',
  57. body: projects,
  58. });
  59. getTeamsMock = MockApiClient.addMockResponse({
  60. url: '/organizations/org-slug/teams/',
  61. body: teams,
  62. });
  63. jest.spyOn(TeamStore, 'loadInitialData');
  64. jest.spyOn(ProjectsStore, 'loadInitialData');
  65. jest.spyOn(OrganizationActionCreator, 'fetchOrganizationDetails');
  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 = Organization({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. 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(openSudoSpy).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. });