2019-03-03 01:37:45 -05:00
|
|
|
var tape = require("tape");
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
var on = require("../../../lib/events/on");
|
|
|
|
|
var trigger = require("../../../lib/events/trigger");
|
|
|
|
|
var attachForm = require("../../../lib/proto/attach-form");
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
var attr = "data-pjax-state";
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
tape("test attach form prototype method", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var form = document.createElement("form");
|
|
|
|
|
var loadUrlCalled = false;
|
|
|
|
|
|
|
|
|
|
attachForm.call(
|
|
|
|
|
{
|
|
|
|
|
options: {
|
|
|
|
|
currentUrlFullReload: true
|
|
|
|
|
},
|
|
|
|
|
loadUrl: function() {
|
|
|
|
|
loadUrlCalled = true;
|
|
|
|
|
}
|
2017-09-18 14:13:39 +02:00
|
|
|
},
|
2019-03-03 01:37:45 -05:00
|
|
|
form
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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 = 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 = "";
|
|
|
|
|
|
|
|
|
|
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.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");
|
|
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
tape("test attach form preventDefaulted events", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var loadUrlCalled = false;
|
|
|
|
|
var form = document.createElement("form");
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
// This needs to be before the call to attachForm()
|
2019-03-03 01:37:45 -05:00
|
|
|
on(form, "submit", function(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
attachForm.call(
|
|
|
|
|
{
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function() {
|
|
|
|
|
loadUrlCalled = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
form
|
|
|
|
|
);
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
form.action = "#";
|
|
|
|
|
trigger(form, "submit");
|
|
|
|
|
t.equal(
|
|
|
|
|
loadUrlCalled,
|
|
|
|
|
false,
|
|
|
|
|
"events that are preventDefaulted should not fire callback"
|
|
|
|
|
);
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
2018-01-23 13:22:48 -05:00
|
|
|
|
|
|
|
|
tape("test options are not modified by attachForm", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var form = document.createElement("form");
|
|
|
|
|
var options = { foo: "bar" };
|
|
|
|
|
var loadUrl = function() {};
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape("test form elements parsed correctly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.plan(1);
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
];
|
2018-04-09 23:36:32 -04:00
|
|
|
var pjax = {
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function(href, options) {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.same(
|
|
|
|
|
options.requestOptions.requestParams,
|
|
|
|
|
params,
|
|
|
|
|
"form elements parsed correctly"
|
|
|
|
|
);
|
2018-04-09 23:36:32 -04:00
|
|
|
}
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
attachForm.call(pjax, form);
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
form.action =
|
|
|
|
|
window.location.protocol + "//" + window.location.host + "/internal";
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
trigger(form, "submit");
|
2018-04-09 23:36:32 -04:00
|
|
|
// see loadUrl defined above
|
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
2018-04-29 15:05:22 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
tape('test form.enctype="multipart/form-data"', function(t) {
|
|
|
|
|
t.plan(4);
|
2018-04-29 15:05:22 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
var form = document.createElement("form");
|
|
|
|
|
form.enctype = "multipart/form-data";
|
|
|
|
|
var input = document.createElement("input");
|
|
|
|
|
input.name = "input";
|
|
|
|
|
input.value = "value";
|
|
|
|
|
form.appendChild(input);
|
2018-04-29 15:05:22 -04:00
|
|
|
|
|
|
|
|
var pjax = {
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function(href, options) {
|
2019-03-03 01:37:45 -05:00
|
|
|
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"
|
|
|
|
|
);
|
2018-04-29 15:05:22 -04:00
|
|
|
}
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-04-29 15:05:22 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
attachForm.call(pjax, form);
|
2018-04-29 15:05:22 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
form.action =
|
|
|
|
|
window.location.protocol + "//" + window.location.host + "/internal";
|
2018-04-29 15:05:22 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
trigger(form, "submit");
|
2018-04-29 15:05:22 -04:00
|
|
|
// see loadUrl defined above
|
|
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|