initializeOrg.tsx 2.5 KB

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