Skip to content

Commit bb9e3e1

Browse files
authored
Use Params.prototype.toSearchParams() in developer-mode.mjs (#511)
- Remove duplicated code and directly use Params.prototype.toSearchParams(). - Now the Params class is the centralized place to implement the serialization logic. - Support empty suites selection - Use a single "default" tag as default value for tags
1 parent dd661c0 commit bb9e3e1

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

resources/developer-mode.mjs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Suites, Tags } from "./tests.mjs";
2-
import { params, defaultParams } from "./shared/params.mjs";
2+
import { params } from "./shared/params.mjs";
33

44
export function createDeveloperModeContainer() {
55
const container = document.createElement("div");
@@ -254,7 +254,7 @@ function updateParamsSuitesAndTags() {
254254
commonTags = new Set(suite.tags.filter((tag) => commonTags.has(tag)));
255255
}
256256
if (selectedSuites.length > 1 && commonTags.size)
257-
params.tags = commonTags.has("default") ? [] : [...commonTags];
257+
params.tags = [...commonTags];
258258
else
259259
params.suites = selectedSuites.map((suite) => suite.name);
260260
}
@@ -263,26 +263,8 @@ function updateURL() {
263263
updateParamsSuitesAndTags();
264264

265265
const url = new URL(window.location.href);
266-
267-
url.searchParams.delete("tags");
268-
url.searchParams.delete("suites");
269-
url.searchParams.delete("suite");
270-
271-
if (params.tags.length)
272-
url.searchParams.set("tags", params.tags.join(","));
273-
else if (params.suites.length)
274-
url.searchParams.set("suites", params.suites.join(","));
275-
276-
const defaultParamKeys = ["iterationCount", "useWarmupSuite", "warmupBeforeSync", "waitBeforeSync", "useAsyncSteps"];
277-
for (const paramKey of defaultParamKeys) {
278-
if (params[paramKey] !== defaultParams[paramKey])
279-
url.searchParams.set(paramKey, params[paramKey]);
280-
else
281-
url.searchParams.delete(paramKey);
282-
}
283-
266+
url.search = params.toSearchParams();
284267
// Only push state if changed
285-
url.search = decodeURIComponent(url.search);
286268
if (url.href !== window.location.href)
287269
window.history.pushState({}, "", url);
288270
}

resources/shared/params.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Params {
99
iterationCount = 10;
1010
suites = [];
1111
// A list of tags to filter suites
12-
tags = [];
12+
tags = ["default"];
1313
// Toggle running a dummy suite once before the normal test suites.
1414
useWarmupSuite = false;
1515
// toggle async type vs default raf type.
@@ -168,10 +168,14 @@ export class Params {
168168
if (this.viewport.width !== defaultParams.viewport.width || this.viewport.height !== defaultParams.viewport.height)
169169
rawUrlParams.viewport = `${this.viewport.width}x${this.viewport.height}`;
170170

171-
if (this.suites.length)
171+
if (this.suites.length) {
172172
rawUrlParams.suites = this.suites.join(",");
173-
else if (this.tags.length)
174-
rawUrlParams.tags = this.tags.join(",");
173+
} else if (this.tags.length) {
174+
if (!(this.tags.length === 1 && this.tags[0] === "default"))
175+
rawUrlParams.tags = this.tags.join(",");
176+
} else {
177+
rawUrlParams.suites = "";
178+
}
175179

176180
return new URLSearchParams(rawUrlParams);
177181
}

tests/unittests/params.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe("Params", () => {
77
});
88
it("should be empty for empty Params", () => {
99
const params = new Params();
10+
expect(params.tags).to.eql(["default"]);
1011
expect(params.toSearchParams()).to.equal("");
1112
});
1213
it("should contain custom viewport", () => {
@@ -41,6 +42,14 @@ describe("Params", () => {
4142
);
4243
expect(params.toSearchParams()).to.equal("suites=Suite1");
4344
});
45+
it("should ignore a single default tag", () => {
46+
const params = new Params(
47+
new URLSearchParams({
48+
tags: ["default"],
49+
})
50+
);
51+
expect(params.toSearchParams()).to.equal("");
52+
});
4453
it("should contain multiple single suite", () => {
4554
const params = new Params(
4655
new URLSearchParams({
@@ -57,6 +66,14 @@ describe("Params", () => {
5766
);
5867
expect(params.toSearchParams()).to.equal("tags=tagB%2Ctag1%2CtagA");
5968
});
69+
it("should support no suites", () => {
70+
const params = new Params(
71+
new URLSearchParams({
72+
suites: "",
73+
})
74+
);
75+
expect(params.toSearchParams()).to.equal("suites=");
76+
});
6077
});
6178

6279
describe("parse input params", () => {

0 commit comments

Comments
 (0)