summaryrefslogtreecommitdiff
path: root/test/dom-text-scanner.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/dom-text-scanner.test.js')
-rw-r--r--test/dom-text-scanner.test.js111
1 files changed, 55 insertions, 56 deletions
diff --git a/test/dom-text-scanner.test.js b/test/dom-text-scanner.test.js
index e9d3405c..e204f2a3 100644
--- a/test/dom-text-scanner.test.js
+++ b/test/dom-text-scanner.test.js
@@ -112,56 +112,57 @@ function createAbsoluteGetComputedStyle(window) {
const domTestEnv = await setupDomTest(path.join(dirname, 'data/html/dom-text-scanner.html'));
-describe('DOMTextScanner', () => {
+describe('DOMTextScanner seek tests', () => {
const {window, teardown} = domTestEnv;
afterAll(() => teardown(global));
- test('Seek tests', () => {
- const {document} = window;
- window.getComputedStyle = createAbsoluteGetComputedStyle(window);
+ const {document} = window;
+ window.getComputedStyle = createAbsoluteGetComputedStyle(window);
- for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('test-case'))) {
- /** @type {import('test/dom-text-scanner').TestData|import('test/dom-text-scanner').TestData[]} */
- let testData = parseJson(/** @type {string} */ (testElement.dataset.testData));
- if (!Array.isArray(testData)) {
- testData = [testData];
- }
- for (const testDataItem of testData) {
- const {
- node: nodeSelector,
- offset,
- length,
- forcePreserveWhitespace,
- generateLayoutContent,
- reversible,
- expected: {
- node: expectedNodeSelector,
- offset: expectedOffset,
- content: expectedContent,
- remainder: expectedRemainder,
- },
- } = testDataItem;
-
- const node = querySelectorTextNode(testElement, nodeSelector);
- const expectedNode = querySelectorTextNode(testElement, expectedNodeSelector);
-
- expect(node).not.toEqual(null);
- expect(expectedNode).not.toEqual(null);
- if (node === null || expectedNode === null) { continue; }
-
- // Standard test
- {
- const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
- scanner.seek(length);
+ for (const testElement of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('test-case'))) {
+ const testDescription = testElement.querySelector('test-description')?.textContent || 'Test description not found.';
- const {node: actualNode1, offset: actualOffset1, content: actualContent1, remainder: actualRemainder1} = scanner;
- expect(actualContent1).toStrictEqual(expectedContent);
- expect(actualOffset1).toStrictEqual(expectedOffset);
- expect(actualNode1).toStrictEqual(expectedNode);
- expect(actualRemainder1).toStrictEqual(expectedRemainder || 0);
- }
+ /** @type {import('test/dom-text-scanner').TestData|import('test/dom-text-scanner').TestData[]} */
+ let testData = parseJson(/** @type {string} */ (testElement.dataset.testData));
+ if (!Array.isArray(testData)) {
+ testData = [testData];
+ }
- // Substring tests
+ for (const testDataItem of testData) {
+ const {
+ node: nodeSelector,
+ offset,
+ length,
+ forcePreserveWhitespace,
+ generateLayoutContent,
+ reversible,
+ expected: {
+ node: expectedNodeSelector,
+ offset: expectedOffset,
+ content: expectedContent,
+ remainder: expectedRemainder,
+ },
+ } = testDataItem;
+
+ const node = querySelectorTextNode(testElement, nodeSelector);
+ const expectedNode = querySelectorTextNode(testElement, expectedNodeSelector);
+
+ expect(node).not.toEqual(null);
+ expect(expectedNode).not.toEqual(null);
+ if (node === null || expectedNode === null) { continue; }
+
+ test(`${testDescription} standard test`, () => {
+ const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(length);
+
+ const {node: actualNode1, offset: actualOffset1, content: actualContent1, remainder: actualRemainder1} = scanner;
+ expect(actualContent1).toStrictEqual(expectedContent);
+ expect(actualOffset1).toStrictEqual(expectedOffset);
+ expect(actualNode1).toStrictEqual(expectedNode);
+ expect(actualRemainder1).toStrictEqual(expectedRemainder || 0);
+ });
+
+ test(`${testDescription} substring test`, () => {
for (let i = 1; i <= length; ++i) {
const scanner = new DOMTextScanner(node, offset, forcePreserveWhitespace, generateLayoutContent);
scanner.seek(length - i);
@@ -169,19 +170,17 @@ describe('DOMTextScanner', () => {
const {content: actualContent} = scanner;
expect(actualContent).toStrictEqual(expectedContent.substring(0, expectedContent.length - i));
}
+ });
- if (reversible === false) { continue; }
-
- // Reversed test
- {
- const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
- scanner.seek(-length);
+ test.runIf(reversible)(`${testDescription} reversed test`, () => {
+ const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
+ scanner.seek(-length);
- const {content: actualContent} = scanner;
- expect(actualContent).toStrictEqual(expectedContent);
- }
+ const {content: actualContent} = scanner;
+ expect(actualContent).toStrictEqual(expectedContent);
+ });
- // Reversed substring tests
+ test.runIf(reversible)(`${testDescription} reversed substring test`, () => {
for (let i = 1; i <= length; ++i) {
const scanner = new DOMTextScanner(expectedNode, expectedOffset, forcePreserveWhitespace, generateLayoutContent);
scanner.seek(-(length - i));
@@ -189,7 +188,7 @@ describe('DOMTextScanner', () => {
const {content: actualContent} = scanner;
expect(actualContent).toStrictEqual(expectedContent.substring(i));
}
- }
+ });
}
- });
+ }
});