initializeOrg.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. project: additionalProject,
  33. projects: additionalProjects,
  34. router: additionalRouter,
  35. }: InitializeOrgOptions<RouterParams> = {}) {
  36. const projects = (
  37. additionalProjects ||
  38. (additionalProject && [additionalProject]) || [{}]
  39. ).map(p => ProjectFixture(p));
  40. const [project] = projects;
  41. const organization = OrganizationFixture({
  42. projects,
  43. ...additionalOrg,
  44. orgRoleList: OrgRoleListFixture(),
  45. teamRoleList: TeamRoleListFixture(),
  46. });
  47. const router = RouterFixture({
  48. ...additionalRouter,
  49. params: {
  50. orgId: organization.slug,
  51. projectId: projects[0]?.slug,
  52. ...additionalRouter?.params,
  53. },
  54. });
  55. /**
  56. * A collection of router props that are passed to components by react-router
  57. *
  58. * Pass custom router params like so:
  59. * ```ts
  60. * initializeOrg({router: {params: {alertId: '123'}}})
  61. * ```
  62. */
  63. const routerProps: RouteComponentProps<RouterParams, {}> = {
  64. params: router.params as any,
  65. routeParams: router.params,
  66. router,
  67. route: router.routes[0],
  68. routes: router.routes,
  69. location: router.location,
  70. };
  71. return {
  72. organization,
  73. project,
  74. projects,
  75. router,
  76. routerProps,
  77. };
  78. }