Skip to content

Commit 8e9f213

Browse files
maxmarkuskwiatekus
authored andcommittedDec 4, 2018
Allow async defaultChildNode (SAP#253)
defaultChildNode did not work if children are loaded via function or promise
1 parent 2f97bca commit 8e9f213

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed
 

‎core/src/services/routing.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getConfigValueFromObject,
1010
addLeadingSlash
1111
} from '../utilities/helpers';
12+
import { getConfigValueFromObjectAsync } from '../utilities/async-helpers';
1213

1314
const iframeNavFallbackTimeout = 2000;
1415
let timeoutHandle;
@@ -19,18 +20,19 @@ const getLastNodeObject = pathData => {
1920
return lastElement ? lastElement : {};
2021
};
2122

22-
const getDefaultChildNode = function(pathData) {
23+
const getDefaultChildNode = async function(pathData) {
2324
const lastElement =
2425
pathData.navigationPath[pathData.navigationPath.length - 1];
2526

26-
const pathExists = lastElement.children.find(
27+
const children = await getConfigValueFromObjectAsync(lastElement, 'children');
28+
const pathExists = children.find(
2729
childNode => childNode.pathSegment === lastElement.defaultChildNode
2830
);
2931

3032
if (lastElement.defaultChildNode && pathExists) {
3133
return lastElement.defaultChildNode;
32-
} else if (lastElement.children && lastElement.children.length > 0) {
33-
return lastElement.children[0].pathSegment;
34+
} else if (children && children.length > 0) {
35+
return children[0].pathSegment;
3436
} else {
3537
return '';
3638
}
@@ -322,7 +324,7 @@ export const handleRouteChange = async (path, component, node, config) => {
322324
const routeExists = isExistingRoute(path, pathData);
323325

324326
if (routeExists) {
325-
const defaultChildNode = getDefaultChildNode(pathData);
327+
const defaultChildNode = await getDefaultChildNode(pathData);
326328
navigateTo(`${pathUrl ? `/${pathUrl}` : ''}/${defaultChildNode}`);
327329
} else {
328330
const alert = {

‎core/test/services/routing.spec.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -849,24 +849,52 @@ describe('Routing', () => {
849849
};
850850
};
851851

852-
it('should return first child if no defaultChildNode is set', () => {
852+
it('should return first child if no defaultChildNode is set', async () => {
853853
let pathData = getPathData();
854854

855-
assert.equal(getDefaultChildNode(pathData), 'stakeholders');
855+
assert.equal(await getDefaultChildNode(pathData), 'stakeholders');
856856
});
857857

858-
it('should return child with pathSegment equal to defaultChildNode', () => {
858+
it('should return child with pathSegment equal to defaultChildNode', async () => {
859859
let pathData = getPathData();
860860
pathData.navigationPath[1].defaultChildNode = 'customers';
861861

862-
assert.equal(getDefaultChildNode(pathData), 'customers');
862+
assert.equal(await getDefaultChildNode(pathData), 'customers');
863863
});
864864

865-
it('should return first child if given defaultChildNode does not exist', () => {
865+
it('should return first child if given defaultChildNode does not exist', async () => {
866866
const pathData = getPathData();
867867
pathData.navigationPath[1].defaultChildNode = 'NOSUCHPATH';
868868

869-
assert.equal(getDefaultChildNode(pathData), 'stakeholders');
869+
assert.equal(await getDefaultChildNode(pathData), 'stakeholders');
870+
});
871+
872+
it('should return first child asynchronous if no defaultChildNode is set', async () => {
873+
let pathData = {
874+
navigationPath: [
875+
{
876+
// DOESN'T MATTER
877+
},
878+
{
879+
pathSegment: 'groups',
880+
children: () =>
881+
Promise.resolve([
882+
{
883+
pathSegment: 'stakeholders',
884+
viewUrl:
885+
'/sampleapp.html#/projects/1/users/groups/stakeholders'
886+
},
887+
{
888+
pathSegment: 'customers',
889+
viewUrl: '/sampleapp.html#/projects/1/users/groups/customers'
890+
}
891+
])
892+
}
893+
],
894+
context: {}
895+
};
896+
897+
assert.equal(await getDefaultChildNode(pathData), 'stakeholders');
870898
});
871899
});
872900
});

0 commit comments

Comments
 (0)
Please sign in to comment.