-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |