initializeOrg.tsx 2.1 KB

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