Created
August 29, 2013 08:35
-
-
Save 0xjjpa/6375634 to your computer and use it in GitHub Desktop.
A simple AngularJS service with its unit test, taking in consideration than an API object was handled to the service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
angular.module('usersResources', [ | |
'apiConstants', | |
'apiModule' | |
]) | |
.service('usersResources.users', [ | |
'$q', | |
'api', | |
function users($q, api) { | |
var _self = this; | |
this.getUsersURI = function() { | |
return '/user'; | |
} | |
this.getUsers = function() { | |
var deferred = $q.defer(); | |
api.get(_self.getUsersURI()).then(function(usersResponse){ | |
deferred.resolve(usersResponse); | |
}, function() { | |
deferred.reject.apply(null, Array.prototype.slice.call(arguments)); | |
}); | |
return deferred.promise; | |
} | |
}]); | |
//////////////////////// Unit Test ///////////////////////// | |
"use strict"; | |
ddescribe('[Service] Users Resources', function () { | |
var componentName = 'usersResources.users'; | |
var succesfulApiResponse = 'Successful Response From API'; | |
var failedApiResponse = 'Failed Response From API'; | |
beforeEach(module('usersResources')); | |
describe(' :: User Resources Actions', function() { | |
var httpBackend, users, apiUrl, api, q, rootScope; | |
var mockedApiDeferred, mockedDeferred; | |
var mockApi = function() { | |
var deferred = q.defer(); | |
spyOn(api, 'get').andReturn(deferred.promise); | |
return deferred; | |
} | |
var mockQ = function() { | |
var deferred = jasmine.createSpyObj('deferred', ['resolve', 'reject']); | |
spyOn(q, 'defer').andReturn(deferred); | |
return deferred; | |
} | |
beforeEach(inject(function ($injector, $q, $rootScope) { | |
users = $injector.get(componentName); | |
apiUrl = $injector.get('apiBaseUrl'); | |
api = $injector.get('api'); | |
q = $q; | |
rootScope = $rootScope; | |
mockedApiDeferred = mockApi(); | |
mockedDeferred = mockQ(); | |
})); | |
it('should perfom an API GET call to the users REST Users Endpoint so it can resolve the server\'s response on success', function() { | |
users.getUsers(); | |
mockedApiDeferred.resolve(succesfulApiResponse); | |
rootScope.$digest(); | |
expect(mockedDeferred.resolve).toHaveBeenCalledWith(succesfulApiResponse); | |
expect(api.get).toHaveBeenCalledWith(users.getUsersURI()); | |
}); | |
it('should perform an API GET call to the users REST Users Endpoint so it can reject the server\'s response on failure', function() { | |
users.getUsers(); | |
mockedApiDeferred.reject(failedApiResponse); | |
rootScope.$digest(); | |
expect(mockedDeferred.reject).toHaveBeenCalledWith(failedApiResponse); | |
expect(api.get).toHaveBeenCalledWith(users.getUsersURI()); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment