samedi 25 juin 2016

Sinon Fakeserver Testing Different Component/File - No Requests?


I'm testing a React component using Mocha/Chai/Sinon and calling an AJAX request in a different file, using something like renderedComponent.serverRequest(callback). I'm passing in a callback similar to this Sinon FakeServer tutorial. However, I'm getting a strange Syntax error, which causes the callback to not be called, and thus server.requests is empty.

I'm aware that I asked a similar question here - however, this issue that's different here is that I'm calling the AJAX request in a different file, rather than the same file, and now the solution that worked before is no longer working. I'm not sure why. In general, my question is: Is there something about including a function in a different file that causes Sinon to fail to be loaded in my JSDOM instance?

For reference, here is my FakeServer code:

var server;

before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });

it("calls callback with deserialized data", function () {
    var callback = sinon.spy();
    renderedComponent.serverRequest(callback);

    // This is part of the FakeXMLHttpRequest API
    server.requests[0].respond(
        200,
        { "Content-Type": "application/json" },
        JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
    );

    assert(callback.calledOnce);
});

And here is the serverRequest function in renderedComponent:

function getTodos(listId, callback) {
    $.ajax({
         url: "/todo/" + listId + "/items",
         success: function (data) {
             // Node-style CPS: callback(err, data)
             callback(null, data);
         }
    });
}

Adding an error parameter to the AJAX request reveals the Syntax error from the jsdom node module trying to do urlObj = new URL(uri, documentBaseURLSerialized(this._ownerDocument));


Aucun commentaire:

Enregistrer un commentaire