Skip to content

Commit d90b6fd

Browse files
Cleanup/shared (#513)
1 parent 06449bd commit d90b6fd

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

resources/shared/benchmark.mjs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class BenchmarkStep {
1313
this.run = run;
1414
}
1515

16-
async runAndRecord(params, suite, test, callback) {
16+
async runAndRecordStep(params, suite, test, callback) {
1717
const testRunner = new TestRunner(null, null, params, suite, test, callback);
1818
const result = await testRunner.runTest();
1919
return result;
@@ -26,9 +26,9 @@ export class BenchmarkStep {
2626
* A single test suite that contains one or more test steps.
2727
*/
2828
export class BenchmarkSuite {
29-
constructor(name, tests) {
29+
constructor(name, steps) {
3030
this.name = name;
31-
this.tests = tests;
31+
this.steps = steps;
3232
}
3333

3434
record(_test, syncTime, asyncTime) {
@@ -41,7 +41,7 @@ export class BenchmarkSuite {
4141
return results;
4242
}
4343

44-
async runAndRecord(params, onProgress) {
44+
async runAndRecordSuite(params, onProgress) {
4545
const measuredValues = {
4646
tests: {},
4747
prepare: 0,
@@ -52,11 +52,11 @@ export class BenchmarkSuite {
5252

5353
performance.mark(suiteStartLabel);
5454

55-
for (const test of this.tests) {
56-
const result = await test.runAndRecord(params, this, test, this.record);
57-
measuredValues.tests[test.name] = result;
55+
for (const step of this.steps) {
56+
const result = await step.runAndRecordStep(params, this, step, this.record);
57+
measuredValues.tests[step.name] = result;
5858
measuredValues.total += result.total;
59-
onProgress?.(test.name);
59+
onProgress?.(step.name);
6060
}
6161

6262
performance.mark(suiteEndLabel);
@@ -104,7 +104,7 @@ export class BenchmarkConnector {
104104
const suite = this.suites[event.data.name];
105105
if (!suite)
106106
console.error(`Suite with the name of "${event.data.name}" not found!`);
107-
const { result } = await suite.runAndRecord(params, (test) => this.sendMessage({ type: "step-complete", status: "success", appId: this.appId, name: this.name, test }));
107+
const { result } = await suite.runAndRecordSuite(params, (test) => this.sendMessage({ type: "step-complete", status: "success", appId: this.appId, name: this.name, test }));
108108
this.sendMessage({ type: "suite-complete", status: "success", appId: this.appId, result });
109109
this.disconnect();
110110
break;

resources/shared/helpers.mjs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
/**
2-
* Helper Methods
2+
* Recursively queries the DOM for an element, traversing through shadow DOMs.
33
*
4-
* Various methods that are extracted from the Page class.
4+
* @param {Element|ShadowRoot} lookupStartNode The node or shadow root to start the lookup from.
5+
* @param {string[]} path An array of CSS selectors representing the path to the target element.
6+
* @returns {Element|null} The target element if found, otherwise null.
57
*/
6-
export function getParent(lookupStartNode, path) {
8+
export function recursivelyQuerySelector(lookupStartNode, path) {
79
lookupStartNode = lookupStartNode.shadowRoot ?? lookupStartNode;
8-
const parent = path.reduce((root, selector) => {
10+
const target = path.reduce((root, selector) => {
911
const node = root.querySelector(selector);
1012
return node.shadowRoot ?? node;
1113
}, lookupStartNode);
1214

13-
return parent;
15+
return target;
1416
}
1517

18+
/**
19+
* Retrieves a single DOM element, optionally traversing through shadow DOMs to a specified path before the final selection.
20+
*
21+
* @param {string} selector The CSS selector for the desired element.
22+
* @param {string[]} [path=[]] An optional array of CSS selectors to reach the desired shadowRoot or parent element.
23+
* @param {Element|ShadowRoot} [lookupStartNode=document] The starting node for the lookup.
24+
* @returns {Element|null} The found element, or null if not found.
25+
*/
1626
export function getElement(selector, path = [], lookupStartNode = document) {
17-
const element = getParent(lookupStartNode, path).querySelector(selector);
27+
const element = recursivelyQuerySelector(lookupStartNode, path).querySelector(selector);
1828
return element;
1929
}
2030

31+
/**
32+
* Retrieves all DOM elements matching a selector, optionally traversing through shadow DOMs to a specified path before the final selection.
33+
*
34+
* @param {string} selector The CSS selector for the desired elements.
35+
* @param {string[]} [path=[]] An optional array of CSS selectors to reach the desired shadowRoot or parent element.
36+
* @param {Element|ShadowRoot} [lookupStartNode=document] The starting node for the lookup.
37+
* @returns {Element[]} An array of found elements.
38+
*/
2139
export function getAllElements(selector, path = [], lookupStartNode = document) {
22-
const elements = Array.from(getParent(lookupStartNode, path).querySelectorAll(selector));
40+
const elements = Array.from(recursivelyQuerySelector(lookupStartNode, path).querySelectorAll(selector));
2341
return elements;
2442
}
2543

resources/shared/test-invoker.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ class TestInvoker {
55
this._reportCallback = reportCallback;
66
this._params = params;
77
}
8-
}
98

10-
class BaseRAFTestInvoker extends TestInvoker {
119
start() {
1210
return new Promise((resolve) => {
1311
if (this._params.waitBeforeSync)
@@ -18,7 +16,7 @@ class BaseRAFTestInvoker extends TestInvoker {
1816
}
1917
}
2018

21-
class RAFTestInvoker extends BaseRAFTestInvoker {
19+
class RAFTestInvoker extends TestInvoker {
2220
_scheduleCallbacks(resolve) {
2321
requestAnimationFrame(() => this._syncCallback());
2422
requestAnimationFrame(() => {
@@ -33,7 +31,7 @@ class RAFTestInvoker extends BaseRAFTestInvoker {
3331
}
3432
}
3533

36-
class AsyncRAFTestInvoker extends BaseRAFTestInvoker {
34+
class AsyncRAFTestInvoker extends TestInvoker {
3735
static mc = new MessageChannel();
3836
_scheduleCallbacks(resolve) {
3937
let gotTimer = false;

0 commit comments

Comments
 (0)