Skip to content

Commit 5e7cec8

Browse files
authored
Improve Params.toSearchParamsObject (#505)
- Add better viewport, suite and tags handling - Add basic Params unittests
1 parent a5ee467 commit 5e7cec8

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

resources/shared/params.mjs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,26 @@ export class Params {
154154
}
155155

156156
toSearchParamsObject(filter = true) {
157-
const rawParams = { __proto__: null };
157+
const rawUrlParams = { __proto__: null };
158158
for (const [key, value] of Object.entries(this)) {
159+
// Handle composite values separately.
160+
if (key === "viewport" || key === "suites" || key === "tags")
161+
continue;
162+
// Skip over default values.
159163
if (filter && value === defaultParams[key])
160164
continue;
161-
rawParams[key] = value;
165+
rawUrlParams[key] = value;
162166
}
163167

164-
// Either suites or params can be used at the same time.
165-
if (rawParams.suites?.length && rawParams.tags?.length)
166-
delete rawParams.suites;
167-
rawParams.viewport = `${this.viewport.width}x${this.viewport.height}`;
168+
if (this.viewport.width !== defaultParams.viewport.width || this.viewport.height !== defaultParams.viewport.height)
169+
rawUrlParams.viewport = `${this.viewport.width}x${this.viewport.height}`;
170+
171+
if (this.suites.length)
172+
rawUrlParams.suites = this.suites.join(",");
173+
else if (this.tags.length)
174+
rawUrlParams.tags = this.tags.join(",");
168175

169-
return new URLSearchParams(rawParams);
176+
return new URLSearchParams(rawUrlParams);
170177
}
171178

172179
toSearchParams() {

tests/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
});
2929

3030
await import("./unittests/benchmark-runner.mjs");
31+
await import("./unittests/params.mjs");
3132

3233
globalThis.testResults = undefined;
3334
globalThis.testRunner = mocha.run();

tests/unittests/params.mjs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Params, defaultParams } from "../../resources/shared/params.mjs";
2+
3+
describe("Params", () => {
4+
describe("toSearchParams", () => {
5+
it("should be empty for defaultParams", () => {
6+
expect(defaultParams.toSearchParams()).to.equal("");
7+
});
8+
it("should be empty for empty Params", () => {
9+
const params = new Params();
10+
expect(params.toSearchParams()).to.equal("");
11+
});
12+
it("should contain custom viewport", () => {
13+
const params = new Params(
14+
new URLSearchParams({
15+
viewport: "100x200",
16+
})
17+
);
18+
expect(params.toSearchParams()).to.equal("viewport=100x200");
19+
});
20+
it("should not contain default viewport", () => {
21+
const params = new Params(
22+
new URLSearchParams({
23+
viewport: "800x600",
24+
})
25+
);
26+
expect(params.toSearchParams()).to.equal("");
27+
});
28+
it("should contain custom iterationCount", () => {
29+
const params = new Params(
30+
new URLSearchParams({
31+
iterationCount: "100",
32+
})
33+
);
34+
expect(params.toSearchParams()).to.equal("iterationCount=100");
35+
});
36+
it("should contain single suite", () => {
37+
const params = new Params(
38+
new URLSearchParams({
39+
suites: "Suite1",
40+
})
41+
);
42+
expect(params.toSearchParams()).to.equal("suites=Suite1");
43+
});
44+
it("should contain multiple single suite", () => {
45+
const params = new Params(
46+
new URLSearchParams({
47+
suites: "SuiteB,Suite1,SuiteA",
48+
})
49+
);
50+
expect(params.toSearchParams()).to.equal("suites=SuiteB%2CSuite1%2CSuiteA");
51+
});
52+
it("should contain multiple tags", () => {
53+
const params = new Params(
54+
new URLSearchParams({
55+
tags: "tagB,tag1,tagA",
56+
})
57+
);
58+
expect(params.toSearchParams()).to.equal("tags=tagB%2Ctag1%2CtagA");
59+
});
60+
});
61+
62+
describe("parse input params", () => {
63+
it("should parse custom viewport", () => {
64+
const params = new Params(
65+
new URLSearchParams({
66+
viewport: "100x300",
67+
})
68+
);
69+
expect(params.viewport).to.eql({ width: 100, height: 300 });
70+
});
71+
it("should parse custom iterationCount", () => {
72+
const params = new Params(
73+
new URLSearchParams({
74+
iterationCount: "100",
75+
})
76+
);
77+
expect(params.iterationCount).to.equal(100);
78+
});
79+
it("should parse custom tags", () => {
80+
const params = new Params(
81+
new URLSearchParams({
82+
tags: "tagB,tag1,tagA",
83+
})
84+
);
85+
expect(params.tags).to.eql(["tagB", "tag1", "tagA"]);
86+
});
87+
it("should parse custom suites", () => {
88+
const params = new Params(
89+
new URLSearchParams({
90+
suites: "SuiteB,Suite1,SuiteA",
91+
})
92+
);
93+
expect(params.suites).to.eql(["SuiteB", "Suite1", "SuiteA"]);
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)