initializeOrg.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import type {Location} from 'history';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {RouterFixture} from 'sentry-fixture/routerFixture';
  5. import type {
  6. InjectedRouter,
  7. PlainRoute,
  8. RouteComponentProps,
  9. } from 'sentry/types/legacyReactRouter';
  10. import type {Organization} from 'sentry/types/organization';
  11. import type {Project} from 'sentry/types/project';
  12. interface RouteWithName extends PlainRoute {
  13. name?: string;
  14. }
  15. interface PartialInjectedRouter<P>
  16. extends Partial<Omit<InjectedRouter<P>, 'location' | 'routes'>> {
  17. location?: Partial<Location>;
  18. routes?: RouteWithName[];
  19. }
  20. interface InitializeOrgOptions<RouterParams> {
  21. organization?: Partial<Organization>;
  22. project?: Partial<Project>;
  23. projects?: Partial<Project>[];
  24. router?: PartialInjectedRouter<RouterParams>;
  25. }
  26. /**
  27. * Creates stubs for:
  28. * - a project or projects
  29. * - organization owning above projects
  30. * - router
  31. * - context that contains router
  32. */
  33. export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
  34. organization: additionalOrg,
  35. projects: additionalProjects,
  36. router: additionalRouter,
  37. }: InitializeOrgOptions<RouterParams> = {}) {
  38. const organization = OrganizationFixture(additionalOrg);
  39. const projects = additionalProjects
  40. ? additionalProjects.map(ProjectFixture)
  41. : [ProjectFixture()];
  42. const [project] = projects;
  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. }