Permalink
Browse files

Add test for beforesend listener.

  • Loading branch information...
1 parent 9f59f94 commit 8e8fcbe6eb4bc188ca01aa8446141f60a07f6a0f @mbostock mbostock committed Aug 10, 2013
Showing with 36 additions and 6 deletions.
  1. +5 −0 test/XMLHttpRequest.js
  2. +31 −6 test/xhr/xhr-test.js
@@ -8,9 +8,12 @@ global.XMLHttpRequest = function XMLHttpRequest() {
// TODO handle file system errors?
+self.readyState = 0;
+
self.open = function(m, u, a) {
info.url = u;
info.async = a;
+ self.readyState = 1;
self.send = a ? read : readSync;
};
@@ -19,6 +22,7 @@ global.XMLHttpRequest = function XMLHttpRequest() {
};
function read() {
+ self.readyState = 2;
fs.readFile(info.url, "binary", function(e, d) {
if (e) {
self.status = 404; // assumed
@@ -35,6 +39,7 @@ global.XMLHttpRequest = function XMLHttpRequest() {
}
function readSync() {
+ self.readyState = 2;
try {
var d = fs.readFileSync(info.url, "binary");
self.status = 200;
View
@@ -9,8 +9,8 @@ suite.addBatch({
topic: load("xhr/xhr").expression("d3.xhr").document(),
"on a sample text file": {
- topic: function(xhr) {
- xhr("test/data/sample.txt", this.callback);
+ topic: function(d3_xhr) {
+ d3_xhr("test/data/sample.txt", this.callback);
},
"makes an asynchronous HTTP request": function(req) {
assert.equal(req._info.url, "test/data/sample.txt");
@@ -29,18 +29,43 @@ suite.addBatch({
},
"when a custom mime type is specified": {
- topic: function(xhr) {
- xhr("test/data/sample.txt", "text/plain", this.callback);
+ topic: function(d3_xhr) {
+ d3_xhr("test/data/sample.txt", "text/plain", this.callback);
},
"observes the optional mime type": function(req) {
assert.equal(req._info.mimeType, "text/plain");
}
},
+ "when a beforesend listener is specified": {
+ topic: function(d3_xhr) {
+ var callback = this.callback;
+ var xhr = d3_xhr("test/data/sample.txt", "text/plain").on("beforesend", function(request) {
+ callback(null, {
+ that: this,
+ xhr: xhr,
+ readyState: request.readyState,
+ request: request
+ });
+ });
+ xhr.get();
+ },
+ "invokes the beforesend listener with the xhr object as the context": function(result) {
+ assert.equal(result.that, result.xhr);
+ assert.ok(result.xhr.get);
+ },
+ "invokes the beforesend listener with the underlying XMLHttpRequest as an argument": function(result) {
+ assert.instanceOf(result.request, XMLHttpRequest);
+ },
+ "invokes the beforesend listener after open and before send": function(result) {
+ assert.equal(result.readyState, 1);
+ }
+ },
+
"on a file that does not exist": {
- topic: function(xhr) {
+ topic: function(d3_xhr) {
var callback = this.callback;
- xhr("//does/not/exist.txt", function(error, req) {
+ d3_xhr("//does/not/exist.txt", function(error, req) {
callback(null, req);
});
},

0 comments on commit 8e8fcbe

Please sign in to comment.