organizationContextContainer.spec.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 {MockConfig} from 'sentry/types/__fixtures__/MockConfig';
  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 = TestStubs.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.mocked(ConfigStore.get).mockImplementation(
  132. key =>
  133. MockConfig({
  134. user: TestStubs.User({isSuperuser: true}),
  135. })[key]
  136. );
  137. getOrgMock = MockApiClient.addMockResponse({
  138. url: '/organizations/org-slug/',
  139. statusCode: 403,
  140. });
  141. renderComponent();
  142. await waitFor(() => expect(openSudo).toHaveBeenCalled());
  143. });
  144. it('uses last organization from ConfigStore', function () {
  145. getOrgMock = MockApiClient.addMockResponse({
  146. url: '/organizations/last-org/',
  147. body: organization,
  148. });
  149. MockApiClient.addMockResponse({
  150. url: '/organizations/last-org/projects/',
  151. body: projects,
  152. });
  153. MockApiClient.addMockResponse({
  154. url: '/organizations/last-org/teams/',
  155. body: teams,
  156. });
  157. // mocking `.get('lastOrganization')`
  158. jest.mocked(ConfigStore.get).mockImplementation(() => 'last-org');
  159. renderComponent({useLastOrganization: true, params: {orgId: ''}});
  160. expect(getOrgMock).toHaveBeenLastCalledWith(
  161. '/organizations/last-org/',
  162. expect.anything()
  163. );
  164. });
  165. it('uses last organization from `organizations` prop', async function () {
  166. MockApiClient.addMockResponse({
  167. url: '/organizations/foo/environments/',
  168. body: TestStubs.Environments(),
  169. });
  170. getOrgMock = MockApiClient.addMockResponse({
  171. url: '/organizations/foo/',
  172. body: organization,
  173. });
  174. getProjectsMock = MockApiClient.addMockResponse({
  175. url: '/organizations/foo/projects/',
  176. body: projects,
  177. });
  178. getTeamsMock = MockApiClient.addMockResponse({
  179. url: '/organizations/foo/teams/',
  180. body: teams,
  181. });
  182. jest.mocked(ConfigStore.get).mockImplementation(() => '');
  183. const {rerender} = renderComponent({
  184. params: {orgId: ''},
  185. useLastOrganization: true,
  186. organizationsLoading: true,
  187. organizations: [],
  188. });
  189. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  190. rerender(
  191. makeComponent({
  192. params: {orgId: ''},
  193. useLastOrganization: true,
  194. organizationsLoading: false,
  195. organizations: [
  196. TestStubs.Organization({slug: 'foo'}),
  197. TestStubs.Organization({slug: 'bar'}),
  198. ],
  199. })
  200. );
  201. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  202. expect(getOrgMock).toHaveBeenCalled();
  203. expect(getProjectsMock).toHaveBeenCalled();
  204. expect(getTeamsMock).toHaveBeenCalled();
  205. });
  206. it('uses last organization when no orgId in URL - and fetches org details once', async function () {
  207. jest.mocked(ConfigStore.get).mockImplementation(() => 'my-last-org');
  208. getOrgMock = MockApiClient.addMockResponse({
  209. url: '/organizations/my-last-org/',
  210. body: TestStubs.Organization({slug: 'my-last-org'}),
  211. });
  212. getProjectsMock = MockApiClient.addMockResponse({
  213. url: '/organizations/my-last-org/projects/',
  214. body: projects,
  215. });
  216. getTeamsMock = MockApiClient.addMockResponse({
  217. url: '/organizations/my-last-org/teams/',
  218. body: teams,
  219. });
  220. const {rerender} = renderComponent({
  221. params: {orgId: ''},
  222. useLastOrganization: true,
  223. organizations: [],
  224. });
  225. expect(await screen.findByText('my-last-org')).toBeInTheDocument();
  226. expect(getOrgMock).toHaveBeenCalledTimes(1);
  227. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  228. // org details fetch
  229. rerender(
  230. makeComponent({
  231. params: {orgId: ''},
  232. useLastOrganization: true,
  233. organizationsLoading: false,
  234. organizations: [
  235. TestStubs.Organization({slug: 'foo'}),
  236. TestStubs.Organization({slug: 'bar'}),
  237. ],
  238. })
  239. );
  240. expect(getOrgMock).toHaveBeenCalledTimes(1);
  241. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  242. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  243. });
  244. it('fetches org details only once if organizations loading store changes', async function () {
  245. const {rerender} = renderComponent({
  246. params: {orgId: 'org-slug'},
  247. organizationsLoading: true,
  248. organizations: [],
  249. });
  250. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  251. expect(getOrgMock).toHaveBeenCalledTimes(1);
  252. // Simulate OrganizationsStore being loaded *after* `OrganizationContext` finishes
  253. // org details fetch
  254. rerender(
  255. makeComponent({
  256. params: {orgId: 'org-slug'},
  257. organizationsLoading: false,
  258. organizations: [
  259. TestStubs.Organization({slug: 'foo'}),
  260. TestStubs.Organization({slug: 'bar'}),
  261. ],
  262. })
  263. );
  264. expect(getOrgMock).toHaveBeenCalledTimes(1);
  265. expect(getProjectsMock).toHaveBeenCalledTimes(1);
  266. expect(getTeamsMock).toHaveBeenCalledTimes(1);
  267. });
  268. });