Code cleanup (#120)
* Lots of code cleanup * Cleanup parse-options tests - Rename objects for clarity and inline unneeded objects - Remove unneeded tests - Use Object.keys().length instead of a custom function - Use typeof === "object" instead of a custom function that checks the prototype tree as well, since we don't expect anything but an object literal. * Remove old switchFallback code * Remove polyfill for Function.prototype.bind * Inline small functions * Add more documentation and tests for options.currentUrlFullReload Closes #17 * Update package.json
This commit was merged in pull request #120.
This commit is contained in:
@@ -18,8 +18,8 @@ if (!("responseURL" in XMLHttpRequest.prototype)) {
|
||||
tape("test aborting xhr request", function(t) {
|
||||
var requestCacheBust = sendRequest.bind({
|
||||
options: {
|
||||
cacheBust: true,
|
||||
},
|
||||
cacheBust: true
|
||||
}
|
||||
})
|
||||
|
||||
t.test("- pending request is aborted", function(t) {
|
||||
|
||||
@@ -7,8 +7,7 @@ var trigger = require("../../lib/events/trigger")
|
||||
var el = document.createElement("div")
|
||||
var el2 = document.createElement("span")
|
||||
var els = [el, el2]
|
||||
// var eventType2 = "resize"
|
||||
// var eventsType = "click resize"
|
||||
|
||||
var classCb = function() {
|
||||
this.className += "on"
|
||||
}
|
||||
@@ -92,14 +91,14 @@ tape("test events on/off/trigger for multiple elements, multiple events", functi
|
||||
})
|
||||
|
||||
tape("test events on top level elements", function(t) {
|
||||
var el = document;
|
||||
var el = document
|
||||
|
||||
el.className = ""
|
||||
on(el, "click", classCb)
|
||||
trigger(el, "click")
|
||||
t.equal(el.className, "on", "attached callback has been fired properly on document")
|
||||
|
||||
el = window;
|
||||
el = window
|
||||
|
||||
el.className = ""
|
||||
// With jsdom, the default this is global, not window, so we need to explicitly bind to window.
|
||||
|
||||
@@ -80,17 +80,16 @@ tape("test attach form preventDefaulted events", function(t) {
|
||||
tape("test options are not modified by attachForm", function(t) {
|
||||
var form = document.createElement("form")
|
||||
var options = {foo: "bar"}
|
||||
var loadUrl = () => {}
|
||||
var loadUrl = function() {}
|
||||
|
||||
attachForm.call({options, loadUrl}, form)
|
||||
attachForm.call({options: options, loadUrl: loadUrl}, form)
|
||||
|
||||
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||
form.action = internalUri
|
||||
form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||
form.method = "GET"
|
||||
trigger(form, "submit")
|
||||
|
||||
t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified")
|
||||
t.equal("bar", options.foo, "options object that is passed in should not be modified")
|
||||
|
||||
t.end();
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -79,17 +79,16 @@ tape("test attach link preventDefaulted events", function(t) {
|
||||
tape("test options are not modified by attachLink", function(t) {
|
||||
var a = document.createElement("a")
|
||||
var options = {foo: "bar"}
|
||||
var loadUrl = () => {};
|
||||
var loadUrl = function() {}
|
||||
|
||||
attachLink.call({options, loadUrl}, a)
|
||||
attachLink.call({options: options, loadUrl: loadUrl}, a)
|
||||
|
||||
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||
a.href = internalUri
|
||||
a.href = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
||||
|
||||
trigger(a, "click")
|
||||
|
||||
t.equal(1, Object.keys(options).length, "options object that is passed in should not be modified")
|
||||
t.equal("bar", options.foo, "options object that is passed in should not be modified")
|
||||
|
||||
t.end();
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -2,72 +2,48 @@ var tape = require("tape")
|
||||
|
||||
var parseOptions = require("../../../lib/proto/parse-options.js")
|
||||
tape("test parse initalization options function", function(t) {
|
||||
// via http://stackoverflow.com/questions/1173549/how-to-determine-if-an-object-is-an-object-literal-in-javascript
|
||||
function isObjLiteral(_obj) {
|
||||
var _test = _obj;
|
||||
return (typeof _obj !== "object" || _obj === null ?
|
||||
false :
|
||||
(
|
||||
(function() {
|
||||
while (!false) {
|
||||
if (Object.getPrototypeOf(_test = Object.getPrototypeOf(_test)) === null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Object.getPrototypeOf(_obj) === _test;
|
||||
})()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function enumerableKeys(_obj) {
|
||||
var c = 0;
|
||||
for (var n in _obj) {
|
||||
n = n;
|
||||
c++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
t.test("- default options", function(t) {
|
||||
var body1 = {};
|
||||
var options1 = {};
|
||||
parseOptions.apply(body1, [options1]);
|
||||
var pjax = {}
|
||||
parseOptions.call(pjax, {})
|
||||
|
||||
t.equal(body1.options.elements, "a[href], form[action]");
|
||||
t.equal(body1.options.selectors.length, 2, "selectors length");
|
||||
t.equal(body1.options.selectors[0], "title");
|
||||
t.equal(body1.options.selectors[1], ".js-Pjax");
|
||||
t.equal(isObjLiteral(body1.options.switches), true);
|
||||
t.equal(enumerableKeys(body1.options.switches), 2);// head and body
|
||||
t.equal(isObjLiteral(body1.options.switchesOptions), true);
|
||||
t.equal(enumerableKeys(body1.options.switchesOptions), 0);
|
||||
t.equal(body1.options.history, true);
|
||||
t.equal(typeof body1.options.analytics, "function");
|
||||
t.equal(body1.options.scrollTo, 0);
|
||||
t.equal(body1.options.scrollRestoration, true);
|
||||
t.equal(body1.options.cacheBust, true);
|
||||
t.equal(body1.options.debug, false);
|
||||
t.end();
|
||||
});
|
||||
t.equal(pjax.options.elements, "a[href], form[action]")
|
||||
t.equal(pjax.options.selectors.length, 2, "selectors length")
|
||||
t.equal(pjax.options.selectors[0], "title")
|
||||
t.equal(pjax.options.selectors[1], ".js-Pjax")
|
||||
|
||||
t.equal(typeof pjax.options.switches, "object")
|
||||
t.equal(Object.keys(pjax.options.switches).length, 2)// head and body
|
||||
|
||||
t.equal(typeof pjax.options.switchesOptions, "object")
|
||||
t.equal(Object.keys(pjax.options.switchesOptions).length, 0)
|
||||
|
||||
t.equal(pjax.options.history, true)
|
||||
t.equal(typeof pjax.options.analytics, "function")
|
||||
t.equal(pjax.options.scrollTo, 0)
|
||||
t.equal(pjax.options.scrollRestoration, true)
|
||||
t.equal(pjax.options.cacheBust, true)
|
||||
t.equal(pjax.options.debug, false)
|
||||
t.equal(pjax.options.currentUrlFullReload, false)
|
||||
t.end()
|
||||
})
|
||||
|
||||
// verify analytics always ends up as a function even when passed not a function
|
||||
t.test("- analytics is a function", function(t) {
|
||||
var body2 = {};
|
||||
var options2 = {analytics: "some string"};
|
||||
parseOptions.apply(body2, [options2]);
|
||||
var pjax = {}
|
||||
parseOptions.call(pjax, {analytics: "some string"})
|
||||
|
||||
t.deepEqual(typeof pjax.options.analytics, "function")
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.deepEqual(typeof body2.options.analytics, "function");
|
||||
t.end();
|
||||
});
|
||||
// verify that the value false for scrollTo is not squashed
|
||||
t.test("- scrollTo remains false", function(t) {
|
||||
var body3 = {};
|
||||
var options3 = {scrollTo: false};
|
||||
parseOptions.apply(body3, [options3]);
|
||||
var pjax = {}
|
||||
parseOptions.call(pjax, {scrollTo: false})
|
||||
|
||||
t.deepEqual(pjax.options.scrollTo, false)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.deepEqual(body3.options.scrollTo, false);
|
||||
t.end();
|
||||
});
|
||||
t.end()
|
||||
})
|
||||
|
||||
@@ -20,9 +20,9 @@ tape("test xhr request", function(t) {
|
||||
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
||||
var requestCacheBust = sendRequest.bind({
|
||||
options: {
|
||||
cacheBust: true,
|
||||
},
|
||||
});
|
||||
cacheBust: true
|
||||
}
|
||||
})
|
||||
var r = requestCacheBust(url, {}, function(result) {
|
||||
t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be")
|
||||
try {
|
||||
@@ -38,9 +38,9 @@ tape("test xhr request", function(t) {
|
||||
t.test("- request is not cache-busted when configured not to be", function(t) {
|
||||
var requestNoCacheBust = sendRequest.bind({
|
||||
options: {
|
||||
cacheBust: false,
|
||||
},
|
||||
});
|
||||
cacheBust: false
|
||||
}
|
||||
})
|
||||
var r = requestNoCacheBust(url, {}, function() {
|
||||
t.equal(r.responseURL, url, "XHR URL is left untouched")
|
||||
t.end()
|
||||
|
||||
Reference in New Issue
Block a user