Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(routes): childRoutes are not required for root module (#657)
Browse files Browse the repository at this point in the history
* feat(routes): childRoutes are not required for root module

* feat(routes): removed duplicate test and refactored moduleRoute in createRoute

* feat(routes): updated test case with mocked hasChildRoutes

* feat(routes): updated test case description
  • Loading branch information
bishnubista authored Jan 26, 2022
1 parent 7fc5e19 commit 0926fb2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
18 changes: 14 additions & 4 deletions __tests__/universal/routes.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ReactTestUtils from 'react-dom/test-utils';
import { fromJS } from 'immutable';

import createRoutes from '../../src/universal/routes';
import hasChildRoutes from '../../src/universal/utils/hasChildRoutes';

jest.mock('holocron-module-route', () => () => null);
jest.mock('@americanexpress/one-app-ducks/lib/errorReporting', () => ({
Expand All @@ -27,6 +28,7 @@ jest.mock('@americanexpress/one-app-ducks/lib/errorReporting', () => ({
meta,
})),
}));
jest.mock('../../src/universal/utils/hasChildRoutes');

describe('routes', () => {
const store = {
Expand All @@ -44,15 +46,23 @@ describe('routes', () => {

beforeEach(() => jest.clearAllMocks());

it('should set up the root module route first', () => {
it('should return array of length 2', () => {
const RootRoute = createRoutes(store);
expect(RootRoute.length).toBe(2);
});

it('should return RootRoute props with path if RootModule has no childroutes', () => {
hasChildRoutes.mockReturnValueOnce(false);
const RootRoute = createRoutes(store)[0];
expect(ReactTestUtils.isElement(RootRoute)).toBe(true);
expect(RootRoute.props).toEqual({ moduleName: 'fakeRootModule', store });
expect(RootRoute.props).toEqual({ moduleName: 'fakeRootModule', path: '/', store });
});

it('should not define a path on the root module', () => {
it('should return RootRoute props without path if RootModule has childroutes', () => {
hasChildRoutes.mockReturnValueOnce(true);
const RootRoute = createRoutes(store)[0];
expect(RootRoute.props.path).toBe(undefined);
expect(ReactTestUtils.isElement(RootRoute)).toBe(true);
expect(RootRoute.props).toEqual({ moduleName: 'fakeRootModule', store });
});

it('should set up a default 404', () => {
Expand Down
27 changes: 27 additions & 0 deletions __tests__/universal/utils/hasChildRoutes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 American Express Travel Related Services Company, Inc.
*
* 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 hasChildRoutes from '../../../src/universal/utils/hasChildRoutes';

it('hasChildRoutes returns false', () => {
[undefined, null, '', [], {}].forEach((value) => {
expect(hasChildRoutes(value)).toBeFalsy();
});
});

it('hasChildRoutes returns true', () => {
expect(hasChildRoutes({ childRoutes: [] })).toBe(true);
});
2 changes: 1 addition & 1 deletion docs/api/modules/Routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Enables components and modules to define their own child routes. `childRoutes` c
route, array of routes or a function which accepts the Redux store as the sole argument and returns
a single route or array of routes.

One App requires the Root Module to have at least one child route defined. This can be either `<Route>` or `<ModuleRoute>` with a `path`.
`childRoutes` is optional for Root Module. `childRoutes` can be either `<Route>` or `<ModuleRoute>` with a `path`. If `childRoutes` is not defined, the default path `/` is added to the root route.

```js
const MyModule = () => { /* jsx */ };
Expand Down
6 changes: 5 additions & 1 deletion src/universal/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ import React from 'react';
import ModuleRoute from 'holocron-module-route';
import { Route } from '@americanexpress/one-app-router';
import { applicationError, clearError } from '@americanexpress/one-app-ducks';
import { getModule } from 'holocron';
import hasChildRoutes from './utils/hasChildRoutes';

const createRoutes = (store) => {
const rootModuleName = store.getState().getIn(['config', 'rootModuleName']);
const rootHasChildRoutes = hasChildRoutes(getModule(rootModuleName));

return [
<ModuleRoute moduleName={rootModuleName} store={store} />,
<ModuleRoute moduleName={rootModuleName} store={store} path={rootHasChildRoutes ? undefined : '/'} />,
<Route
path="*"
component={() => 'Not found'}
Expand Down
19 changes: 19 additions & 0 deletions src/universal/utils/hasChildRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2022 American Express Travel Related Services Company, Inc.
*
* 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.
*/

const hasChildRoutes = (module) => module && !!module.childRoutes;

export default hasChildRoutes;

0 comments on commit 0926fb2

Please sign in to comment.