Fix and refactors tests

This commit is contained in:
Behind The Math
2018-03-20 18:45:41 -04:00
parent cd7be77d99
commit f3dd755a97
7 changed files with 56 additions and 54 deletions

View File

@@ -14,8 +14,8 @@ tape("test evalScript method", function(t) {
t.equal(document.body.className, "executed", "script has been properly executed") t.equal(document.body.className, "executed", "script has been properly executed")
script.innerHTML = "document.write('failure')" script.innerHTML = "document.write('failure')"
document.body.text = "document.write hasn't been executed" var bodyText = "document.write hasn't been executed"
var bodyText = document.body.text document.body.text = bodyText
evalScript(script) evalScript(script)
t.equal(document.body.text, bodyText, "document.write hasn't been executed") t.equal(document.body.text, bodyText, "document.write hasn't been executed")

View File

@@ -2,7 +2,7 @@ var tape = require("tape")
var executeScripts = require("../../lib/execute-scripts") var executeScripts = require("../../lib/execute-scripts")
tape("test executeScripts method", function(t) { tape("test executeScripts method when the script tag is inside a container", function(t) {
document.body.className = "" document.body.className = ""
var container = document.createElement("div") var container = document.createElement("div")

View File

@@ -4,20 +4,18 @@ var on = require("../../../lib/events/on")
var trigger = require("../../../lib/events/trigger") var trigger = require("../../../lib/events/trigger")
var attachForm = require("../../../lib/proto/attach-form") var attachForm = require("../../../lib/proto/attach-form")
var form = document.createElement("form") var attr = "data-pjax-state"
var attr = "data-pjax-click-state"
var preventDefault = function(e) { e.preventDefault() }
tape("test attach form prototype method", function(t) { tape("test attach form prototype method", function(t) {
t.plan(7) var form = document.createElement("form")
var loadUrlCalled = false
attachForm.call({ attachForm.call({
options: {}, options: {
reload: function() { currentUrlFullReload: true
t.equal(form.getAttribute(attr), "reload", "triggering a simple reload will just submit the form")
}, },
loadUrl: function() { loadUrl: function() {
t.equal(form.getAttribute(attr), "submit", "triggering a post to the next page") loadUrlCalled = true
} }
}, form) }, form)
@@ -29,50 +27,57 @@ tape("test attach form prototype method", function(t) {
form.action = internalUri + "#anchor" form.action = internalUri + "#anchor"
trigger(form, "submit") trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor-present", "internal anchor stop behavior") t.equal(form.getAttribute(attr), "anchor", "internal anchor stop behavior")
window.location.hash = "#anchor" window.location.hash = "#anchor"
form.action = internalUri + "#another-anchor" form.action = internalUri + "#another-anchor"
trigger(form, "submit") trigger(form, "submit")
t.notEqual(form.getAttribute(attr), "anchor", "differents anchors stop behavior") t.equal(form.getAttribute(attr), "anchor", "different anchors stop behavior")
window.location.hash = "" window.location.hash = ""
form.action = internalUri + "#" form.action = internalUri + "#"
trigger(form, "submit") trigger(form, "submit")
t.equal(form.getAttribute(attr), "anchor-empty", "empty anchor stop behavior") t.equal(form.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
form.action = internalUri form.action = window.location.href
trigger(form, "submit") trigger(form, "submit")
// see reload defined above 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.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "POST" form.method = "POST"
trigger(form, "submit") trigger(form, "submit")
// see post defined above 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.action = window.location.protocol + "//" + window.location.host + "/internal"
form.method = "GET" form.method = "GET"
trigger(form, "submit") trigger(form, "submit")
// see post defined above 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) { tape("test attach form preventDefaulted events", function(t) {
var callbacked = false var loadUrlCalled = false
var form = document.createElement("form") var form = document.createElement("form")
// This needs to be before the call to attachFormk()
on(form, "submit", function(event) { event.preventDefault() })
attachForm.call({ attachForm.call({
options: {}, options: {},
loadUrl: function() { loadUrl: function() {
callbacked = true loadUrlCalled = true
} }
}, form) }, form)
form.action = "#" form.action = "#"
on(form, "submit", preventDefault)
trigger(form, "submit") trigger(form, "submit")
t.equal(callbacked, false, "events that are preventDefaulted should not fire callback") t.equal(loadUrlCalled, false, "events that are preventDefaulted should not fire callback")
t.end() t.end()
}) })

View File

@@ -4,27 +4,22 @@ var on = require("../../../lib/events/on")
var trigger = require("../../../lib/events/trigger") var trigger = require("../../../lib/events/trigger")
var attachLink = require("../../../lib/proto/attach-link") var attachLink = require("../../../lib/proto/attach-link")
var a = document.createElement("a")
var attr = "data-pjax-state" var attr = "data-pjax-state"
var preventDefault = function(e) { e.preventDefault() }
tape("test attach link prototype method", function(t) { tape("test attach link prototype method", function(t) {
t.plan(7) var a = document.createElement("a")
var loadUrlCalled = false
attachLink.call({ attachLink.call({
options: {}, options: {},
reload: function() {
t.equal(a.getAttribute(attr), "reload", "triggering exact same url reload the page")
},
loadUrl: function() { loadUrl: function() {
t.equal(a.getAttribute(attr), "load", "triggering a internal link actually load the page") loadUrlCalled = true
} }
}, a) }, 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 a.href = internalUri
on(a, "click", preventDefault) // to avoid link to be open (break testing env)
trigger(a, "click", {metaKey: true}) trigger(a, "click", {metaKey: true})
t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior") t.equal(a.getAttribute(attr), "modifier", "event key modifier stop behavior")
@@ -32,46 +27,47 @@ tape("test attach link prototype method", function(t) {
trigger(a, "click") trigger(a, "click")
t.equal(a.getAttribute(attr), "external", "external url stop behavior") t.equal(a.getAttribute(attr), "external", "external url stop behavior")
window.location.hash = "#anchor"
a.href = internalUri + "#anchor" a.href = internalUri + "#anchor"
trigger(a, "click") trigger(a, "click")
t.equal(a.getAttribute(attr), "anchor-present", "internal anchor stop behavior") t.equal(a.getAttribute(attr), "anchor", "internal anchor stop behavior")
window.location.hash = "#anchor"
a.href = internalUri + "#another-anchor" a.href = internalUri + "#another-anchor"
trigger(a, "click") trigger(a, "click")
t.notEqual(a.getAttribute(attr), "anchor", "differents anchors stop behavior") t.equal(a.getAttribute(attr), "anchor", "different anchors stop behavior")
window.location.hash = "" window.location.hash = ""
a.href = internalUri + "#" a.href = internalUri + "#"
trigger(a, "click") trigger(a, "click")
t.equal(a.getAttribute(attr), "anchor-empty", "empty anchor stop behavior") t.equal(a.getAttribute(attr), "anchor-empty", "empty anchor stop behavior")
a.href = internalUri
trigger(a, "click")
// see reload defined above
a.href = window.location.protocol + "//" + window.location.host + "/internal" a.href = window.location.protocol + "//" + window.location.host + "/internal"
trigger(a, "click") trigger(a, "click")
// see loadUrl defined above 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) { tape("test attach link preventDefaulted events", function(t) {
var callbacked = false var loadUrlCalled = false
var a = document.createElement("a") var a = document.createElement("a")
// This needs to be before the call to attachLink()
on(a, "click", function(event) {
event.preventDefault()
})
attachLink.call({ attachLink.call({
options: {}, options: {},
loadUrl: function() { loadUrl: function() {
callbacked = true loadUrlCalled = true
} }
}, a) }, a)
a.href = "#" a.href = "#"
on(a, "click", preventDefault)
trigger(a, "click") trigger(a, "click")
t.equal(callbacked, false, "events that are preventDefaulted should not fire callback") t.equal(loadUrlCalled, false, "events that are preventDefaulted should not fire callback")
t.end() t.end()
}) })

View File

@@ -1,7 +1,8 @@
var tape = require("tape") var tape = require("tape")
var parseElement = require("../../../lib/proto/parse-element") var parseElement = require("../../../lib/proto/parse-element")
var protoMock = {
var pjax = {
attachLink: function() { return true }, attachLink: function() { return true },
attachForm: function() { return true } attachForm: function() { return true }
} }
@@ -9,12 +10,12 @@ var protoMock = {
tape("test parse element prototype method", function(t) { tape("test parse element prototype method", function(t) {
t.doesNotThrow(function() { t.doesNotThrow(function() {
var a = document.createElement("a") var a = document.createElement("a")
parseElement.call(protoMock, a) parseElement.call(pjax, a)
}, "<a> element can be parsed") }, "<a> element can be parsed")
t.doesNotThrow(function() { t.doesNotThrow(function() {
var form = document.createElement("form") var form = document.createElement("form")
parseElement.call(protoMock, form) parseElement.call(pjax, form)
}, "<form> element can be parsed") }, "<form> element can be parsed")
t.end() t.end()

View File

@@ -1,18 +1,20 @@
var tape = require("tape") var tape = require("tape")
var switchesSelectors = require("../../lib/switches-selectors.js") 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 // @author darylteo
tape("test switchesSelectors", function(t) { tape("test switchesSelectors", function(t) {
// switchesSelectors relies on a higher level function callback // switchesSelectors relies on a higher level function callback
// should really be passed in instead so I'll leave it here as a TODO: // 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() var tmpEl = document.implementation.createHTMLDocument()
// a div container is used because swapping the containers // a div container is used because swapping the containers

View File

@@ -4,12 +4,10 @@ var extend = require("../../../lib/util/extend")
tape("test extend method", function(t) { tape("test extend method", function(t) {
var obj = {one: 1, two: 2} var obj = {one: 1, two: 2}
var extended = extend({}, obj, {two: "two", three: 3}) var extended = extend({}, obj, {two: "two", three: 3})
t.notEqual(obj, extended, "extended object isn't the original object") 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, 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") t.notSame(obj.two, extended.two, "extended object value overwrites value from original object")
t.end() t.end()