Skip to content

Commit

Permalink
Function addNodeParams does not delete properties with value undefined (
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDoberer authored Jan 14, 2022
1 parent c17606a commit 1522522
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/core-api/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class LuigiRouting {

searchParams.set(paramKey, value);
if (value === undefined) {
searchParams.delete(key);
searchParams.delete(paramKey);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion core/test/core-api/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ describe('Luigi routing', function() {
LuigiRouting.addSearchParams('bar', true);
sinon.assert.calledWith(console.log, 'Params argument must be an object');
});
it('delete search params from url', () => {
it('delete search params from url with keepBrowserHistory is true', () => {
window.state = {};
global.location = 'http://some.url.de?luigi=rocks&mario=red';
LuigiRouting.addSearchParams({ mario: undefined }, true);
sinon.assert.calledWithExactly(window.history.pushState, window.state, '', 'http://some.url.de/?luigi=rocks');
});
it('delete search params from url with keepBrowserHistory is false', () => {
window.state = {};
global.location = 'http://some.url.de?luigi=rocks&mario=red';
LuigiRouting.addSearchParams({ mario: undefined }, false);
sinon.assert.calledWithExactly(window.history.replaceState, window.state, '', 'http://some.url.de/?luigi=rocks');
});
});
describe('SearchParams hash routing', () => {
beforeEach(() => {
Expand Down Expand Up @@ -182,5 +188,15 @@ describe('Luigi routing', function() {
LuigiRouting.addNodeParams('bar', true);
sinon.assert.calledWith(console.log, 'Params argument must be an object');
});
it('remove node param if value of params object is undefined', ()=>{
window.state = {};
global.location = 'http://some.url.de';
LuigiRouting.addNodeParams({ test: undefined }, false);
sinon.assert.calledWithExactly(window.history.replaceState, window.state, '', 'http://some.url.de/');
LuigiRouting.addNodeParams({ foo: 'bar' }, false);
sinon.assert.calledWithExactly(window.history.replaceState, window.state, '', 'http://some.url.de/?%7Efoo=bar');
LuigiRouting.addNodeParams({ foo: undefined }, false);
sinon.assert.calledWithExactly(window.history.replaceState, window.state, '', 'http://some.url.de/');
});
});
});

0 comments on commit 1522522

Please sign in to comment.