Skip to content

Commit

Permalink
refactor: update router logic in AppProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Jun 9, 2023
1 parent d602048 commit 904c59a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/react/AppProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
* @param {Object} [props.store] A redux store.
* @memberof module:React
*/
export default function AppProvider({ store, children }) {
export default function AppProvider({ store, children, wrapInRouter }) {
const [config, setConfig] = useState(getConfig());
const [authenticatedUser, setAuthenticatedUser] = useState(getAuthenticatedUser());
const [locale, setLocale] = useState(getLocale());
Expand Down Expand Up @@ -71,9 +71,11 @@ export default function AppProvider({ store, children }) {
value={appContextValue}
>
<OptionalReduxProvider store={store}>
<Router>
{children}
</Router>
{wrapInRouter ? (
<Router>
{children}
</Router>
) : children}
</OptionalReduxProvider>
</AppContext.Provider>
</ErrorBoundary>
Expand All @@ -85,8 +87,10 @@ AppProvider.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
store: PropTypes.object,
children: PropTypes.node.isRequired,
wrapInRouter: PropTypes.bool,
};

AppProvider.defaultProps = {
store: null,
wrapInRouter: true,
};
23 changes: 22 additions & 1 deletion src/react/AppProvider.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { createStore } from 'redux';
import { mount } from 'enzyme';
import { BrowserRouter as Router } from 'react-router-dom';
import AppProvider from './AppProvider';
import { initialize } from '../initialize';

Expand Down Expand Up @@ -48,7 +49,7 @@ describe('AppProvider', () => {
});
});

it('should render its children', () => {
it('should render its children with a router', () => {
const component = (
<AppProvider store={createStore(state => state)}>
<div>Child One</div>
Expand All @@ -58,6 +59,26 @@ describe('AppProvider', () => {

const wrapper = mount(component);
const list = wrapper.find('div');
expect(wrapper.find(Router).length).toEqual(1);
expect(list.length).toEqual(2);
expect(list.at(0).text()).toEqual('Child One');
expect(list.at(1).text()).toEqual('Child Two');

const reduxProvider = wrapper.find('Provider');
expect(reduxProvider.length).toEqual(1);
});

it('should render its children without a router', () => {
const component = (
<AppProvider store={createStore(state => state)} wrapInRouter={false}>
<div>Child One</div>
<div>Child Two</div>
</AppProvider>
);

const wrapper = mount(component);
const list = wrapper.find('div');
expect(wrapper.find(Router).length).toEqual(0);
expect(list.length).toEqual(2);
expect(list.at(0).text()).toEqual('Child One');
expect(list.at(1).text()).toEqual('Child Two');
Expand Down

0 comments on commit 904c59a

Please sign in to comment.