From 91fb4600e7d893de01a07abdf2a7e0e95a5b6553 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 23 Aug 2022 17:07:33 +0200 Subject: [PATCH] core-app-api: add isReactRouterBeta and select traversal mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: blam Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- packages/core-app-api/src/app/AppManager.tsx | 5 ++- .../src/app/isReactRouterBeta.test.tsx | 33 +++++++++++++++++++ .../src/app/isReactRouterBeta.tsx | 23 +++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 packages/core-app-api/src/app/isReactRouterBeta.test.tsx create mode 100644 packages/core-app-api/src/app/isReactRouterBeta.tsx diff --git a/packages/core-app-api/src/app/AppManager.tsx b/packages/core-app-api/src/app/AppManager.tsx index 364f4f8d70..89402476e3 100644 --- a/packages/core-app-api/src/app/AppManager.tsx +++ b/packages/core-app-api/src/app/AppManager.tsx @@ -81,6 +81,7 @@ import { defaultConfigLoader } from './defaultConfigLoader'; import { ApiRegistry } from '../apis/system/ApiRegistry'; import { resolveRouteBindings } from './resolveRouteBindings'; import { BackstageRouteObject } from '../routing/types'; +import { isReactRouterBeta } from './isReactRouterBeta'; type CompatiblePlugin = | BackstagePlugin @@ -224,7 +225,9 @@ export class AppManager implements BackstageApp { root: children, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routing: routingV2Collector, + routing: isReactRouterBeta() + ? routingV1Collector + : routingV2Collector, collectedPlugins: pluginCollector, featureFlags: featureFlagCollector, }, diff --git a/packages/core-app-api/src/app/isReactRouterBeta.test.tsx b/packages/core-app-api/src/app/isReactRouterBeta.test.tsx new file mode 100644 index 0000000000..ce026b7849 --- /dev/null +++ b/packages/core-app-api/src/app/isReactRouterBeta.test.tsx @@ -0,0 +1,33 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +describe.each(['beta', 'stable'])('react-router %s', rrVersion => { + it('should return the correct value for the different version', () => { + jest.isolateModules(() => { + jest.doMock('react-router-dom', () => + rrVersion === 'beta' + ? jest.requireActual('react-router-dom-beta') + : jest.requireActual('react-router-dom-stable'), + ); + + const { isReactRouterBeta } = require('./isReactRouterBeta'); + expect(isReactRouterBeta()).toBe(rrVersion === 'beta'); + }); + }); +}); + +// eslint-disable-next-line jest/no-export +export {}; diff --git a/packages/core-app-api/src/app/isReactRouterBeta.tsx b/packages/core-app-api/src/app/isReactRouterBeta.tsx new file mode 100644 index 0000000000..bef4f8521f --- /dev/null +++ b/packages/core-app-api/src/app/isReactRouterBeta.tsx @@ -0,0 +1,23 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { createRoutesFromChildren, Route } from 'react-router-dom'; + +export function isReactRouterBeta(): boolean { + const [obj] = createRoutesFromChildren(} />); + return !obj.index; +}