initializeOrg.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type {RouteComponent, RouteComponentProps} from 'react-router';
  2. import type {Location} from 'history';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {OrgRoleListFixture, TeamRoleListFixture} from 'sentry-fixture/roleList';
  6. import {RouterContextFixture} from 'sentry-fixture/routerContextFixture';
  7. import {RouterFixture} from 'sentry-fixture/routerFixture';
  8. import type {Organization as TOrganization, Project} from 'sentry/types';
  9. // Workaround react-router PlainRoute type not covering redirect routes.
  10. type RouteShape = {
  11. childRoutes?: RouteShape[];
  12. component?: RouteComponent;
  13. from?: string;
  14. indexRoute?: RouteShape;
  15. name?: string;
  16. path?: string;
  17. };
  18. interface InitializeOrgOptions<RouterParams> {
  19. organization?: Partial<TOrganization>;
  20. project?: Partial<Project>;
  21. projects?: Partial<Project>[];
  22. router?: {
  23. location?: Partial<Location>;
  24. params?: RouterParams;
  25. push?: jest.Mock;
  26. routes?: RouteShape[];
  27. };
  28. }
  29. /**
  30. * Creates stubs for:
  31. * - a project or projects
  32. * - organization owning above projects
  33. * - router
  34. * - context that contains org + projects + router
  35. */
  36. export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
  37. organization: additionalOrg,
  38. project: additionalProject,
  39. projects: additionalProjects,
  40. router: additionalRouter,
  41. }: InitializeOrgOptions<RouterParams> = {}) {
  42. const projects = (
  43. additionalProjects ||
  44. (additionalProject && [additionalProject]) || [{}]
  45. ).map(p => ProjectFixture(p));
  46. const [project] = projects;
  47. const organization = OrganizationFixture({
  48. projects,
  49. ...additionalOrg,
  50. orgRoleList: OrgRoleListFixture(),
  51. teamRoleList: TeamRoleListFixture(),
  52. });
  53. const router = RouterFixture({
  54. ...additionalRouter,
  55. params: {
  56. orgId: organization.slug,
  57. projectId: projects[0]?.slug,
  58. ...additionalRouter?.params,
  59. },
  60. });
  61. const routerContext: any = RouterContextFixture([
  62. {
  63. organization,
  64. project,
  65. router,
  66. location: router.location,
  67. },
  68. ]);
  69. /**
  70. * A collection of router props that are passed to components by react-router
  71. *
  72. * Pass custom router params like so:
  73. * ```ts
  74. * initializeOrg({router: {params: {alertId: '123'}}})
  75. * ```
  76. */
  77. const routerProps: RouteComponentProps<RouterParams, {}> = {
  78. params: router.params as any,
  79. routeParams: router.params,
  80. router,
  81. route: router.routes[0],
  82. routes: router.routes,
  83. location: routerContext.context.location,
  84. };
  85. return {
  86. organization,
  87. project,
  88. projects,
  89. router,
  90. routerContext,
  91. routerProps,
  92. // @deprecated - not sure what purpose this serves
  93. route: {},
  94. };
  95. }