In my project, I have an existing API library that is not Angular-based. This library contains a method called .request
which returns promises using jQuery.Deferred. To integrate this with Angular, I created a simple service that wraps the .request
method to convert its output into Angular $q promises. Here's how it looks:
var module = angular.module('example.api', []);
module.factory('api', function(
$q,
$window
) {
function wrappedRequest() {
var result = $window.API.request.apply($window.API, arguments);
return $q.when(result);
};
return {
request: wrappedRequest
};
});
I want to write a test to ensure that this service works correctly. The test should involve creating a mock $window
object with an API
property that has a request
method returning jQuery.Deferred promises. The goal is to confirm that the returned objects are indeed Angular $q promises.
Is there a way to determine whether an object is an Angular $q promise?
While it's important for the given scenario to distinguish between jQuery.Deferred and Angular $q promises, ideally we should be able to identify Angular $q promises in any context.