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 1e32e04..cea115d 100644
--- a/tests/lib/switch-selectors.js
+++ b/tests/lib/switch-selectors.js
@@ -1,18 +1,20 @@
var tape = require("tape")
var switchesSelectors = require("../../lib/switches-selectors.js")
+var noop = require("../../lib/util/noop")
+
+var pjax = {
+ onSwitch: function() {
+ console.log("Switched")
+ },
+ state: {},
+ log: noop
+}
// @author darylteo
tape("test switchesSelectors", function(t) {
// switchesSelectors relies on a higher level function callback
// should really be passed in instead so I'll leave it here as a TODO:
- var pjax = {
- onSwitch: function() {
- console.log("Switched")
- },
- state: {}
- }
-
var tmpEl = document.implementation.createHTMLDocument()
// a div container is used because swapping the containers
@@ -40,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/switches.js b/tests/lib/switches.js
index 1752cb8..ecf5ed8 100644
--- a/tests/lib/switches.js
+++ b/tests/lib/switches.js
@@ -2,6 +2,60 @@ var tape = require("tape")
var switches = require("../../lib/switches")
var noop = require("../../lib/util/noop")
+tape("test outerHTML switch", function(t) {
+ var outerHTML = switches.outerHTML
+
+ var doc = document.implementation.createHTMLDocument()
+
+ var container = doc.createElement("div")
+ container.innerHTML = "
Original Text
"
+ doc.body.appendChild(container)
+
+ var p = doc.createElement("p")
+ p.innerHTML = "New Text"
+
+ outerHTML.bind({
+ onSwitch: noop
+ })(doc.querySelector("p"), p)
+
+ t.equals(doc.querySelector("p").innerHTML, "New Text", "Elements correctly switched")
+ t.notEquals(doc.querySelector("p").id, "p", "other attributes overwritten correctly")
+
+ t.end()
+})
+
+tape("test innerHTML switch", function(t) {
+ var innerHTML = switches.innerHTML
+
+ var doc = document.implementation.createHTMLDocument()
+
+ var container = doc.createElement("div")
+ container.innerHTML = "
Original Text
"
+ doc.body.appendChild(container)
+
+ var p = doc.createElement("p")
+ p.innerHTML = "New Text"
+ p.className = "p"
+
+ innerHTML.bind({
+ onSwitch: noop
+ })(doc.querySelector("p"), p)
+
+ t.equals(doc.querySelector("p").innerHTML, "New Text", "Elements correctly switched")
+ t.equals(doc.querySelector("p").className, "p", "classname set correctly")
+ t.equals(doc.querySelector("p").id, "p", "other attributes set correctly")
+
+ p.removeAttribute("class")
+
+ innerHTML.bind({
+ onSwitch: noop
+ })(doc.querySelector("p"), p)
+
+ t.equals(doc.querySelector("p").className, "", "classname set correctly")
+
+ t.end()
+})
+
tape("test replaceNode switch", function(t) {
var replaceNode = switches.replaceNode
diff --git a/tests/lib/util/extend.js b/tests/lib/util/extend.js
index 0319797..add707f 100644
--- a/tests/lib/util/extend.js
+++ b/tests/lib/util/extend.js
@@ -4,13 +4,14 @@ var extend = require("../../../lib/util/extend")
tape("test extend method", function(t) {
var obj = {one: 1, two: 2}
+
var extended = extend({}, obj, {two: "two", three: 3})
-
t.notEqual(obj, extended, "extended object isn't the original object")
-
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()
})
diff --git a/tests/lib/util/noop.js b/tests/lib/util/noop.js
new file mode 100644
index 0000000..b9eda26
--- /dev/null
+++ b/tests/lib/util/noop.js
@@ -0,0 +1,9 @@
+var tape = require("tape")
+
+var noop = require("../../../lib/util/noop")
+
+tape("test noop function", function(t) {
+ t.equal(typeof noop, "function", "noop is a function")
+ t.equal(noop(), undefined, "noop() returns nothing")
+ t.end()
+})