Browse Source

test(js): Simplify types in initializeOrg (#72243)

Evan Purkhiser 9 months ago
parent
commit
953f64aa90
2 changed files with 18 additions and 21 deletions
  1. 12 17
      tests/js/sentry-test/initializeOrg.tsx
  2. 6 4
      tests/js/sentry-test/reactTestingLibrary.tsx

+ 12 - 17
tests/js/sentry-test/initializeOrg.tsx

@@ -1,33 +1,28 @@
-import type {RouteComponent, RouteComponentProps} from 'react-router';
+import type {InjectedRouter, PlainRoute, RouteComponentProps} from 'react-router';
 import type {Location} from 'history';
 import {OrganizationFixture} from 'sentry-fixture/organization';
 import {ProjectFixture} from 'sentry-fixture/project';
 import {OrgRoleListFixture, TeamRoleListFixture} from 'sentry-fixture/roleList';
 import {RouterFixture} from 'sentry-fixture/routerFixture';
 
-import type {Organization as TOrganization} from 'sentry/types/organization';
+import type {Organization} from 'sentry/types/organization';
 import type {Project} from 'sentry/types/project';
 
-// Workaround react-router PlainRoute type not covering redirect routes.
-type RouteShape = {
-  childRoutes?: RouteShape[];
-  component?: RouteComponent;
-  from?: string;
-  indexRoute?: RouteShape;
+interface RouteWithName extends PlainRoute {
   name?: string;
-  path?: string;
-};
+}
+
+interface PartialInjectedRouter<P>
+  extends Partial<Omit<InjectedRouter<P>, 'location' | 'routes'>> {
+  location?: Partial<Location>;
+  routes?: RouteWithName[];
+}
 
 interface InitializeOrgOptions<RouterParams> {
-  organization?: Partial<TOrganization>;
+  organization?: Partial<Organization>;
   project?: Partial<Project>;
   projects?: Partial<Project>[];
-  router?: {
-    location?: Partial<Location>;
-    params?: RouterParams;
-    push?: jest.Mock;
-    routes?: RouteShape[];
-  };
+  router?: PartialInjectedRouter<RouterParams>;
 }
 
 /**

+ 6 - 4
tests/js/sentry-test/reactTestingLibrary.tsx

@@ -32,8 +32,11 @@ interface ProviderOptions {
 
 interface Options extends ProviderOptions, rtl.RenderOptions {}
 
-function makeAllTheProviders(initializeOrgOptions: ProviderOptions) {
-  const {organization, router} = initializeOrg(initializeOrgOptions as any);
+function makeAllTheProviders(providers: ProviderOptions) {
+  const {organization, router} = initializeOrg({
+    organization: providers.organization === null ? undefined : providers.organization,
+    router: providers.router,
+  });
 
   class LegacyRouterProvider extends Component<{children?: React.ReactNode}> {
     static childContextTypes = {
@@ -50,8 +53,7 @@ function makeAllTheProviders(initializeOrgOptions: ProviderOptions) {
   }
 
   // In some cases we may want to not provide an organization at all
-  const optionalOrganization =
-    initializeOrgOptions.organization === null ? null : organization;
+  const optionalOrganization = providers.organization === null ? null : organization;
 
   return function ({children}: {children?: React.ReactNode}) {
     return (