Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the relative path bug #261

Merged
merged 6 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions core/examples/luigi-sample-angular/e2e/tests/navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('Navigation', () => {
});
};

//go to absolute path
goToAnotherNodeFeature();
cy.get('.fd-app__sidebar .fd-side-nav__item')
.contains('Go to absolute path')
Expand All @@ -106,6 +107,7 @@ describe('Navigation', () => {
expect(loc.hash).to.eq('#/settings');
});

//go to relative path from the parent node
goToAnotherNodeFeature();
cy.get('.fd-app__sidebar .fd-side-nav__item')
.contains('Go to relative path')
Expand All @@ -114,6 +116,24 @@ describe('Navigation', () => {
cy.location().should(loc => {
expect(loc.hash).to.eq('#/projects/pr2/dps/dps1');
});

//go to relative path from node that is a sibiling
goToAnotherNodeFeature();
cy.get('.fd-app__sidebar .fd-side-nav__item')
.contains('Keep Selected Example')
.click();

cy.location().should(loc => {
expect(loc.hash).to.eq('#/projects/pr2/avengers');
});

cy.get('.fd-app__sidebar .fd-side-nav__item')
.contains('Go to relative path')
.click();

cy.location().should(loc => {
expect(loc.hash).to.eq('#/projects/pr2/dps/dps1');
});
});
});
});
20 changes: 14 additions & 6 deletions core/src/services/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,20 @@ export const navigateTo = async route => {
window.dispatchEvent(event);
};

export const buildFromRelativePath = path => {
if (LuigiConfig.getConfigValue('routing.useHashRouting')) {
return window.location.hash + '/' + path;
} else {
return window.location.pathname + '/' + path;
export const buildFromRelativePath = node => {
Copy link
Contributor

@pekura pekura Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is safer to align the path by number of path segments. My suggestion would be:

export const buildFromRelativePath = node => {
  let windowPath = LuigiConfig.getConfigValue('routing.useHashRouting')
    ? getPathWithoutHash(window.location.hash)
    : window.location.pathname;
  if (node.parent && node.parent.pathSegment) {
    const nodePathSegments = trimLeadingSlash(getNodePath(node.parent)).split('/');
    const windowPathSegments = trimLeadingSlash(windowPath).split('/');
    if (windowPathSegments.length > nodePathSegments.length) {
      windowPath = windowPathSegments.slice(0, nodePathSegments.length).join('/');
    }
  }
  return addLeadingSlash(concatenatePath(windowPath, node.link));
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion here, for consistency and avoiding issues with leading / trailing slashes in the future or between routing strategies, I would use helper methods to normalize nodePathSegments and windowPathSegments before applying split, something like:

const nodePathSegments = Helpers.normalizePath(getNodePath(node.parent)).split('/');
const windowPathSegments = Helpers.normalizePath(windowPath).split('/');

Also what is being checked in if (windowPathSegments.length > nodePathSegments.length - 1) is at least for me not straightforward to understand, maybe a variable with a meaningful name would be helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed the '-1', it was not necessary with properly prepared paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied suggested changes :)
The only difference is that instead of
windowPath = windowPathSegments.slice(0, nodePathSegments.length).join('/');
I used
windowPath = nodePathSegments.join('/');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather stick to windowPath = windowPathSegments.slice(0, nodePathSegments.length).join('/'); because of dynamic path segments. It might work now but in the future after a planned refactoring (because of mutating the navigation data structure that we do not want to happen) it will get broken for sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I reverted my latest commit.

let windowPath = LuigiConfig.getConfigValue('routing.useHashRouting')
? window.location.hash
: window.location.pathname;
if (node.parent && node.parent.pathSegment) {
// use only this part of the current path that refers to the parent of the node (remove additional parts refering to the sibiling)
// remove everything that is after the last occurance of the parents pathSegment 'parent/keepSelectedForChildren/something' -> 'parent'
windowPath = windowPath.substr(
0,
windowPath.lastIndexOf(node.parent.pathSegment) +
node.parent.pathSegment.length
);
}
return windowPath + '/' + node.link;
};

export const handleRouteClick = node => {
Expand All @@ -473,7 +481,7 @@ export const handleRouteClick = node => {
} else if (node.link) {
const link = node.link.startsWith('/')
? node.link
: buildFromRelativePath(node.link);
: buildFromRelativePath(node);
navigateTo(link);
return;
} else {
Expand Down
39 changes: 39 additions & 0 deletions core/test/services/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,45 @@ describe('Routing', () => {
});
});

describe('#buildFromRelativePath()', () => {
beforeEach(() => {
window.dispatchEvent = sinon.spy();
});

const nodeWithParent = {
link: 'child-node',
parent: {
pathSegment: 'parent-node'
}
};

it('should return proper hash route', () => {
// given
const expectedRoute = '#/parent-node/child-node';
LuigiConfig.getConfigValue.returns(true);

// when
window.location.hash = '#/parent-node';
const route = routing.buildFromRelativePath(nodeWithParent);

// then
assert.equal(route, expectedRoute);
});

it("should return proper hash route even if it's relative to a different node in the tree than the current one", () => {
// given
const expectedRoute = '#/parent-node/child-node';
LuigiConfig.getConfigValue.returns(true);

// when
window.location.hash = '#/parent-node/different-node';
const route = routing.buildFromRelativePath(nodeWithParent);

// then
assert.equal(route, expectedRoute);
});
});

describe('#handleRouteClick()', () => {
beforeEach(() => {
window.dispatchEvent = sinon.spy();
Expand Down