Skip to content

Commit

Permalink
Start tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Oct 13, 2014
1 parent eb89ca1 commit 44e796a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
@@ -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 changes: 67 additions & 0 deletions test/mock-xhr.js
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 44e796a

Please sign in to comment.