Files
pjax/tests/lib/switch-selectors.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-03-03 01:37:45 -05:00
var tape = require("tape");
2019-03-03 01:37:45 -05:00
var switchesSelectors = require("../../lib/switches-selectors.js");
var noop = require("../../lib/util/noop");
var pjax = {
onSwitch: function() {
2019-03-03 01:37:45 -05:00
console.log("Switched");
},
state: {},
log: noop
2019-03-03 01:37:45 -05:00
};
2016-01-05 14:12:52 +11:00
// @author darylteo
tape("test switchesSelectors", function(t) {
2016-01-05 14:12:52 +11:00
// switchesSelectors relies on a higher level function callback
// should really be passed in instead so I'll leave it here as a TODO:
2019-03-03 01:37:45 -05:00
var tmpEl = document.implementation.createHTMLDocument();
2016-01-05 14:12:52 +11:00
// a div container is used because swapping the containers
// will generate a new element, so things get weird
// using "body" generates a lot of testling cruft that I don't
// want so let's avoid that
2019-03-03 01:37:45 -05:00
var container = document.createElement("div");
container.innerHTML = "<p>Original Text</p><span>No Change</span>";
document.body.appendChild(container);
2016-01-05 14:12:52 +11:00
2019-03-03 01:37:45 -05:00
var container2 = tmpEl.createElement("div");
container2.innerHTML = "<p>New Text</p><span>New Span</span>";
tmpEl.body.appendChild(container2);
2016-01-05 14:12:52 +11:00
switchesSelectors.bind(pjax)(
{}, // switches
{}, // switchesOptions
2018-01-09 00:44:20 -05:00
["p"], // selectors,
2016-01-05 14:12:52 +11:00
tmpEl, // fromEl
document, // toEl,
{} // options
2019-03-03 01:37:45 -05:00
);
2016-01-05 14:12:52 +11:00
2019-03-03 01:37:45 -05:00
t.equals(
container.innerHTML,
"<p>New Text</p><span>No Change</span>",
"Elements correctly switched"
);
2019-03-03 01:37:45 -05:00
t.end();
});
tape("test switchesSelectors when number of elements don't match", function(t) {
2019-03-03 01:37:45 -05:00
var newTempDoc = document.implementation.createHTMLDocument();
var originalTempDoc = document.implementation.createHTMLDocument();
// a div container is used because swapping the containers
// will generate a new element, so things get weird
// using "body" generates a lot of testling cruft that I don't
// want so let's avoid that
2019-03-03 01:37:45 -05:00
var container = originalTempDoc.createElement("div");
container.innerHTML = "<p>Original text</p><span>No change</span>";
originalTempDoc.body.appendChild(container);
2019-03-03 01:37:45 -05:00
var container2 = newTempDoc.createElement("div");
container2.innerHTML =
"<p>New text</p><p>More new text</p><span>New span</span>";
newTempDoc.body.appendChild(container2);
2019-03-03 01:37:45 -05:00
var switchSelectorsFn = switchesSelectors.bind(
pjax,
{}, // switches
{}, // switchesOptions
["p"], // selectors,
newTempDoc, // fromEl
originalTempDoc, // toEl,
{} // options
2019-03-03 01:37:45 -05:00
);
2019-03-03 01:37:45 -05:00
t.throws(
switchSelectorsFn,
null,
"error was thrown properly since number of elements don't match"
);
2019-03-03 01:37:45 -05:00
t.end();
});