summaryrefslogtreecommitdiff
path: root/test/document-util.test.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-16 19:52:00 -0500
committerGitHub <noreply@github.com>2023-12-17 00:52:00 +0000
commit77d27113d347b4724302f1c72de1f238e04aeead (patch)
tree1d9d5a8f2533778e98d662b8757f52a9533ed5f0 /test/document-util.test.js
parent64e4598b8cb59983fa21787550f68c562256fe67 (diff)
Fix DOM tests (#363)
* Only apply eslint rules to .test.js files * Fix tests * Use built-in environment * Fix tests * Optional file name * Remove environment
Diffstat (limited to 'test/document-util.test.js')
-rw-r--r--test/document-util.test.js86
1 files changed, 18 insertions, 68 deletions
diff --git a/test/document-util.test.js b/test/document-util.test.js
index 8c6ab69b..10857df9 100644
--- a/test/document-util.test.js
+++ b/test/document-util.test.js
@@ -16,15 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import fs from 'fs';
-import {JSDOM} from 'jsdom';
import {fileURLToPath} from 'node:url';
import path from 'path';
-import {expect, test} from 'vitest';
+import {describe, expect} from 'vitest';
import {DocumentUtil} from '../ext/js/dom/document-util.js';
import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js';
import {TextSourceElement} from '../ext/js/dom/text-source-element.js';
import {TextSourceRange} from '../ext/js/dom/text-source-range.js';
+import {domTest} from './document-test.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -69,27 +68,6 @@ class DOMRect {
/**
- * @param {string} fileName
- * @returns {JSDOM}
- */
-function createJSDOM(fileName) {
- const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
- const dom = new JSDOM(domSource);
- const document = dom.window.document;
- const window = dom.window;
-
- // Define innerText setter as an alias for textContent setter
- Object.defineProperty(window.HTMLDivElement.prototype, 'innerText', {
- set(value) { this.textContent = value; }
- });
-
- // Placeholder for feature detection
- document.caretRangeFromPoint = () => null;
-
- return dom;
-}
-
-/**
* @param {Element} element
* @param {string|undefined} selector
* @returns {?Element}
@@ -99,13 +77,13 @@ function querySelectorChildOrSelf(element, selector) {
}
/**
- * @param {JSDOM} dom
+ * @param {import('jsdom').DOMWindow} window
* @param {?Node} node
* @returns {?Text|Node}
*/
-function getChildTextNodeOrSelf(dom, node) {
+function getChildTextNodeOrSelf(window, node) {
if (node === null) { return null; }
- const Node = dom.window.Node;
+ const Node = window.Node;
const childNode = node.firstChild;
return (childNode !== null && childNode.nodeType === Node.TEXT_NODE ? childNode : node);
}
@@ -131,27 +109,10 @@ function findImposterElement(document) {
return document.querySelector('div[style*="2147483646"]>*');
}
-
-/** */
-async function testDocument1() {
- const dom = createJSDOM(path.join(dirname, 'data', 'html', 'test-document1.html'));
- const window = dom.window;
-
- try {
- await testDocumentTextScanningFunctions(dom);
- await testTextSourceRangeSeekFunctions(dom);
- } finally {
- window.close();
- }
-}
-
-/**
- * @param {JSDOM} dom
- */
-async function testDocumentTextScanningFunctions(dom) {
- const document = dom.window.document;
-
- test('DocumentTextScanningFunctions', () => {
+describe('DocumentUtil', () => {
+ const testDoc = domTest(path.join(dirname, 'data/html/test-document1.html'));
+ testDoc('Text scanning functions', ({window}) => {
+ const {document} = window;
for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('.test[data-test-type=scan]'))) {
// Get test parameters
const {
@@ -170,8 +131,8 @@ async function testDocumentTextScanningFunctions(dom) {
const elementFromPointValue = querySelectorChildOrSelf(testElement, elementFromPointSelector);
const caretRangeFromPointValue = querySelectorChildOrSelf(testElement, caretRangeFromPointSelector);
- const startNode = getChildTextNodeOrSelf(dom, querySelectorChildOrSelf(testElement, startNodeSelector));
- const endNode = getChildTextNodeOrSelf(dom, querySelectorChildOrSelf(testElement, endNodeSelector));
+ const startNode = getChildTextNodeOrSelf(window, querySelectorChildOrSelf(testElement, startNodeSelector));
+ const endNode = getChildTextNodeOrSelf(window, querySelectorChildOrSelf(testElement, endNodeSelector));
const startOffset2 = parseInt(/** @type {string} */ (startOffset), 10);
const endOffset2 = parseInt(/** @type {string} */ (endOffset), 10);
@@ -187,7 +148,7 @@ async function testDocumentTextScanningFunctions(dom) {
document.elementFromPoint = () => elementFromPointValue;
document.caretRangeFromPoint = (x, y) => {
- const imposter = getChildTextNodeOrSelf(dom, findImposterElement(document));
+ const imposter = getChildTextNodeOrSelf(window, findImposterElement(document));
expect(!!imposter).toStrictEqual(hasImposter === 'true');
const range = document.createRange();
@@ -264,15 +225,12 @@ async function testDocumentTextScanningFunctions(dom) {
source.cleanup();
}
});
-}
+});
-/**
- * @param {JSDOM} dom
- */
-async function testTextSourceRangeSeekFunctions(dom) {
- const document = dom.window.document;
-
- test('TextSourceRangeSeekFunctions', async () => {
+describe('DOMTextScanner', () => {
+ const testDoc = domTest(path.join(dirname, 'data/html/test-document1.html'));
+ testDoc('Seek functions', async ({window}) => {
+ const {document} = window;
for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('.test[data-test-type=text-source-range-seek]'))) {
// Get test parameters
const {
@@ -314,12 +272,4 @@ async function testTextSourceRangeSeekFunctions(dom) {
expect(content).toStrictEqual(expectedResultContent);
}
});
-}
-
-
-/** */
-async function main() {
- await testDocument1();
-}
-
-await main();
+});