Prettier fixes

This commit is contained in:
Behind The Math
2019-03-03 01:37:45 -05:00
parent 3c1a4b2e18
commit c13149626b
32 changed files with 1318 additions and 903 deletions

View File

@@ -1,160 +1,231 @@
var tape = require("tape")
var tape = require("tape");
var on = require("../../../lib/events/on")
var trigger = require("../../../lib/events/trigger")
var attachForm = require("../../../lib/proto/attach-form")
var on = require("../../../lib/events/on");
var trigger = require("../../../lib/events/trigger");
var attachForm = require("../../../lib/proto/attach-form");
var attr = "data-pjax-state"
var attr = "data-pjax-state";
tape("test attach form prototype method", function(t) {
var form = document.createElement("form")
var loadUrlCalled = false
var form = document.createElement("form");
var loadUrlCalled = false;
attachForm.call({
options: {
currentUrlFullReload: true
attachForm.call(
{
options: {
currentUrlFullReload: true
},
loadUrl: function() {
loadUrlCalled = true;
}
},
loadUrl: function() {
loadUrlCalled = true
}
}, form)
form
);
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
var internalUri =
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
window.location.search;
form.action = "http://external.com/"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "external", "external url stop behavior")
form.action = "http://external.com/";
trigger(form, "submit");
t.equal(form.getAttribute(attr), "external", "external url stop behavior");
form.action = internalUri + "#anchor"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor", "internal anchor stop behavior")
form.action = internalUri + "#anchor";
trigger(form, "submit");
t.equal(form.getAttribute(attr), "anchor", "internal anchor stop behavior");
window.location.hash = "#anchor"
form.action = internalUri + "#another-anchor"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor", "different anchors stop behavior")
window.location.hash = ""
window.location.hash = "#anchor";
form.action = internalUri + "#another-anchor";
trigger(form, "submit");
t.equal(form.getAttribute(attr), "anchor", "different anchors stop behavior");
window.location.hash = "";
form.action = internalUri + "#"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
form.action = internalUri + "#";
trigger(form, "submit");
t.equal(
form.getAttribute(attr),
"anchor-empty",
"empty anchor stop behavior"
);
form.action = window.location.href
trigger(form, "submit")
t.equal(form.getAttribute(attr), "reload", "submitting when currentUrlFullReload is true will submit normally, without XHR")
t.equal(loadUrlCalled, false, "loadUrl() not called")
form.action = window.location.href;
trigger(form, "submit");
t.equal(
form.getAttribute(attr),
"reload",
"submitting when currentUrlFullReload is true will submit normally, without XHR"
);
t.equal(loadUrlCalled, false, "loadUrl() not called");
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "POST"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "submit", "triggering a POST request to the next page")
t.equal(loadUrlCalled, true, "loadUrl() called correctly")
form.action =
window.location.protocol + "//" + window.location.host + "/internal";
form.method = "POST";
trigger(form, "submit");
t.equal(
form.getAttribute(attr),
"submit",
"triggering a POST request to the next page"
);
t.equal(loadUrlCalled, true, "loadUrl() called correctly");
loadUrlCalled = false
form.setAttribute(attr, "")
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "GET"
trigger(form, "submit")
t.equal(form.getAttribute(attr), "submit", "triggering a GET request to the next page")
t.equal(loadUrlCalled, true, "loadUrl() called correctly")
loadUrlCalled = false;
form.setAttribute(attr, "");
form.action =
window.location.protocol + "//" + window.location.host + "/internal";
form.method = "GET";
trigger(form, "submit");
t.equal(
form.getAttribute(attr),
"submit",
"triggering a GET request to the next page"
);
t.equal(loadUrlCalled, true, "loadUrl() called correctly");
t.end()
})
t.end();
});
tape("test attach form preventDefaulted events", function(t) {
var loadUrlCalled = false
var form = document.createElement("form")
var loadUrlCalled = false;
var form = document.createElement("form");
// This needs to be before the call to attachForm()
on(form, "submit", function(event) { event.preventDefault() })
on(form, "submit", function(event) {
event.preventDefault();
});
attachForm.call({
options: {},
loadUrl: function() {
loadUrlCalled = true
}
}, form)
attachForm.call(
{
options: {},
loadUrl: function() {
loadUrlCalled = true;
}
},
form
);
form.action = "#"
trigger(form, "submit")
t.equal(loadUrlCalled, false, "events that are preventDefaulted should not fire callback")
form.action = "#";
trigger(form, "submit");
t.equal(
loadUrlCalled,
false,
"events that are preventDefaulted should not fire callback"
);
t.end()
})
t.end();
});
tape("test options are not modified by attachForm", function(t) {
var form = document.createElement("form")
var options = {foo: "bar"}
var loadUrl = function() {}
var form = document.createElement("form");
var options = { foo: "bar" };
var loadUrl = function() {};
attachForm.call({options: options, loadUrl: loadUrl}, form)
attachForm.call({ options: options, loadUrl: loadUrl }, form);
form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
form.method = "GET"
trigger(form, "submit")
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.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();
});
tape("test form elements parsed correctly", function(t) {
t.plan(1)
t.plan(1);
var form = document.createElement("form")
var input = document.createElement("input")
input.name = "input"
input.value = "value"
form.appendChild(input)
var form = document.createElement("form");
var input = document.createElement("input");
input.name = "input";
input.value = "value";
form.appendChild(input);
var params = [{
name: "input",
value: "value"
}]
var params = [
{
name: "input",
value: "value"
}
];
var pjax = {
options: {},
loadUrl: function(href, options) {
t.same(options.requestOptions.requestParams, params, "form elements parsed correctly")
t.same(
options.requestOptions.requestParams,
params,
"form elements parsed correctly"
);
}
}
};
attachForm.call(pjax, form)
attachForm.call(pjax, form);
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.action =
window.location.protocol + "//" + window.location.host + "/internal";
trigger(form, "submit")
trigger(form, "submit");
// see loadUrl defined above
t.end()
})
t.end();
});
tape("test form.enctype=\"multipart/form-data\"", function(t) {
t.plan(4)
tape('test form.enctype="multipart/form-data"', function(t) {
t.plan(4);
var form = document.createElement("form")
form.enctype = "multipart/form-data"
var input = document.createElement("input")
input.name = "input"
input.value = "value"
form.appendChild(input)
var form = document.createElement("form");
form.enctype = "multipart/form-data";
var input = document.createElement("input");
input.name = "input";
input.value = "value";
form.appendChild(input);
var pjax = {
options: {},
loadUrl: function(href, options) {
t.equals(options.requestOptions.requestParams, undefined, "form elements not parsed manually")
t.true(options.requestOptions.formData instanceof FormData, "requestOptions.formData is a FormData")
t.equals(Array.from(options.requestOptions.formData.entries()).length, 1, "correct number of FormData elements")
t.equals(options.requestOptions.formData.get("input"), "value", "FormData element value set correctly")
t.equals(
options.requestOptions.requestParams,
undefined,
"form elements not parsed manually"
);
t.true(
options.requestOptions.formData instanceof FormData,
"requestOptions.formData is a FormData"
);
t.equals(
Array.from(options.requestOptions.formData.entries()).length,
1,
"correct number of FormData elements"
);
t.equals(
options.requestOptions.formData.get("input"),
"value",
"FormData element value set correctly"
);
}
}
};
attachForm.call(pjax, form)
attachForm.call(pjax, form);
form.action = window.location.protocol + "//" + window.location.host + "/internal"
form.action =
window.location.protocol + "//" + window.location.host + "/internal";
trigger(form, "submit")
trigger(form, "submit");
// see loadUrl defined above
t.end()
})
t.end();
});

