summaryrefslogtreecommitdiff
path: root/preact/benches/src/util.js
blob: 64c4765356387389b0f7db7e2716665a867fc71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// import afterFrame from "../node_modules/afterframe/dist/afterframe.module.js";
import afterFrame from 'afterframe';

export { afterFrame };

export const measureName = 'duration';

let promise = null;
export function afterFrameAsync() {
	if (promise === null) {
		promise = new Promise(resolve =>
			afterFrame(time => {
				promise = null;
				resolve(time);
			})
		);
	}

	return promise;
}

export function measureMemory() {
	if ('gc' in window && 'memory' in performance) {
		// Report results in MBs
		window.gc();
		window.usedJSHeapSize = performance.memory.usedJSHeapSize / 1e6;
	} else {
		window.usedJSHeapSize = 0;
	}
}

export function markRunStart(runId) {
	performance.mark(`run-${runId}-start`);
}

let staticPromise = Promise.resolve();
export function markRunEnd(runId) {
	return staticPromise.then(() => {
		performance.mark(`run-${runId}-end`);
		performance.measure(
			`run-${runId}`,
			`run-${runId}-start`,
			`run-${runId}-end`
		);
	});
}

export function getRowIdSel(index) {
	return `tbody > tr:nth-child(${index}) > td:first-child`;
}

export function getRowLinkSel(index) {
	return `tbody > tr:nth-child(${index}) > td:nth-child(2) > a`;
}

/**
 * @param {string} selector
 * @returns {Element}
 */
export function getBySelector(selector) {
	const element = document.querySelector(selector);
	if (element == null) {
		throw new Error(`Could not find element matching selector: ${selector}`);
	}

	return element;
}

export function testElement(selector) {
	const testElement = document.querySelector(selector);
	if (testElement == null) {
		throw new Error(
			'Test failed. Rendering after one paint was not successful'
		);
	}
}

export function testElementText(selector, expectedText) {
	const elm = document.querySelector(selector);
	if (elm == null) {
		throw new Error('Could not find element matching selector: ' + selector);
	}

	if (elm.textContent != expectedText) {
		throw new Error(
			`Element did not have expected text. Expected: '${expectedText}' Actual: '${elm.textContent}'`
		);
	}
}

export function testElementTextContains(selector, expectedText) {
	const elm = getBySelector(selector);
	if (!elm.textContent.includes(expectedText)) {
		throw new Error(
			`Element did not include expected text. Expected to include: '${expectedText}' Actual: '${elm.textContent}'`
		);
	}
}