Skip to content

Commit

Permalink
migrate testutils & highlighter test to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
giabao committed Apr 22, 2019
1 parent fb7d611 commit 6dad987
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 74 deletions.
57 changes: 19 additions & 38 deletions test/highlighter/index.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Rangy - Highlighter Tests</title>

<script type="text/javascript" src="../external/log4javascript.js"></script>
<script type="text/javascript" src="../external/jshashtable.js"></script>
<script type="text/javascript">
//var appender = new log4javascript.InPageAppender();
var appender = new log4javascript.BrowserConsoleAppender();
//log4javascript.getRootLogger().addAppender(appender);
var log = log4javascript.getRootLogger();
log4javascript.setShowStackTraces(true);
//log4javascript.setEnabled(false);
</script>
<script type="text/javascript" src="xntest.js"></script>
<script type="text/javascript" src="testutils.js"></script>
<script type="text/javascript">
xn.test.enableStackTraces = true;
</script>

<script type="text/javascript" src="../src/core/core.js"></script>
<script type="text/javascript" src="../src/core/dom.js"></script>
<script type="text/javascript" src="../src/core/domrange.js"></script>
<script type="text/javascript" src="../src/core/wrappedrange.js"></script>
<script type="text/javascript" src="../src/core/wrappedselection.js"></script>
<script type="text/javascript" src="../src/modules/rangy-classapplier.js"></script>
<script type="text/javascript" src="../src/modules/rangy-highlighter.js"></script>
<script type="text/javascript" src="highlightertests.js"></script>
<link rel="stylesheet" type="text/css" href="tests.css"/>
</head>
<body>
<div id="test"></div>
<div id="messages"></div>
</body>

</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>QUnit Example</title>
<!-- load rangy2 here in `head` to verify that rangy2 can init without need dom ready -->
<script src="../../dist/core/bundles/index.umd.min.js"></script>
<script src="../../dist/highlighter/bundles/index.umd.min.js"></script>
<script src="../../node_modules/qunit/qunit/qunit.js"></script>
<link rel="stylesheet" href="../../node_modules/qunit/qunit/qunit.css">
<script src="../qunit-ex.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="index.test.js"></script>
</body>
</html>
18 changes: 9 additions & 9 deletions test/highlighter/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
xn.test.suite("Highlighter module tests", function(s) {
s.tearDown = function() {
document.getElementById("test").innerHTML = "";
};
import * as rangy from "rangy2";
import "rangy-classapplier";
import "rangy-highlighter";
import {createRangeInHtml} from "../testutils"

s.test("highlightSelection test", function(t) {
QUnit.module("Highlighter module tests");

QUnit.test("highlightSelection test", function(t) {
var applier = rangy.createClassApplier("c1");
var highlighter = rangy.createHighlighter();
highlighter.addClassApplier(applier);

var testEl = document.getElementById("test");
var range = rangyTestUtils.createRangeInHtml(testEl, 'one [two] three four');
var range = createRangeInHtml(testEl, 'one [two] three four');
range.select();

var highlights = highlighter.highlightSelection("c1");
Expand All @@ -22,12 +24,10 @@ xn.test.suite("Highlighter module tests", function(s) {

});

s.test("Options test (issue 249)", function(t) {
QUnit.test("Options test (issue 249)", function(t) {
var applier = rangy.createClassApplier("c1");
var highlighter = rangy.createHighlighter();
highlighter.addClassApplier(applier);

highlighter.highlightSelection("c1", { selection: rangy.getSelection() });
});

}, false);
46 changes: 19 additions & 27 deletions test/testutils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var rangyTestUtils = (function() {
function createNodeTree(levels, copiesPerLevel) {
import * as rangy from "rangy2";
import "core-js/es/map";

export function createNodeTree(levels, copiesPerLevel) {
function createTestNodes(parentNode, limit, copies) {
if (limit > 0) {
var n = parentNode.appendChild(document.createElement("div"));
Expand All @@ -19,9 +21,9 @@ var rangyTestUtils = (function() {
}

var nextIterationId = 1;
var nodeIterationIds = new Hashtable();
var nodeIterationIds = new Map();

function iterateNodes(node, func, includeSelf, iterationId) {
export function iterateNodes(node, func, includeSelf, iterationId?) {
if (!iterationId) {
iterationId = nextIterationId++;
}
Expand All @@ -31,27 +33,29 @@ var rangyTestUtils = (function() {
if (includeSelf) {
func(node);
}
nodeIterationIds.put(node, iterationId);
nodeIterationIds.set(node, iterationId);
for (var child = node.firstChild, nextChild; !!child; child = nextChild) {
nextChild = child.nextSibling;
iterateNodes(child, func, true, iterationId);
}
}

function RangeInfo() {}

RangeInfo.prototype = {
setStart: function(node, offset) {
export class RangeInfo {
sc: Node;
ec: Node;
so: number;
eo: number;
setStart(node, offset) {
this.sc = node;
this.so = offset;
},
setEnd: function(node, offset) {
}
setEnd(node, offset) {
this.ec = node;
this.eo = offset;
}
};
}

function createRangeInHtml(containerEl, html) {
export function createRangeInHtml(containerEl, html) {
containerEl.innerHTML = html;
var range = rangy.createRange(), foundStart = false;
var rangeInfo = new RangeInfo();
Expand Down Expand Up @@ -103,7 +107,7 @@ var rangyTestUtils = (function() {
return range;
}

function getSortedClassName(el) {
export function getSortedClassName(el) {
return el.className.split(/\s+/).sort().join(" ");
}

Expand All @@ -112,7 +116,7 @@ var rangyTestUtils = (function() {
return r2.compareBoundaryPoints(r1.START_TO_START, r1);
}

function htmlAndRangeToString(containerEl, range) {
export function htmlAndRangeToString(containerEl, range) {
function isElementRangeBoundary(el, offset, range, isStart) {
var prefix = isStart ? "start" : "end";
return (el == range[prefix + "Container"] && offset == range[prefix + "Offset"]);
Expand Down Expand Up @@ -173,15 +177,3 @@ var rangyTestUtils = (function() {

return getHtml(containerEl, false);
}


return {
createNodeTree: createNodeTree,
RangeInfo: RangeInfo,
iterateNodes: iterateNodes,
createRangeInHtml: createRangeInHtml,
getSortedClassName: getSortedClassName,
htmlAndRangeToString: htmlAndRangeToString
}

})();

0 comments on commit 6dad987

Please sign in to comment.