organizationContext.spec.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {Component} from 'react';
  2. import type {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 type {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. declare context: {organization: Organization | undefined};
  85. render() {
  86. return <div>{this.context.organization?.slug ?? 'no-org'}</div>;
  87. }
  88. }
  89. it('fetches org, projects, teams, and provides organization context', async function () {
  90. render(
  91. <OrganizationContextProvider>
  92. <OrganizationLoaderStub />
  93. <OrganizationName />
  94. </OrganizationContextProvider>
  95. );
  96. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  97. expect(getOrgMock).toHaveBeenCalled();
  98. expect(getProjectsMock).toHaveBeenCalled();
  99. expect(getTeamsMock).toHaveBeenCalled();
  100. });
  101. it('provides legacy organization context', async function () {
  102. render(
  103. <OrganizationContextProvider>
  104. <OrganizationLoaderStub />
  105. <OrganizationNameLegacyConsumer />
  106. </OrganizationContextProvider>
  107. );
  108. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  109. });
  110. it('does not fetch if organization is already set', async function () {
  111. OrganizationStore.onUpdate(organization);
  112. render(
  113. <OrganizationContextProvider>
  114. <OrganizationLoaderStub />
  115. <OrganizationName />
  116. </OrganizationContextProvider>
  117. );
  118. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  119. expect(getOrgMock).not.toHaveBeenCalled();
  120. });
  121. it('fetches new org when router params change', async function () {
  122. // First render with org-slug
  123. const {rerender} = render(
  124. <RouteContext.Provider value={router}>
  125. <OrganizationContextProvider>
  126. <OrganizationLoaderStub />
  127. <OrganizationName />
  128. </OrganizationContextProvider>
  129. </RouteContext.Provider>
  130. );
  131. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  132. const anotherOrg = OrganizationFixture({slug: 'another-org'});
  133. const {orgMock, projectMock, teamMock} = setupOrgMocks(anotherOrg);
  134. const switchOrganization = jest.spyOn(orgsActionCreators, 'switchOrganization');
  135. // re-render with another-org
  136. rerender(
  137. <RouteContext.Provider value={{...router, params: {orgId: 'another-org'}}}>
  138. <OrganizationContextProvider>
  139. <OrganizationLoaderStub />
  140. <OrganizationName />
  141. </OrganizationContextProvider>
  142. </RouteContext.Provider>
  143. );
  144. expect(await screen.findByText(anotherOrg.slug)).toBeInTheDocument();
  145. expect(orgMock).toHaveBeenCalled();
  146. expect(projectMock).toHaveBeenCalled();
  147. expect(teamMock).toHaveBeenCalled();
  148. expect(switchOrganization).toHaveBeenCalled();
  149. });
  150. it('opens sudo modal for superusers on 403s', async function () {
  151. ConfigStore.set('user', UserFixture({isSuperuser: true}));
  152. getOrgMock = MockApiClient.addMockResponse({
  153. url: '/organizations/org-slug/',
  154. statusCode: 403,
  155. });
  156. render(
  157. <OrganizationContextProvider>
  158. <OrganizationLoaderStub />
  159. <OrganizationName />
  160. </OrganizationContextProvider>
  161. );
  162. await waitFor(() => !OrganizationStore.getState().loading);
  163. // eslint-disable-next-line no-console
  164. await waitFor(() => expect(console.error).toHaveBeenCalled());
  165. expect(openSudo).toHaveBeenCalled();
  166. });
  167. /**
  168. * This test will rarely happen since most configurations are now using customer domains
  169. */
  170. it('uses last organization slug from ConfigStore', async function () {
  171. const configStoreOrg = OrganizationFixture({slug: 'config-store-org'});
  172. ConfigStore.set('lastOrganization', configStoreOrg.slug);
  173. const {orgMock, projectMock, teamMock} = setupOrgMocks(configStoreOrg);
  174. // orgId is not present in the router.
  175. render(
  176. <RouteContext.Provider value={{...router, params: {}}}>
  177. <OrganizationContextProvider>
  178. <OrganizationLoaderStub />
  179. <OrganizationName />
  180. </OrganizationContextProvider>
  181. </RouteContext.Provider>
  182. );
  183. expect(await screen.findByText(configStoreOrg.slug)).toBeInTheDocument();
  184. expect(orgMock).toHaveBeenCalled();
  185. expect(projectMock).toHaveBeenCalled();
  186. expect(teamMock).toHaveBeenCalled();
  187. });
  188. });