2017-09-18 14:13:39 +02:00
|
|
|
var tape = require("tape")
|
|
|
|
|
|
|
|
|
|
var on = require("../../../lib/events/on")
|
|
|
|
|
var trigger = require("../../../lib/events/trigger")
|
|
|
|
|
var attachForm = require("../../../lib/proto/attach-form")
|
|
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
var attr = "data-pjax-state"
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
tape("test attach form prototype method", function(t) {
|
2018-04-09 23:36:32 -04:00
|
|
|
var form = document.createElement("form")
|
|
|
|
|
var loadUrlCalled = false
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
attachForm.call({
|
2018-04-09 23:36:32 -04:00
|
|
|
options: {
|
|
|
|
|
currentUrlFullReload: true
|
2017-09-18 14:13:39 +02:00
|
|
|
},
|
|
|
|
|
loadUrl: function() {
|
2018-04-09 23:36:32 -04:00
|
|
|
loadUrlCalled = true
|
2017-09-18 14:13:39 +02: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")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(form.getAttribute(attr), "anchor", "internal anchor stop behavior")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
window.location.hash = "#anchor"
|
|
|
|
|
form.action = internalUri + "#another-anchor"
|
|
|
|
|
trigger(form, "submit")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(form.getAttribute(attr), "anchor", "different anchors stop behavior")
|
2017-09-18 14:13:39 +02:00
|
|
|
window.location.hash = ""
|
|
|
|
|
|
|
|
|
|
form.action = internalUri + "#"
|
|
|
|
|
trigger(form, "submit")
|
|
|
|
|
t.equal(form.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
|
|
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
form.action = window.location.href
|
2017-09-18 14:13:39 +02:00
|
|
|
trigger(form, "submit")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(form.getAttribute(attr), "reload", "submitting when currentUrlFullReload is true will submit normally, without XHR")
|
|
|
|
|
t.equal(loadUrlCalled, false, "loadUrl() not called")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
form.action = window.location.protocol + "//" + window.location.host + "/internal"
|
2018-01-09 00:44:20 -05:00
|
|
|
form.method = "POST"
|
2017-09-18 14:13:39 +02:00
|
|
|
trigger(form, "submit")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(form.getAttribute(attr), "submit", "triggering a POST request to the next page")
|
|
|
|
|
t.equal(loadUrlCalled, true, "loadUrl() called correctly")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
loadUrlCalled = false
|
|
|
|
|
form.setAttribute(attr, "")
|
2017-09-18 14:13:39 +02:00
|
|
|
form.action = window.location.protocol + "//" + window.location.host + "/internal"
|
2018-01-09 00:44:20 -05:00
|
|
|
form.method = "GET"
|
2017-09-18 14:13:39 +02:00
|
|
|
trigger(form, "submit")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(form.getAttribute(attr), "submit", "triggering a GET request to the next page")
|
|
|
|
|
t.equal(loadUrlCalled, true, "loadUrl() called correctly")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
tape("test attach form preventDefaulted events", function(t) {
|
2018-04-09 23:36:32 -04:00
|
|
|
var loadUrlCalled = false
|
2017-09-18 14:13:39 +02:00
|
|
|
var form = document.createElement("form")
|
|
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
// This needs to be before the call to attachForm()
|
|
|
|
|
on(form, "submit", function(event) { event.preventDefault() })
|
|
|
|
|
|
2017-09-18 14:13:39 +02:00
|
|
|
attachForm.call({
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function() {
|
2018-04-09 23:36:32 -04:00
|
|
|
loadUrlCalled = true
|
2017-09-18 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
}, form)
|
|
|
|
|
|
|
|
|
|
form.action = "#"
|
|
|
|
|
trigger(form, "submit")
|
2018-04-09 23:36:32 -04:00
|
|
|
t.equal(loadUrlCalled, false, "events that are preventDefaulted should not fire callback")
|
2017-09-18 14:13:39 +02:00
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
})
|
2018-01-23 13:22:48 -05:00
|
|
|
|
|
|
|
|
tape("test options are not modified by attachForm", function(t) {
|
|
|
|
|
var form = document.createElement("form")
|
|
|
|
|
var options = {foo: "bar"}
|
2018-02-02 09:52:44 -05:00
|
|
|
var loadUrl = function() {}
|
2018-01-23 13:22:48 -05:00
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
attachForm.call({options: options, loadUrl: loadUrl}, form)
|
2018-01-23 13:22:48 -05:00
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search
|
2018-01-23 13:22:48 -05:00
|
|
|
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")
|
|
|
|
|
|
2018-02-02 09:52:44 -05:00
|
|
|
t.end()
|
2018-01-23 13:22:48 -05:00
|
|
|
})
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape("test submit triggered by keyboard", function(t) {
|
|
|
|
|
var form = document.createElement("form")
|
|
|
|
|
var pjax = {
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function() {
|
|
|
|
|
t.equal(form.getAttribute(attr), "submit", "triggering a internal link actually submits the form")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.plan(2)
|
|
|
|
|
|
|
|
|
|
attachForm.call(pjax, form)
|
|
|
|
|
|
|
|
|
|
form.action = window.location.protocol + "//" + window.location.host + "/internal"
|
|
|
|
|
|
|
|
|
|
trigger(form, "keyup", {keyCode: 14})
|
|
|
|
|
t.equal(form.getAttribute(attr), "", "keycode other than 13 doesn't trigger anything")
|
|
|
|
|
|
|
|
|
|
trigger(form, "keyup", {keyCode: 13})
|
|
|
|
|
// see loadUrl defined above
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
tape("test form elements parsed correctly", function(t) {
|
|
|
|
|
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"
|
|
|
|
|
}]
|
|
|
|
|
var pjax = {
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function(href, options) {
|
|
|
|
|
t.same(options.requestOptions.requestParams, params, "form elements parsed correctly")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachForm.call(pjax, form)
|
|
|
|
|
|
|
|
|
|
form.action = window.location.protocol + "//" + window.location.host + "/internal"
|
|
|
|
|
|
|
|
|
|
trigger(form, "submit")
|
|
|
|
|
// see loadUrl defined above
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
})
|
2018-04-18 09:58:12 -04:00
|
|
|
|
|
|
|
|
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 pjax = {
|
|
|
|
|
options: {},
|
|
|
|
|
loadUrl: function(href, options) {
|
2018-04-27 09:49:19 -04:00
|
|
|
t.equals(options.requestOptions.requestParams, undefined, "form elements not parsed manually")
|
2018-04-18 09:58:12 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
form.action = window.location.protocol + "//" + window.location.host + "/internal"
|
|
|
|
|
|
|
|
|
|
trigger(form, "submit")
|
|
|
|
|
// see loadUrl defined above
|
|
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
|
})
|