initializeOrg.tsx 2.4 KB

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