lundi 4 juillet 2016

Unit testing jQuery references and ajax calls in ES6 classes

I'm refactoring a codebase to use ES6 classes. I have limited knowledge in jQuery/ajax and I am struggling to set up a test environment that can be tested outside of being included as a script in an HTML page and also works with ES6 class syntax.

export class VimRegistrationAPI {
getProducts(header, sendparty) {

    $.ajax({
        type: 'GET',
        url: 'api/product',
        beforeSend: function (xhr) { xhr.setRequestHeader('Environment', header); }
    }).done(function (data, textStatus, xhrObject) {
        sendparty(null, data);
    }).fail(function (xhrObject, textStatus, errorThrown) {
        sendparty(new Error("Bad"), errorThrown);
    });
}

I've attempted to unit test this using Mocha.js, but get an error saying $ is undefined. My attempt to solve this was to create a var $ and requiring jQuery, but returns an error declaring that $.ajax is not a function. I am thinking that this is because jQuery has no document to reference. How can I create an environment that allows for unit testing with these ajax calls inside of the class?

Aucun commentaire:

Enregistrer un commentaire