initializeOrg.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. organization,
  65. project,
  66. router,
  67. location: router.location,
  68. },
  69. ]);
  70. /**
  71. * A collection of router props that are passed to components by react-router
  72. *
  73. * Pass custom router params like so:
  74. * ```ts
  75. * initializeOrg({router: {params: {alertId: '123'}}})
  76. * ```
  77. */
  78. const routerProps: RouteComponentProps<RouterParams, {}> = {
  79. params: router.params as any,
  80. routeParams: router.params,
  81. router,
  82. route: router.routes[0],
  83. routes: router.routes,
  84. location: routerContext.context.location,
  85. };
  86. return {
  87. organization,
  88. project,
  89. projects,
  90. router,
  91. routerContext,
  92. routerProps,
  93. // @deprecated - not sure what purpose this serves
  94. route: {},
  95. };
  96. }