Commit 91d0184f authored by Miguel Rincon's avatar Miguel Rincon

Function setUrlParams removes URL array params

This change extends setUrlParams when changing a URL by having it
remove the parameters when arrays are empty.

Now it is possible to remove the array parameters of the URL:

https://gitlab.com/test?labels[]=foo&labels[]=bar

By adding `{ labels: [] }`, to obtain:

https://gitlab.com/test
parent e2f3ccf3
......@@ -484,13 +484,17 @@ export const setUrlParams = (
searchParams.delete(key);
} else if (Array.isArray(params[key])) {
const keyName = railsArraySyntax ? `${key}[]` : key;
params[key].forEach((val, idx) => {
if (idx === 0) {
searchParams.set(keyName, val);
} else {
searchParams.append(keyName, val);
}
});
if (params[key].length === 0) {
searchParams.delete(keyName);
} else {
params[key].forEach((val, idx) => {
if (idx === 0) {
searchParams.set(keyName, val);
} else {
searchParams.append(keyName, val);
}
});
}
} else {
searchParams.set(key, params[key]);
}
......
......@@ -798,15 +798,29 @@ describe('URL utility', () => {
);
});
it('handles arrays properly', () => {
it('adds parameters from arrays', () => {
const url = 'https://gitlab.com/test';
expect(urlUtils.setUrlParams({ label_name: ['foo', 'bar'] }, url)).toEqual(
'https://gitlab.com/test?label_name=foo&label_name=bar',
expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url)).toEqual(
'https://gitlab.com/test?labels=foo&labels=bar',
);
});
it('handles arrays properly when railsArraySyntax=true', () => {
it('removes parameters from empty arrays', () => {
const url = 'https://gitlab.com/test?labels=foo&labels=bar';
expect(urlUtils.setUrlParams({ labels: [] }, url)).toEqual('https://gitlab.com/test');
});
it('removes parameters from empty arrays while keeping other parameters', () => {
const url = 'https://gitlab.com/test?labels=foo&labels=bar&unrelated=unrelated';
expect(urlUtils.setUrlParams({ labels: [] }, url)).toEqual(
'https://gitlab.com/test?unrelated=unrelated',
);
});
it('adds parameters from arrays when railsArraySyntax=true', () => {
const url = 'https://gitlab.com/test';
expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url, false, true)).toEqual(
......@@ -814,6 +828,14 @@ describe('URL utility', () => {
);
});
it('removes parameters from empty arrays when railsArraySyntax=true', () => {
const url = 'https://gitlab.com/test?labels%5B%5D=foo&labels%5B%5D=bar';
expect(urlUtils.setUrlParams({ labels: [] }, url, false, true)).toEqual(
'https://gitlab.com/test',
);
});
it('decodes URI when decodeURI=true', () => {
const url = 'https://gitlab.com/test';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment