initializeOrg.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type {RouteComponent, RouteComponentProps} from 'react-router';
  2. import type {Location} from 'history';
  3. import {Organization} from 'sentry-fixture/organization';
  4. import {OrgRoleList, TeamRoleList} from 'sentry-fixture/roleList';
  5. import RouterContextFixture from 'sentry-fixture/routerContextFixture';
  6. import type {Organization as TOrganization, Project} from 'sentry/types';
  7. // Workaround react-router PlainRoute type not covering redirect routes.
  8. type RouteShape = {
  9. childRoutes?: RouteShape[];
  10. component?: RouteComponent;
  11. from?: string;
  12. indexRoute?: RouteShape;
  13. name?: string;
  14. path?: string;
  15. };
  16. interface InitializeOrgOptions<RouterParams> {
  17. organization?: Partial<TOrganization>;
  18. project?: Partial<Project>;
  19. projects?: Partial<Project>[];
  20. router?: {
  21. location?: Partial<Location>;
  22. params?: RouterParams;
  23. push?: jest.Mock;
  24. routes?: RouteShape[];
  25. };
  26. }
  27. /**
  28. * Creates stubs for:
  29. * - a project or projects
  30. * - organization owning above projects
  31. * - router
  32. * - context that contains org + projects + router
  33. */
  34. export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
  35. organization: additionalOrg,
  36. project: additionalProject,
  37. projects: additionalProjects,
  38. router: additionalRouter,
  39. }: InitializeOrgOptions<RouterParams> = {}) {
  40. const projects = (
  41. additionalProjects ||
  42. (additionalProject && [additionalProject]) || [{}]
  43. ).map(p => TestStubs.Project(p));
  44. const [project] = projects;
  45. const organization = Organization({
  46. projects,
  47. ...additionalOrg,
  48. orgRoleList: OrgRoleList(),
  49. teamRoleList: TeamRoleList(),
  50. });
  51. const router = TestStubs.router({
  52. ...additionalRouter,
  53. params: {
  54. orgId: organization.slug,
  55. projectId: projects[0]?.slug,
  56. ...additionalRouter?.params,
  57. },
  58. });
  59. const routerContext: any = RouterContextFixture([
  60. {
  61. organization,
  62. project,
  63. router,
  64. location: router.location,
  65. },
  66. ]);
  67. /**
  68. * A collection of router props that are passed to components by react-router
  69. *
  70. * Pass custom router params like so:
  71. * ```ts
  72. * initializeOrg({router: {params: {alertId: '123'}}})
  73. * ```
  74. */
  75. const routerProps: RouteComponentProps<RouterParams, {}> = {
  76. params: router.params as any,
  77. routeParams: router.params,
  78. router,
  79. route: router.routes[0],
  80. routes: router.routes,
  81. location: routerContext.context.location,
  82. };
  83. return {
  84. organization,
  85. project,
  86. projects,
  87. router,
  88. routerContext,
  89. routerProps,
  90. // @deprecated - not sure what purpose this serves
  91. route: {},
  92. };
  93. }