initializeOrg.tsx 2.4 KB

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