Skip to content

Commit

Permalink
Multiple path parameters do not get replaced in view url (#211)
Browse files Browse the repository at this point in the history
Multiple path parameter replacement in the viewUrl fixed.
  • Loading branch information
pekura authored and kwiatekus committed Nov 15, 2018
1 parent aec4a0a commit d77cde4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
24 changes: 12 additions & 12 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions core/src/services/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,30 @@ const escapeRegExp = string => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};

const replaceVars = (viewUrl, params, prefix) => {
const replaceVars = (viewUrl, params, prefix, parenthesis = true) => {
let processedUrl = viewUrl;
if (params) {
Object.entries(params).forEach(entry => {
processedUrl = processedUrl.replace(
new RegExp(escapeRegExp('{' + prefix + entry[0] + '}'), 'g'),
new RegExp(
escapeRegExp(
(parenthesis ? '{' : '') +
prefix +
entry[0] +
(parenthesis ? '}' : '')
),
'g'
),
encodeURIComponent(entry[1])
);
});
}
processedUrl = processedUrl.replace(
new RegExp('\\{' + escapeRegExp(prefix) + '[^\\}]+\\}', 'g'),
''
);
if (parenthesis) {
processedUrl = processedUrl.replace(
new RegExp('\\{' + escapeRegExp(prefix) + '[^\\}]+\\}', 'g'),
''
);
}
return processedUrl;
};

Expand All @@ -141,6 +151,7 @@ const navigateIframe = (config, component, node) => {
const componentData = component.get();
let viewUrl = componentData.viewUrl;
if (viewUrl) {
viewUrl = replaceVars(viewUrl, componentData.pathParams, ':', false);
viewUrl = replaceVars(viewUrl, componentData.context, contextVarPrefix);
viewUrl = replaceVars(
viewUrl,
Expand Down
87 changes: 87 additions & 0 deletions core/test/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ describe('Routing', () => {
viewUrl: 't2.html'
}
]
},
{
pathSegment: 'categories',
children: [
{
pathSegment: ':category',
viewUrl: 'cats/:category#details',
children: [
{
pathSegment: ':sub',
viewUrl: 'cats/:category/:sub'
}
]
}
]
}
],
context: {
Expand Down Expand Up @@ -376,6 +391,78 @@ describe('Routing', () => {
);
});

it('should set component data with path param', async () => {
// given
const path = '#/projects/categories/cat1';
const expectedViewUrl = 'cats/cat1#details';
const mockBrowser = new MockBrowser();
const window = mockBrowser.getWindow();
global.window = window;
const document = mockBrowser.getDocument();
global.document = document;

const node = {
style: {},
prepend: sinon.spy()
};

const config = {
iframe: null,
builderCompatibilityMode: false,
navigateOk: null
};

// when
window.Luigi = {};
window.Luigi.config = sampleLuigiConfig;
const iframeMock = { src: null };
sinon.stub(document, 'createElement').callsFake(() => iframeMock);
await routing.handleRouteChange(path, component, node, config, window);

// then
assert.equal(iframeMock.src, expectedViewUrl);
assert.equal(
component.get().hideNav,
window.Luigi.config.settings.hideNavigation
);
});

it('should set component data with multiple path params', async () => {
// given
const path = '#/projects/categories/cat1/sub23';
const expectedViewUrl = 'cats/cat1/sub23';
const mockBrowser = new MockBrowser();
const window = mockBrowser.getWindow();
global.window = window;
const document = mockBrowser.getDocument();
global.document = document;

const node = {
style: {},
prepend: sinon.spy()
};

const config = {
iframe: null,
builderCompatibilityMode: false,
navigateOk: null
};

// when
window.Luigi = {};
window.Luigi.config = sampleLuigiConfig;
const iframeMock = { src: null };
sinon.stub(document, 'createElement').callsFake(() => iframeMock);
await routing.handleRouteChange(path, component, node, config, window);

// then
assert.equal(iframeMock.src, expectedViewUrl);
assert.equal(
component.get().hideNav,
window.Luigi.config.settings.hideNavigation
);
});

it('should get DefaultChildNode if viewUrl is not defined', async () => {
// given
const path = '#/projects/teams';
Expand Down

0 comments on commit d77cde4

Please sign in to comment.