diff --git a/tests/lib/execute-scripts.js b/tests/lib/execute-scripts.js index 2fdd39e..0d93e15 100644 --- a/tests/lib/execute-scripts.js +++ b/tests/lib/execute-scripts.js @@ -14,3 +14,16 @@ tape("test executeScripts method when the script tag is inside a container", fun t.end() }) + +tape("test executeScripts method with just a script tag", function(t) { + document.body.className = "" + + var script = document.createElement("script") + script.innerHTML = "document.body.className = 'executed correctly';" + + t.equal(document.body.className, "", "script hasn't been executed yet") + executeScripts(script) + t.equal(document.body.className, "executed correctly", "script has been properly executed") + + t.end() +}) diff --git a/tests/lib/proto/attach-form.js b/tests/lib/proto/attach-form.js index fbb243d..249afc8 100644 --- a/tests/lib/proto/attach-form.js +++ b/tests/lib/proto/attach-form.js @@ -98,3 +98,57 @@ tape("test options are not modified by attachForm", function(t) { t.end() }) + +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() +}) diff --git a/tests/lib/proto/attach-link.js b/tests/lib/proto/attach-link.js index 113ea83..fb88682 100644 --- a/tests/lib/proto/attach-link.js +++ b/tests/lib/proto/attach-link.js @@ -88,3 +88,56 @@ tape("test options are not modified by attachLink", function(t) { t.end() }) + +tape("test link triggered by keyboard", function(t) { + 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.plan(3) + + attachLink.call(pjax, a) + + 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: 13, metaKey: true}) + t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior") + + trigger(a, "keyup", {keyCode: 13}) + // see loadUrl defined above + + 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") + } + } + + 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() +}) diff --git a/tests/lib/proto/parse-element.js b/tests/lib/proto/parse-element.js index 9d64e34..e58ef67 100644 --- a/tests/lib/proto/parse-element.js +++ b/tests/lib/proto/parse-element.js @@ -18,5 +18,10 @@ tape("test parse element prototype method", function(t) { parseElement.call(pjax, form) }, "
element can be parsed") + t.throws(function() { + var el = document.createElement("div") + parseElement.call(pjax, el) + }, "
element cannot be parsed") + t.end() }) diff --git a/tests/lib/send-request.js b/tests/lib/send-request.js index 8b86726..32d6de9 100644 --- a/tests/lib/send-request.js +++ b/tests/lib/send-request.js @@ -57,3 +57,81 @@ tape("request headers are sent properly", function(t) { }) }) +tape("HTTP status codes other than 200 are handled properly", function(t) { + var url = "https://httpbin.org/status/400" + + sendRequest(url, {}, function(responseText, request) { + t.equals(responseText, null, "responseText is null") + t.equals(request.status, 400, "HTTP status code is correct") + + t.end() + }) +}) + +tape.skip("XHR error is handled properly", function(t) { + var url = "https://encrypted.google.com/foobar" + + sendRequest(url, {}, function(responseText) { + t.equals(responseText, null, "responseText is null") + + t.end() + }) +}) + +tape("POST body data is sent properly", function(t) { + var url = "https://httpbin.org/post" + var params = [{ + name: "test", + value: "1" + }]; + var options = { + requestOptions: { + requestMethod: "POST", + requestParams: params + } + } + + sendRequest(url, options, function(responseText) { + var response = JSON.parse(responseText) + + t.same(response.form[params[0].name], params[0].value, "requestParams were sent properly") + t.equals(response.headers["Content-Type"], "application/x-www-form-urlencoded", "Content-Type header was set properly") + + t.end() + }) +}) + +tape("GET query data is sent properly", function(t) { + var url = "https://httpbin.org/get" + var params = [{ + name: "test", + value: "1" + }]; + var options = { + requestOptions: { + requestParams: params + } + } + + sendRequest(url, options, function(responseText) { + var response = JSON.parse(responseText) + + t.same(response.args[params[0].name], params[0].value, "requestParams were sent properly") + + t.end() + }) +}) + +tape("XHR timeout is handled properly", function(t) { + var url = "https://httpbin.org/delay/5" + var options = { + timeout: 1000 + } + + sendRequest(url, options, function(responseText) { + t.equals(responseText, null, "responseText is null") + + t.end() + }) +}) + diff --git a/tests/lib/switch-selectors.js b/tests/lib/switch-selectors.js index a999d9a..cea115d 100644 --- a/tests/lib/switch-selectors.js +++ b/tests/lib/switch-selectors.js @@ -42,3 +42,33 @@ tape("test switchesSelectors", function(t) { t.end() }) + +tape("test switchesSelectors when number of elements don't match", function(t) { + 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 + var container = originalTempDoc.createElement("div") + container.innerHTML = "

Original text

No change" + originalTempDoc.body.appendChild(container) + + var container2 = newTempDoc.createElement("div") + container2.innerHTML = "

New text

More new text

New span" + newTempDoc.body.appendChild(container2) + + var switchSelectorsFn = switchesSelectors.bind(pjax, + {}, // switches + {}, // switchesOptions + ["p"], // selectors, + newTempDoc, // fromEl + originalTempDoc, // toEl, + {} // options + ) + + t.throws(switchSelectorsFn, null, "error was thrown properly since number of elements don't match") + + t.end() +}) diff --git a/tests/lib/util/extend.js b/tests/lib/util/extend.js index f90468b..add707f 100644 --- a/tests/lib/util/extend.js +++ b/tests/lib/util/extend.js @@ -10,5 +10,8 @@ tape("test extend method", function(t) { t.notSame(obj, extended, "extended object doesn't have the same values as original object") t.notSame(obj.two, extended.two, "extended object value overwrites value from original object") + extended = extend(null) + t.equals(extended, null, "passing null returns null") + t.end() })