diff --git a/test/XMLHttpRequest.js b/test/XMLHttpRequest.js index 1a8454f2784c9..21ee57468e5e9 100644 --- a/test/XMLHttpRequest.js +++ b/test/XMLHttpRequest.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; diff --git a/test/xhr/xhr-test.js b/test/xhr/xhr-test.js index cbb7c47e503ec..dd2976216e1cd 100644 --- a/test/xhr/xhr-test.js +++ b/test/xhr/xhr-test.js @@ -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); }); },