fix(ui): deduplicate filter in query args

If we get same filter value multiple times we should only use it once
This commit is contained in:
Łukasz Mierzwa
2019-08-14 18:51:52 +01:00
parent 380c5543a4
commit be5b19c8d8
2 changed files with 26 additions and 2 deletions

View File

@@ -46,8 +46,11 @@ function DecodeLocationSearch(searchString) {
if (parsed.q === "") {
params.q = [];
} else if (Array.isArray(parsed.q)) {
// filter out empty strings, so 'q=' doesn't end up [""] but rather []
params.q = parsed.q.filter(v => v !== "");
// first filter out duplicates
// then filter out empty strings, so 'q=' doesn't end up [""] but rather []
params.q = parsed.q
.filter((v, i) => parsed.q.indexOf(v) === i)
.filter(v => v !== "");
} else {
params.q = [parsed.q];
}

View File

@@ -181,6 +181,13 @@ describe("DecodeLocationSearch", () => {
});
});
it("no value q[]=&q[]= search param is decoded correctly", () => {
expect(DecodeLocationSearch("?q=")).toMatchObject({
defaultsUsed: false,
params: { q: [] }
});
});
it("single value q=foo search param is decoded correctly", () => {
expect(DecodeLocationSearch("?q=foo")).toMatchObject({
defaultsUsed: false,
@@ -201,6 +208,20 @@ describe("DecodeLocationSearch", () => {
params: { q: ["foo", "bar"] }
});
});
it("multi value q[]=foo&q[]=bar&q[]=foo search param is decoded correctly", () => {
expect(DecodeLocationSearch("?q[]=foo&q[]=bar&q[]=foo")).toMatchObject({
defaultsUsed: false,
params: { q: ["foo", "bar"] }
});
});
it("multi value q[]=foo&q[]=&q[]=foo search param is decoded correctly", () => {
expect(DecodeLocationSearch("?q[]=foo&q[]=&q[]=foo")).toMatchObject({
defaultsUsed: false,
params: { q: ["foo"] }
});
});
});
describe("UpdateLocationSearch", () => {