organizationContext.spec.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {Component} from 'react';
  2. import {RouteContextInterface} from 'react-router';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. import {OrganizationFixture} from 'sentry-fixture/organization';
  5. import {ProjectFixture} from 'sentry-fixture/project';
  6. import {RouterFixture} from 'sentry-fixture/routerFixture';
  7. import {TeamFixture} from 'sentry-fixture/team';
  8. import {UserFixture} from 'sentry-fixture/user';
  9. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  10. import * as orgsActionCreators from 'sentry/actionCreators/organizations';
  11. import {openSudo} from 'sentry/actionCreators/sudoModal';
  12. import {SentryPropTypeValidators} from 'sentry/sentryPropTypeValidators';
  13. import ConfigStore from 'sentry/stores/configStore';
  14. import OrganizationStore from 'sentry/stores/organizationStore';
  15. import ProjectsStore from 'sentry/stores/projectsStore';
  16. import TeamStore from 'sentry/stores/teamStore';
  17. import {Organization} from 'sentry/types';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {OrganizationContextProvider, useEnsureOrganization} from './organizationContext';
  20. import {RouteContext} from './routeContext';
  21. jest.mock('sentry/actionCreators/sudoModal');
  22. describe('OrganizationContext', function () {
  23. let getOrgMock: jest.Mock;
  24. let getProjectsMock: jest.Mock;
  25. let getTeamsMock: jest.Mock;
  26. const organization = OrganizationFixture();
  27. const project = ProjectFixture();
  28. const team = TeamFixture();
  29. const router: RouteContextInterface = {
  30. router: RouterFixture(),
  31. location: LocationFixture(),
  32. routes: [],
  33. params: {orgId: organization.slug},
  34. };
  35. function setupOrgMocks(org: Organization) {
  36. const orgMock = MockApiClient.addMockResponse({
  37. url: `/organizations/${org.slug}/`,
  38. body: org,
  39. });
  40. const projectMock = MockApiClient.addMockResponse({
  41. url: `/organizations/${org.slug}/projects/`,
  42. body: [project],
  43. });
  44. const teamMock = MockApiClient.addMockResponse({
  45. url: `/organizations/${org.slug}/teams/`,
  46. body: [team],
  47. });
  48. return {orgMock, projectMock, teamMock};
  49. }
  50. beforeEach(function () {
  51. MockApiClient.clearMockResponses();
  52. const {orgMock, projectMock, teamMock} = setupOrgMocks(organization);
  53. getOrgMock = orgMock;
  54. getProjectsMock = projectMock;
  55. getTeamsMock = teamMock;
  56. jest.spyOn(TeamStore, 'loadInitialData');
  57. jest.spyOn(ProjectsStore, 'loadInitialData');
  58. ConfigStore.init();
  59. OrganizationStore.reset();
  60. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  61. });
  62. afterEach(function () {
  63. // eslint-disable-next-line no-console
  64. jest.mocked(console.error).mockRestore();
  65. });
  66. function OrganizationLoaderStub() {
  67. useEnsureOrganization();
  68. return null;
  69. }
  70. /**
  71. * Used to test that the organization context is propegated
  72. */
  73. function OrganizationName() {
  74. const org = useOrganization({allowNull: true});
  75. return <div>{org?.slug ?? 'no-org'}</div>;
  76. }
  77. /**
  78. * Used to test the legacy organization context behavior
  79. */
  80. class OrganizationNameLegacyConsumer extends Component {
  81. static contextTypes = {
  82. organization: SentryPropTypeValidators.isOrganization,
  83. };
  84. render() {
  85. return <div>{this.context.organization?.slug ?? 'no-org'}</div>;
  86. }
  87. }
  88. it('fetches org, projects, teams, and provides organization context', async function () {
  89. render(
  90. <OrganizationContextProvider>
  91. <OrganizationLoaderStub />
  92. <OrganizationName />
  93. </OrganizationContextProvider>
  94. );
  95. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  96. expect(getOrgMock).toHaveBeenCalled();
  97. expect(getProjectsMock).toHaveBeenCalled();
  98. expect(getTeamsMock).toHaveBeenCalled();
  99. });
  100. it('provides legacy organization context', async function () {
  101. render(
  102. <OrganizationContextProvider>
  103. <OrganizationLoaderStub />
  104. <OrganizationNameLegacyConsumer />
  105. </OrganizationContextProvider>
  106. );
  107. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  108. });
  109. it('does not fetch if organization is already set', async function () {
  110. OrganizationStore.onUpdate(organization);
  111. render(
  112. <OrganizationContextProvider>
  113. <OrganizationLoaderStub />
  114. <OrganizationName />
  115. </OrganizationContextProvider>
  116. );
  117. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  118. expect(getOrgMock).not.toHaveBeenCalled();
  119. });
  120. it('fetches new org when router params change', async function () {
  121. // First render with org-slug
  122. const {rerender} = render(
  123. <RouteContext.Provider value={router}>
  124. <OrganizationContextProvider>
  125. <OrganizationLoaderStub />
  126. <OrganizationName />
  127. </OrganizationContextProvider>
  128. </RouteContext.Provider>
  129. );
  130. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  131. const anotherOrg = OrganizationFixture({slug: 'another-org'});
  132. const {orgMock, projectMock, teamMock} = setupOrgMocks(anotherOrg);
  133. const switchOrganization = jest.spyOn(orgsActionCreators, 'switchOrganization');
  134. // re-render with another-org
  135. rerender(
  136. <RouteContext.Provider value={{...router, params: {orgId: 'another-org'}}}>
  137. <OrganizationContextProvider>
  138. <OrganizationLoaderStub />
  139. <OrganizationName />
  140. </OrganizationContextProvider>
  141. </RouteContext.Provider>
  142. );
  143. expect(await screen.findByText(anotherOrg.slug)).toBeInTheDocument();
  144. expect(orgMock).toHaveBeenCalled();
  145. expect(projectMock).toHaveBeenCalled();
  146. expect(teamMock).toHaveBeenCalled();
  147. expect(switchOrganization).toHaveBeenCalled();
  148. });
  149. it('opens sudo modal for superusers on 403s', async function () {
  150. ConfigStore.set('user', UserFixture({isSuperuser: true}));
  151. getOrgMock = MockApiClient.addMockResponse({
  152. url: '/organizations/org-slug/',
  153. statusCode: 403,
  154. });
  155. render(
  156. <OrganizationContextProvider>
  157. <OrganizationLoaderStub />
  158. <OrganizationName />
  159. </OrganizationContextProvider>
  160. );
  161. await waitFor(() => !OrganizationStore.getState().loading);
  162. // eslint-disable-next-line no-console
  163. expect(console.error).toHaveBeenCalled();
  164. expect(openSudo).toHaveBeenCalled();
  165. });
  166. /**
  167. * This test will rarely happen since most configurations are now using customer domains
  168. */
  169. it('uses last organization slug from ConfigStore', async function () {
  170. const configStoreOrg = OrganizationFixture({slug: 'config-store-org'});
  171. ConfigStore.set('lastOrganization', configStoreOrg.slug);
  172. const {orgMock, projectMock, teamMock} = setupOrgMocks(configStoreOrg);
  173. // orgId is not present in the router.
  174. render(
  175. <RouteContext.Provider value={{...router, params: {}}}>
  176. <OrganizationContextProvider>
  177. <OrganizationLoaderStub />
  178. <OrganizationName />
  179. </OrganizationContextProvider>
  180. </RouteContext.Provider>
  181. );
  182. expect(await screen.findByText(configStoreOrg.slug)).toBeInTheDocument();
  183. expect(orgMock).toHaveBeenCalled();
  184. expect(projectMock).toHaveBeenCalled();
  185. expect(teamMock).toHaveBeenCalled();
  186. });
  187. });