Spork
Spork

Reputation: 1651

Can the jQuery ajax success function hold 'undefined' as data?

In the process of adding unit test coverage to existing javascript code we've encountered a case which we cannot seem to cover well, namely the code that checks for the callback's data to be undefined:

$.ajax({
    dataType: "json",
    url: someUrl,
    success: function(data) {
     if (typeof data === "undefined") {
        // *** How do I cover this line with jasmine? ***
     }
    },
    error: function () {
      // handle errors
    }
});

One of the possible answers would be that we cannot test this line, as data passed to the success function by jQuery.ajax cannot be undefined. Is that the case?

The reference mentions the data type of the passed data to be Anything, but I'm not sure if this does or does not mean data could be undefined, and if we could test the branch.

success

Type: Function( Anything data, String textStatus, jqXHR jqXHR )

Upvotes: 0

Views: 222

Answers (0)

Related Questions