core-app-api: add isReactRouterBeta and select traversal mode

Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-08-23 17:07:33 +02:00
parent 7642899503
commit 91fb4600e7
3 changed files with 60 additions and 1 deletions
+4 -1
View File
@@ -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,
},
@@ -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 {};
@@ -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(<Route index element={<div />} />);
return !obj.index;
}