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} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. // Workaround react-router PlainRoute type not covering redirect routes.
  11. type RouteShape = {
  12. childRoutes?: RouteShape[];
  13. component?: RouteComponent;
  14. from?: string;
  15. indexRoute?: RouteShape;
  16. name?: string;
  17. path?: string;
  18. };
  19. interface InitializeOrgOptions<RouterParams> {
  20. organization?: Partial<TOrganization>;
  21. project?: Partial<Project>;
  22. projects?: Partial<Project>[];
  23. router?: {
  24. location?: Partial<Location>;
  25. params?: RouterParams;
  26. push?: jest.Mock;
  27. routes?: RouteShape[];
  28. };
  29. }
  30. /**
  31. * Creates stubs for:
  32. * - a project or projects
  33. * - organization owning above projects
  34. * - router
  35. * - context that contains org + projects + router
  36. */
  37. export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
  38. organization: additionalOrg,
  39. project: additionalProject,
  40. projects: additionalProjects,
  41. router: additionalRouter,
  42. }: InitializeOrgOptions<RouterParams> = {}) {
  43. const projects = (
  44. additionalProjects ||
  45. (additionalProject && [additionalProject]) || [{}]
  46. ).map(p => ProjectFixture(p));
  47. const [project] = projects;
  48. const organization = OrganizationFixture({
  49. projects,
  50. ...additionalOrg,
  51. orgRoleList: OrgRoleListFixture(),
  52. teamRoleList: TeamRoleListFixture(),
  53. });
  54. const router = RouterFixture({
  55. ...additionalRouter,
  56. params: {
  57. orgId: organization.slug,
  58. projectId: projects[0]?.slug,
  59. ...additionalRouter?.params,
  60. },
  61. });
  62. const routerContext: any = RouterContextFixture([
  63. {
  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. }