Skip to content

Commit

Permalink
DOM element tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRixham committed Apr 6, 2017
1 parent b67f8db commit b82a271
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/element/DOMElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ define([

function DOMElement(element) {

this.hasInScope = function(other) {

return other.isInScope(element);
};

this.isInScope = function(scope) {

return isInScope(element, scope);
Expand All @@ -23,11 +18,15 @@ define([

if (!currentElement) {

return false;
}
else if (currentElement == scope) {

return true;
}
else if (currentElement.__DATUM__REBIND) {

return currentElement == scope;
return false;
}
else {

Expand All @@ -51,7 +50,7 @@ define([

var newElement = new DOMElement(elements[i]);

if (this.hasInScope(newElement)) {
if (newElement.isInScope(element)) {

elementsArray.push(newElement);
}
Expand Down
98 changes: 98 additions & 0 deletions test/element/dom-element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM Element Tests</title>
<link rel="stylesheet" href="../../node_modules/qunitjs/qunit/qunit.css">
<script src="../../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../../node_modules/requirejs/require.js"></script>
</head>
<body>
<style>
#qunit ~ * {
display: none;
}
</style>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script>
var i = 0;

function require(dependencies, factory) {

requirejs.config({ baseUrl: "../../src", context: i++ })(dependencies, factory);
}
</script>
<div id="super">
<div id="sub"></div>
</div>
<script>
QUnit.test("Subelement is in scope", function(assert) {

var done = assert.async();

require(["element/DOMElement"], function(DOMElement) {

var sup = document.querySelector("#super");
var sub = document.querySelector("#sub");

var element = new DOMElement(sup);
var subelement = new DOMElement(sub);

assert.ok(subelement.isInScope(sup));
assert.ok(!element.isInScope(sub));

done();
});
});
</script>
<div id="non-super"></div>
<div id="non-sub"></div>
<script>
QUnit.test("Fail to bind subobject outside of scope", function(assert) {

var done = assert.async();

require(["element/DOMElement"], function(DOMElement) {

var nonSuper = document.querySelector("#non-super");
var nonSub = document.querySelector("#non-sub");

var element = new DOMElement(nonSuper);
var subelement = new DOMElement(nonSub);

assert.ok(!subelement.isInScope(nonSuper));
assert.ok(!element.isInScope(nonSub));

done();
});
});
</script>
<div id="fail-super">
<div id="fail-sub">
<div id="fail"></div>
</div>
</div>
<script>
QUnit.test("Fail to bind object out of scope", function(assert) {

var done = assert.async();

require(["element/DOMElement"], function(DOMElement) {

var failSuper = document.querySelector("#fail-super");
var failSub = document.querySelector("#fail-sub");
var fail = document.querySelector("#fail");

failSub.__DATUM__REBIND = function() {};

var element = new DOMElement(fail);

assert.ok(!element.isInScope(failSuper));

done();
});
});
</script>
</body>
</html>

0 comments on commit b82a271

Please sign in to comment.