initial commit (work in progress)
This commit is contained in:
parent
13fdb60f66
commit
559369016d
83 changed files with 10461 additions and 0 deletions
147
test-utils/tests/test-ajaxMock.js
Normal file
147
test-utils/tests/test-ajaxMock.js
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
"use strict"
|
||||
|
||||
var o = require("../../ospec/ospec")
|
||||
var ajaxMock = require("../../test-utils/ajaxMock")
|
||||
var parseQueryString = require("../../querystring/parse")
|
||||
|
||||
o.spec("ajaxMock", function() {
|
||||
var $window, ajax
|
||||
o.beforeEach(function() {
|
||||
$window = ajaxMock()
|
||||
})
|
||||
|
||||
o.spec("xhr", function() {
|
||||
o("works", function(done, timeout) {
|
||||
$window.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
o(request.url).equals("/item")
|
||||
return {status: 200, responseText: "test"}
|
||||
}
|
||||
})
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open("GET", "/item")
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
o(xhr.status).equals(200)
|
||||
o(xhr.responseText).equals("test")
|
||||
done()
|
||||
}
|
||||
}
|
||||
xhr.send()
|
||||
})
|
||||
o("works w/ search", function(done, timeout) {
|
||||
$window.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
o(request.query).equals("?a=b")
|
||||
return {status: 200, responseText: "test"}
|
||||
}
|
||||
})
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open("GET", "/item?a=b")
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
xhr.send()
|
||||
})
|
||||
o("works w/ body", function(done, timeout) {
|
||||
$window.$defineRoutes({
|
||||
"POST /item": function(request) {
|
||||
o(request.body).equals("a=b")
|
||||
return {status: 200, responseText: "test"}
|
||||
}
|
||||
})
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open("POST", "/item")
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
xhr.send("a=b")
|
||||
})
|
||||
o("handles routing error", function(done, timeout) {
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open("GET", "/nonexistent")
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
o(xhr.status).equals(500)
|
||||
done()
|
||||
}
|
||||
}
|
||||
xhr.send("a=b")
|
||||
})
|
||||
})
|
||||
o.spec("jsonp", function() {
|
||||
o("works", function(done) {
|
||||
$window.$defineRoutes({
|
||||
"GET /test": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify({a: 1}) + ")"}
|
||||
}
|
||||
})
|
||||
|
||||
$window["cb"] = finish
|
||||
|
||||
var script = $window.document.createElement("script")
|
||||
script.src = "/test?callback=cb"
|
||||
$window.document.documentElement.appendChild(script)
|
||||
|
||||
function finish(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
done()
|
||||
}
|
||||
})
|
||||
o("works w/ custom callback key", function(done) {
|
||||
$window.$defineRoutes({
|
||||
"GET /test": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["cb"] + "(" + JSON.stringify({a: 2}) + ")"}
|
||||
}
|
||||
})
|
||||
$window.$defineJSONPCallbackKey("cb")
|
||||
|
||||
$window["customcb"] = finish2
|
||||
|
||||
var script = $window.document.createElement("script")
|
||||
script.src = "/test?cb=customcb"
|
||||
$window.document.documentElement.appendChild(script)
|
||||
|
||||
function finish2(data) {
|
||||
o(data).deepEquals({a: 2})
|
||||
done()
|
||||
}
|
||||
})
|
||||
o("works with other querystring params", function(done, timeout) {
|
||||
$window.$defineRoutes({
|
||||
"GET /test": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify({a: 3}) + ")"}
|
||||
}
|
||||
})
|
||||
|
||||
$window["cbwithinparams"] = finish
|
||||
|
||||
var script = $window.document.createElement("script")
|
||||
script.src = "/test?a=b&callback=cbwithinparams&c=d"
|
||||
$window.document.documentElement.appendChild(script)
|
||||
|
||||
function finish(data) {
|
||||
o(data).deepEquals({a: 3})
|
||||
done()
|
||||
}
|
||||
})
|
||||
o("handles error", function(done) {
|
||||
var script = $window.document.createElement("script")
|
||||
script.onerror = finish
|
||||
script.src = "/test?cb=nonexistent"
|
||||
$window.document.documentElement.appendChild(script)
|
||||
|
||||
function finish(e) {
|
||||
o(e.type).equals("error")
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue