organizationContextContainer.spec.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {openSudo} from 'sentry/actionCreators/modal';
  4. import * as OrganizationActionCreator from 'sentry/actionCreators/organization';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {OrganizationLegacyContext} from 'sentry/views/organizationContextContainer';
  11. jest.mock('sentry/stores/configStore', () => ({
  12. get: jest.fn(),
  13. }));
  14. jest.mock('sentry/actionCreators/modal', () => ({
  15. openSudo: jest.fn(),
  16. }));
  17. describe('OrganizationContextContainer', function () {
  18. const {organization, projects, routerProps} = initializeOrg();
  19. const teams = [TestStubs.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={TestStubs.location({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 = TestStubs.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. 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(openSudo).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: [
  192. TestStubs.Organization({slug: 'foo'}),
  193. TestStubs.Organization({slug: 'bar'}),
  194. ],
  195. })
  196. );
  197. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  198. expect(getOrgMock).toHaveBeenCalled();
  199. expect(getProjectsMock).toHaveBeenCalled();
  200. expect(getTeamsMock).toHaveBeenCalled();
  201. });
  202. it('uses last organization when no orgId in URL - and fetches org details once', async function () {
  203. jest.mocked(ConfigStore.get).mockImplementation(() => 'my-last-org');
  204. getOrgMock = MockApiClient.addMockResponse({
  205. url: '/organizations/my-last-org/',
  206. body: TestStubs.Organization({slug: 'my-last-org'}),
  207. });
  208. getProjectsMock = MockApiClient.addMockResponse({
  209. url: '/organizations/my-last-org/projects/',
  210. body: projects,
  211. });
  212. getTeamsMock = MockApiClient.addMockResponse({
  213. url: '/organizations/my-last-org/teams/',
  214. body: teams,
  215. });
  216. const {rerender} = renderComponent({
  217. params: {orgId: ''},
  218. useLastOrganization: true,
  219. organizations: [],
  220. });
  221. expect(await screen.findByText('my-last-org')).toBeInTheDocument();
  222. expect(getOrgMock).toHaveBeenCalledTimes(1);
  223. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  224. // org details fetch
  225. rerender(
  226. makeComponent({
  227. params: {orgId: ''},
  228. useLastOrganization: true,
  229. organizationsLoading: false,
  230. organizations: [
  231. TestStubs.Organization({slug: 'foo'}),
  232. TestStubs.Organization({slug: 'bar'}),
  233. ],
  234. })
  235. );
  236. expect(getOrgMock).toHaveBeenCalledTimes(1);
  237. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  238. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  239. });
  240. it('fetches org details only once if organizations loading store changes', async function () {
  241. const {rerender} = renderComponent({
  242. params: {orgId: 'org-slug'},
  243. organizationsLoading: true,
  244. organizations: [],
  245. });
  246. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  247. expect(getOrgMock).toHaveBeenCalledTimes(1);
  248. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  249. // org details fetch
  250. rerender(
  251. makeComponent({
  252. params: {orgId: 'org-slug'},
  253. organizationsLoading: false,
  254. organizations: [
  255. TestStubs.Organization({slug: 'foo'}),
  256. TestStubs.Organization({slug: 'bar'}),
  257. ],
  258. })
  259. );
  260. expect(getOrgMock).toHaveBeenCalledTimes(1);
  261. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  262. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  263. });
  264. });