initializeOrg.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import type {InjectedRouter, PlainRoute, 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 {RouterFixture} from 'sentry-fixture/routerFixture';
  7. import type {Organization} from 'sentry/types/organization';
  8. import type {Project} from 'sentry/types/project';
  9. interface RouteWithName extends PlainRoute {
  10. name?: string;
  11. }
  12. interface PartialInjectedRouter<P>
  13. extends Partial<Omit<InjectedRouter<P>, 'location' | 'routes'>> {
  14. location?: Partial<Location>;
  15. routes?: RouteWithName[];
  16. }
  17. interface InitializeOrgOptions<RouterParams> {
  18. organization?: Partial<Organization>;
  19. project?: Partial<Project>;
  20. projects?: Partial<Project>[];
  21. router?: PartialInjectedRouter<RouterParams>;
  22. }
  23. /**
  24. * Creates stubs for:
  25. * - a project or projects
  26. * - organization owning above projects
  27. * - router
  28. * - context that contains router
  29. */
  30. export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
  31. organization: additionalOrg,
  32. projects: additionalProjects,
  33. router: additionalRouter,
  34. }: InitializeOrgOptions<RouterParams> = {}) {
  35. const projects = (additionalProjects || [{}]).map(p => ProjectFixture(p));
  36. const [project] = projects;
  37. const organization = OrganizationFixture({
  38. projects,
  39. ...additionalOrg,
  40. orgRoleList: OrgRoleListFixture(),
  41. teamRoleList: TeamRoleListFixture(),
  42. });
  43. const router = RouterFixture({
  44. ...additionalRouter,
  45. params: {
  46. orgId: organization.slug,
  47. projectId: projects[0]?.slug,
  48. ...additionalRouter?.params,
  49. },
  50. });
  51. /**
  52. * A collection of router props that are passed to components by react-router
  53. *
  54. * Pass custom router params like so:
  55. * ```ts
  56. * initializeOrg({router: {params: {alertId: '123'}}})
  57. * ```
  58. */
  59. const routerProps: RouteComponentProps<RouterParams, {}> = {
  60. params: router.params as any,
  61. routeParams: router.params,
  62. router,
  63. route: router.routes[0],
  64. routes: router.routes,
  65. location: router.location,
  66. };
  67. return {
  68. organization,
  69. project,
  70. projects,
  71. router,
  72. routerProps,
  73. };
  74. }