organizationContextContainer.spec.jsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {openSudo} from 'sentry/actionCreators/modal';
  3. import * as OrganizationActionCreator from 'sentry/actionCreators/organization';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import OrganizationStore from 'sentry/stores/organizationStore';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. import TeamStore from 'sentry/stores/teamStore';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import {OrganizationLegacyContext} from 'sentry/views/organizationContextContainer';
  10. jest.mock('sentry/stores/configStore', () => ({
  11. get: jest.fn(),
  12. }));
  13. jest.mock('sentry/actionCreators/modal', () => ({
  14. openSudo: jest.fn(),
  15. }));
  16. describe('OrganizationContextContainer', function () {
  17. const org = TestStubs.Organization();
  18. const teams = [TestStubs.Team()];
  19. const projects = [TestStubs.Project()];
  20. const api = new MockApiClient();
  21. let getOrgMock;
  22. let getProjectsMock;
  23. let getTeamsMock;
  24. function DisplayOrg() {
  25. const contextOrg = useOrganization();
  26. return <div>{contextOrg.slug}</div>;
  27. }
  28. function makeComponent(props) {
  29. return (
  30. <OrganizationLegacyContext
  31. api={api}
  32. params={{orgId: 'org-slug'}}
  33. location={{query: {}}}
  34. routes={[]}
  35. {...props}
  36. >
  37. <DisplayOrg />
  38. </OrganizationLegacyContext>
  39. );
  40. }
  41. function renderComponent(props) {
  42. return render(makeComponent(props));
  43. }
  44. beforeEach(function () {
  45. MockApiClient.clearMockResponses();
  46. getOrgMock = MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/',
  48. body: org,
  49. });
  50. getProjectsMock = MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/projects/',
  52. body: projects,
  53. });
  54. getTeamsMock = MockApiClient.addMockResponse({
  55. url: '/organizations/org-slug/teams/',
  56. body: teams,
  57. });
  58. jest.spyOn(TeamStore, 'loadInitialData');
  59. jest.spyOn(ProjectsStore, 'loadInitialData');
  60. jest.spyOn(OrganizationActionCreator, 'fetchOrganizationDetails');
  61. });
  62. afterEach(function () {
  63. OrganizationStore.reset();
  64. TeamStore.loadInitialData.mockRestore();
  65. ProjectsStore.loadInitialData.mockRestore();
  66. ConfigStore.get.mockRestore();
  67. OrganizationActionCreator.fetchOrganizationDetails.mockRestore();
  68. });
  69. it('renders and fetches org, projects, and teams', async function () {
  70. renderComponent();
  71. await waitFor(() => expect(getOrgMock).toHaveBeenCalled());
  72. expect(getProjectsMock).toHaveBeenCalled();
  73. expect(getTeamsMock).toHaveBeenCalled();
  74. expect(screen.queryByRole('loading-indicator')).not.toBeInTheDocument();
  75. expect(screen.getByText(org.slug)).toBeInTheDocument();
  76. expect(
  77. screen.queryByText('The organization you were looking for was not found.')
  78. ).not.toBeInTheDocument();
  79. expect(TeamStore.loadInitialData).toHaveBeenCalledWith(teams);
  80. expect(ProjectsStore.loadInitialData).toHaveBeenCalledWith(projects);
  81. expect(OrganizationActionCreator.fetchOrganizationDetails).toHaveBeenCalledWith(
  82. api,
  83. 'org-slug',
  84. true,
  85. true
  86. );
  87. });
  88. it('fetches new org when router params change', async function () {
  89. const newOrg = TestStubs.Organization({slug: 'new-slug'});
  90. const {rerender} = renderComponent();
  91. expect(await screen.findByText(org.slug)).toBeInTheDocument();
  92. const mock = MockApiClient.addMockResponse({
  93. url: '/organizations/new-slug/',
  94. body: newOrg,
  95. });
  96. const projectsMock = MockApiClient.addMockResponse({
  97. url: '/organizations/new-slug/projects/',
  98. body: projects,
  99. });
  100. const teamsMock = MockApiClient.addMockResponse({
  101. url: '/organizations/new-slug/teams/',
  102. body: teams,
  103. });
  104. // Re-render with new org slug
  105. rerender(makeComponent({params: {orgId: newOrg.slug}}));
  106. // Loads new org
  107. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  108. // Renders new org
  109. expect(await screen.findByText(newOrg.slug)).toBeInTheDocument();
  110. expect(mock).toHaveBeenLastCalledWith('/organizations/new-slug/', expect.anything());
  111. expect(projectsMock).toHaveBeenCalled();
  112. expect(teamsMock).toHaveBeenCalled();
  113. });
  114. it('shows loading error for non-superusers on 403s', async function () {
  115. getOrgMock = MockApiClient.addMockResponse({
  116. url: '/organizations/org-slug/',
  117. statusCode: 403,
  118. });
  119. jest.spyOn(console, 'error').mockImplementation(jest.fn()); // eslint-disable-line no-console
  120. renderComponent();
  121. expect(
  122. await screen.findByText('There was an error loading data.')
  123. ).toBeInTheDocument();
  124. // eslint-disable-next-line no-console
  125. expect(console.error).toHaveBeenCalled();
  126. // eslint-disable-next-line no-console
  127. console.error.mockRestore();
  128. });
  129. it('opens sudo modal for superusers on 403s', async function () {
  130. ConfigStore.get.mockImplementation(() => ({
  131. isSuperuser: true,
  132. }));
  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: org,
  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. ConfigStore.get.mockImplementation(() => 'last-org');
  155. renderComponent({useLastOrganization: true, params: {}});
  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: org,
  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. 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(org.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. 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: {},
  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: {},
  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(org.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. });