This commit is contained in:
David Graham 2014-10-12 18:22:45 -06:00
Родитель eb89ca18d0
Коммит 44e796a874
4 изменённых файлов: 140 добавлений и 0 удалений

38
test/.jshintrc Normal file
Просмотреть файл

@ -0,0 +1,38 @@
{
"curly": true,
"eqeqeq": true,
"es3": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": true,
"undef": true,
"unused": true,
"trailing": true,
"boss": true,
"asi": true,
"eqnull": true,
"browser": true,
"globals": {
"MockXHR": false,
"QUnit": false,
"fetch": false,
"module": false,
"test": false,
"asyncTest": false,
"testDone": false,
"expect": false,
"start": false,
"stop": false,
"ok": false,
"equal": false,
"notEqual": false,
"deepEqual": false,
"notDeepEqual": false,
"strictEqual": false,
"notStrictEqual": false,
"raises": false
}
}

67
test/mock-xhr.js Normal file
Просмотреть файл

@ -0,0 +1,67 @@
function MockXHR() {
this.method = null
this.url = null
this.data = null
this.headers = {}
this.readyState = 0
this.status = 0
this.responseText = null
}
MockXHR.responses = {}
MockXHR.prototype.open = function(method, url) {
this.method = method
this.url = url
}
MockXHR.prototype.getAllResponseHeaders = function() {
return ''
}
MockXHR.prototype.setRequestHeader = function (name, value) {
this.headers[name] = value
}
var origin = (function() {
var link = document.createElement('a')
link.href = '/'
return link.href
})()
MockXHR.prototype.send = function(data) {
this.data = data
var xhr = this
setTimeout(function() {
var path = xhr.url.replace(origin, '/')
var handle = MockXHR.responses[path]
if (handle) {
handle(xhr)
} else {
throw 'missing mocked response: ' + path
}
}, 100);
}
MockXHR.prototype.respond = function(status, body) {
this.readyState = 4
this.status = status
this.responseText = body
var event = {}
this.onload(event)
}
MockXHR.prototype.abort = function() {
// Do nothing.
}
MockXHR.prototype.slow = function() {
var event = {}
this.ontimeout(event)
}
MockXHR.prototype.error = function() {
var event = {}
this.onerror(event)
}

17
test/test.html Normal file
Просмотреть файл

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Suite</title>
<link rel="stylesheet" href="../bower_components/qunit/qunit/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="../bower_components/es6-promise/promise.js"></script>
<script src="../fetch.js"></script>
<script src="../bower_components/qunit/qunit/qunit.js"></script>
<script src="./mock-xhr.js"></script>
<script src="./test.js"></script>
</body>
</html>

18
test/test.js Normal file
Просмотреть файл

@ -0,0 +1,18 @@
MockXHR.responses = {
'/hello': function(xhr) {
xhr.respond(200, 'hi')
},
'/boom': function(xhr) {
xhr.respond(500, 'boom')
}
}
window.XMLHttpRequest = MockXHR
asyncTest('populates response body', 2, function() {
fetch('/hello').then(function(response) {
equal(response.status, 200)
equal(response.body, 'hi')
start()
})
})