View File

@@ -1,143 +1,190 @@
var tape = require("tape")
var tape = require("tape");
var on = require("../../../lib/events/on")
var trigger = require("../../../lib/events/trigger")
var attachLink = require("../../../lib/proto/attach-link")
var on = require("../../../lib/events/on");
var trigger = require("../../../lib/events/trigger");
var attachLink = require("../../../lib/proto/attach-link");
var attr = "data-pjax-state"
var attr = "data-pjax-state";
tape("test attach link prototype method", function(t) {
var a = document.createElement("a")
var loadUrlCalled = false
var a = document.createElement("a");
var loadUrlCalled = false;
attachLink.call({
options: {},
loadUrl: function() {
loadUrlCalled = true
}
}, a)
attachLink.call(
{
options: {},
loadUrl: function() {
loadUrlCalled = true;
}
},
a
);
var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
var internalUri =
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
window.location.search;
a.href = internalUri
trigger(a, "click", {metaKey: true})
t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior")
a.href = internalUri;
trigger(a, "click", { metaKey: true });
t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior");
a.href = "http://external.com/"
trigger(a, "click")
t.equal(a.getAttribute(attr), "external", "external url stop behavior")
a.href = "http://external.com/";
trigger(a, "click");
t.equal(a.getAttribute(attr), "external", "external url stop behavior");
window.location.hash = "#anchor"
a.href = internalUri + "#anchor"
trigger(a, "click")
t.equal(a.getAttribute(attr), "anchor", "internal anchor stop behavior")
window.location.hash = "#anchor";
a.href = internalUri + "#anchor";
trigger(a, "click");
t.equal(a.getAttribute(attr), "anchor", "internal anchor stop behavior");
a.href = internalUri + "#another-anchor"
trigger(a, "click")
t.equal(a.getAttribute(attr), "anchor", "different anchors stop behavior")
window.location.hash = ""
a.href = internalUri + "#another-anchor";
trigger(a, "click");
t.equal(a.getAttribute(attr), "anchor", "different anchors stop behavior");
window.location.hash = "";
a.href = internalUri + "#"
trigger(a, "click")
t.equal(a.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
a.href = internalUri + "#";
trigger(a, "click");
t.equal(a.getAttribute(attr), "anchor-empty", "empty anchor stop behavior");
a.href = window.location.protocol + "//" + window.location.host + "/internal"
trigger(a, "click")
t.equals(a.getAttribute(attr), "load", "triggering an internal link sets the state attribute to 'load'")
t.equals(loadUrlCalled, true, "triggering an internal link actually loads the page")
a.href = window.location.protocol + "//" + window.location.host + "/internal";
trigger(a, "click");
t.equals(
a.getAttribute(attr),
"load",
"triggering an internal link sets the state attribute to 'load'"
);
t.equals(
loadUrlCalled,
true,
"triggering an internal link actually loads the page"
);
t.end()
})
t.end();
});
tape("test attach link preventDefaulted events", function(t) {
var loadUrlCalled = false
var a = document.createElement("a")
var loadUrlCalled = false;
var a = document.createElement("a");
// This needs to be before the call to attachLink()
on(a, "click", function(event) {
event.preventDefault()
})
event.preventDefault();
});
attachLink.call({
options: {},
loadUrl: function() {
loadUrlCalled = true
}
}, a)
attachLink.call(
{
options: {},
loadUrl: function() {
loadUrlCalled = true;
}
},
a
);
a.href = "#"
trigger(a, "click")
t.equal(loadUrlCalled, false, "events that are preventDefaulted should not fire callback")
a.href = "#";
trigger(a, "click");
t.equal(
loadUrlCalled,
false,
"events that are preventDefaulted should not fire callback"
);
t.end()
})
t.end();
});
tape("test options are not modified by attachLink", function(t) {
var a = document.createElement("a")
var options = {foo: "bar"}
var loadUrl = function() {}
var a = document.createElement("a");
var options = { foo: "bar" };
var loadUrl = function() {};
attachLink.call({options: options, loadUrl: loadUrl}, a)
attachLink.call({ options: options, loadUrl: loadUrl }, a);
a.href = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
a.href =
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
window.location.search;
trigger(a, "click")
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.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();
});
tape("test link triggered by keyboard", function(t) {
var a = document.createElement("a")
var a = document.createElement("a");
var pjax = {
options: {},
loadUrl: function() {
t.equal(a.getAttribute(attr), "load", "triggering a internal link actually loads the page")
t.equal(
a.getAttribute(attr),
"load",
"triggering a internal link actually loads the page"
);
}
}
};
t.plan(3)
t.plan(3);
attachLink.call(pjax, a)
attachLink.call(pjax, a);
a.href = window.location.protocol + "//" + window.location.host + "/internal"
a.href = window.location.protocol + "//" + window.location.host + "/internal";
trigger(a, "keyup", {keyCode: 14})
t.equal(a.getAttribute(attr), "", "keycode other than 13 doesn't trigger anything")
trigger(a, "keyup", { keyCode: 14 });
t.equal(
a.getAttribute(attr),
"",
"keycode other than 13 doesn't trigger anything"
);
trigger(a, "keyup", {keyCode: 13, metaKey: true})
t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior")
trigger(a, "keyup", { keyCode: 13, metaKey: true });
t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior");
trigger(a, "keyup", {keyCode: 13})
trigger(a, "keyup", { keyCode: 13 });
// see loadUrl defined above
t.end()
})
t.end();
});
tape("test link with the same URL as the current one, when currentUrlFullReload set to true", function(t) {
var a = document.createElement("a")
var pjax = {
options: {
currentUrlFullReload: true
},
reload: function() {
t.pass("this.reload() was called correctly")
},
loadUrl: function() {
t.fail("loadUrl() was called wrongly")
}
tape(
"test link with the same URL as the current one, when currentUrlFullReload set to true",
function(t) {
var a = document.createElement("a");
var pjax = {
options: {
currentUrlFullReload: true
},
reload: function() {
t.pass("this.reload() was called correctly");
},
loadUrl: function() {
t.fail("loadUrl() was called wrongly");
}
};
t.plan(2);
attachLink.call(pjax, a);
a.href = window.location.href;
trigger(a, "click");
t.equal(a.getAttribute(attr), "reload", "reload stop behavior");
t.end();
}
t.plan(2)
attachLink.call(pjax, a)
a.href = window.location.href
trigger(a, "click")
t.equal(a.getAttribute(attr), "reload", "reload stop behavior")
t.end()
})
);

View File

@@ -1,39 +1,43 @@
var tape = require("tape")
var tape = require("tape");
var handleReponse = require("../../../lib/proto/handle-response")
var noop = require("../../../lib/util/noop")
var handleReponse = require("../../../lib/proto/handle-response");
var noop = require("../../../lib/util/noop");
var href = "https://example.org/"
var href = "https://example.org/";
var storeEventHandler
var pjaxErrorEventTriggerTest
var storeEventHandler;
var pjaxErrorEventTriggerTest;
tape("test events triggered when handleResponse(false) is called", function(t) {
t.plan(3)
t.plan(3);
var pjax = {
options: {
test: 1
}
}
};
var events = []
var events = [];
storeEventHandler = function(e) {
events.push(e.type)
events.push(e.type);
t.equal(e.test, 1)
}
t.equal(e.test, 1);
};
document.addEventListener("pjax:complete", storeEventHandler)
document.addEventListener("pjax:error", storeEventHandler)
document.addEventListener("pjax:complete", storeEventHandler);
document.addEventListener("pjax:error", storeEventHandler);
handleReponse.bind(pjax)(false, null)
handleReponse.bind(pjax)(false, null);
t.same(events, ["pjax:complete", "pjax:error"], "calling handleResponse(false) triggers 'pjax:complete' and 'pjax:error'")
t.same(
events,
["pjax:complete", "pjax:error"],
"calling handleResponse(false) triggers 'pjax:complete' and 'pjax:error'"
);
t.end()
})
t.end();
});
tape("test when handleResponse() is called normally", function(t) {
var pjax = {
@@ -42,167 +46,209 @@ tape("test when handleResponse() is called normally", function(t) {
},
loadContent: noop,
state: {}
}
};
var request = {
getResponseHeader: noop
};
handleReponse.bind(pjax)("", request, "href");
delete window.history.state.uid;
t.same(
window.history.state,
{
url: href,
title: "",
scrollPos: [0, 0]
},
"window.history.state is set correctly"
);
t.equals(pjax.state.href, "href", "this.state.href is set correctly");
t.equals(
Object.keys(pjax.state.options).length,
2,
"this.state.options is set correctly"
);
t.same(
pjax.state.options.request,
request,
"this.state.options is set correctly"
);
t.equals(pjax.state.options.test, 1, "this.state.options is set correctly");
t.end();
});
tape(
"test when handleResponse() is called normally with request.responseURL",
function(t) {
var pjax = {
options: {},
loadContent: noop,
state: {}
};
var request = {
responseURL: href + "1",
getResponseHeader: noop
};
handleReponse.bind(pjax)("", request, "");
t.equals(
pjax.state.href,
request.responseURL,
"this.state.href is set correctly"
);
t.end();
}
);
handleReponse.bind(pjax)("", request, "href")
delete window.history.state.uid
t.same(window.history.state, {
url: href,
title: "",
scrollPos: [0, 0]
}, "window.history.state is set correctly")
t.equals(pjax.state.href, "href", "this.state.href is set correctly")
t.equals(Object.keys(pjax.state.options).length, 2, "this.state.options is set correctly")
t.same(pjax.state.options.request, request, "this.state.options is set correctly")
t.equals(pjax.state.options.test, 1, "this.state.options is set correctly")
t.end()
})
tape("test when handleResponse() is called normally with request.responseURL", function(t) {
tape("test when handleResponse() is called normally with X-PJAX-URL", function(
t
) {
var pjax = {
options: {},
loadContent: noop,
state: {}
}
var request = {
responseURL: href + "1",
getResponseHeader: noop
}
handleReponse.bind(pjax)("", request, "")
t.equals(pjax.state.href, request.responseURL, "this.state.href is set correctly")
t.end()
})
tape("test when handleResponse() is called normally with X-PJAX-URL", function(t) {
var pjax = {
options: {},
loadContent: noop,
state: {}
}
};
var request = {
getResponseHeader: function(header) {
if (header === "X-PJAX-URL") {
return href + "2"
return href + "2";
}
}
}
};
handleReponse.bind(pjax)("", request, "")
handleReponse.bind(pjax)("", request, "");
t.equals(pjax.state.href, href + "2", "this.state.href is set correctly")
t.equals(pjax.state.href, href + "2", "this.state.href is set correctly");
t.end()
})
t.end();
});
tape("test when handleResponse() is called normally with X-XHR-Redirected-To", function(t) {
var pjax = {
options: {},
loadContent: noop,
state: {}
}
tape(
"test when handleResponse() is called normally with X-XHR-Redirected-To",
function(t) {
var pjax = {
options: {},
loadContent: noop,
state: {}
};
var request = {
getResponseHeader: function(header) {
if (header === "X-XHR-Redirected-To") {
return href + "3"
var request = {
getResponseHeader: function(header) {
if (header === "X-XHR-Redirected-To") {
return href + "3";
}
}
}
};
handleReponse.bind(pjax)("", request, "");
t.equals(pjax.state.href, href + "3", "this.state.href is set correctly");
t.end();
}
handleReponse.bind(pjax)("", request, "")
t.equals(pjax.state.href, href + "3", "this.state.href is set correctly")
t.end()
})
);
tape("test when handleResponse() is called normally with a hash", function(t) {
var pjax = {
options: {},
loadContent: noop,
state: {}
}
};
var request = {
responseURL: href + "2",
getResponseHeader: noop
}
};
handleReponse.bind(pjax)("", request, href + "1#test")
handleReponse.bind(pjax)("", request, href + "1#test");
t.equals(pjax.state.href, href + "2#test", "this.state.href is set correctly")
t.equals(
pjax.state.href,
href + "2#test",
"this.state.href is set correctly"
);
t.end()
})
t.end();
});
tape("test try...catch for loadContent() when options.debug is true", function(t) {
t.plan(2)
tape("test try...catch for loadContent() when options.debug is true", function(
t
) {
t.plan(2);
var pjax = {
options: {},
loadContent: noop,
state: {}
}
};
var request = {
getResponseHeader: noop
}
};
pjax.loadContent = function() {
throw new Error()
}
pjax.options.debug = true
throw new Error();
};
pjax.options.debug = true;
document.removeEventListener("pjax:error", storeEventHandler)
document.removeEventListener("pjax:error", storeEventHandler);
pjaxErrorEventTriggerTest = function() {
t.pass("pjax:error event triggered")
}
document.addEventListener("pjax:error", pjaxErrorEventTriggerTest)
t.pass("pjax:error event triggered");
};
document.addEventListener("pjax:error", pjaxErrorEventTriggerTest);
t.throws(function() {
handleReponse.bind(pjax)("", request, "")
}, Error, "error is rethrown")
t.throws(
function() {
handleReponse.bind(pjax)("", request, "");
},
Error,
"error is rethrown"
);
t.end()
})
t.end();
});
tape("test try...catch for loadContent()", function(t) {
t.plan(2)
t.plan(2);
var pjax = {
options: {},
loadContent: noop,
state: {}
}
};
var request = {
getResponseHeader: noop
}
};
pjax.loadContent = function() {
throw new Error()
}
throw new Error();
};
pjax.latestChance = function() {
return true
}
pjax.options.debug = false
return true;
};
pjax.options.debug = false;
document.removeEventListener("pjax:error", pjaxErrorEventTriggerTest)
document.removeEventListener("pjax:error", pjaxErrorEventTriggerTest);
t.doesNotThrow(function() {
t.equals(handleReponse.bind(pjax)("", request, ""), true, "this.latestChance() is called")
}, Error, "error is not thrown")
t.doesNotThrow(
function() {
t.equals(
handleReponse.bind(pjax)("", request, ""),
true,
"this.latestChance() is called"
);
},
Error,
"error is not thrown"
);
t.end()
})
t.end();
});

View File

@@ -1,27 +1,31 @@
var tape = require("tape")
var tape = require("tape");
var parseElement = require("../../../lib/proto/parse-element")
var parseElement = require("../../../lib/proto/parse-element");
var pjax = {
attachLink: function() { return true },
attachForm: function() { return true }
}
attachLink: function() {
return true;
},
attachForm: function() {
return true;
}
};
tape("test parse element prototype method", function(t) {
t.doesNotThrow(function() {
var a = document.createElement("a")
parseElement.call(pjax, a)
}, "<a> element can be parsed")
var a = document.createElement("a");
parseElement.call(pjax, a);
}, "<a> element can be parsed");
t.doesNotThrow(function() {
var form = document.createElement("form")
parseElement.call(pjax, form)
}, "<form> element can be parsed")
var form = document.createElement("form");
parseElement.call(pjax, form);
}, "<form> element can be parsed");
t.throws(function() {
var el = document.createElement("div")
parseElement.call(pjax, el)
}, "<div> element cannot be parsed")
var el = document.createElement("div");
parseElement.call(pjax, el);
}, "<div> element cannot be parsed");
t.end()
})
t.end();
});