Skip to content

Commit

Permalink
Modify $.jsonP() test to use server
Browse files Browse the repository at this point in the history
  • Loading branch information
townxelliot committed Aug 29, 2013
1 parent 5cb6032 commit e5ec2f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
1 change: 0 additions & 1 deletion test/fixtures/jsonp.loaded.js

This file was deleted.

61 changes: 46 additions & 15 deletions test/jsonp.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
var path = require("path");
require("./chai.helper");
var domHelper = require("./dom.helper");

// this loads fixtures/jsonp.loaded.js from the local filesystem
var basepath = path.join(__dirname, "fixtures");
var JSONP_LOADED_URI = "file://" + path.join(basepath, "jsonp.loaded.js");
var HttpFake = require("./http-fake.helper");

describe("jsonp", function () {
beforeEach(function () {
domHelper(
"<div id=\"foo\">" +
"</div>"
);
var server = HttpFake.createServer();

// we need to refer to this from the tests, but we allow
// the server to randomly get a port before setting it
var host;

before(function (done) {
domHelper();

server.start(function () {
host = "localhost:" + server.port;
done.apply(null, arguments);
});
});

afterEach(function () {
server.clearFakes();
});

after(function (done) {
server.stop(done);
});

it("should load an external JSONP script with callback in the URI", function (done) {
var expected = { JSONP_LOADED: true };

server.registerFake(
// response config
{
data: function (req) {
var callback = req.query["callback"];
return callback + "(" + JSON.stringify(expected) + ");";
}
},

// request matcher
{
path: '/1'
}
);

// make chai's expect function available in the execution context
// for the script
window.expect = expect;

// this is the function referenced in the jsonp.loaded.js script
// this is the function referenced in the querystring sent
// to the server
window.parseResponse = function (data) {
var expected = { JSONP_LOADED: true };
window.expect(data).to.eql(expected);
done();
};

$.jsonP({
url: JSONP_LOADED_URI + "?callback=?",
// NB querystring callback set to the function defined above
url: "http://" + host + "/1?callback=window.parseResponse",

error: function () {
done(new Error("could not load jsonp.loaded.js script"));
error: function (xhr) {
console.log(xhr);
done(new Error("could not load jsonp script"));
}
});
});
Expand Down

0 comments on commit e5ec2f8

Please sign in to comment.