Skip to content

Instantly share code, notes, and snippets.

@aj-dev
Last active October 5, 2017 21:21
Show Gist options
  • Save aj-dev/8942cdcd7c92ff9e2b80d384939023af to your computer and use it in GitHub Desktop.
Save aj-dev/8942cdcd7c92ff9e2b80d384939023af to your computer and use it in GitHub Desktop.
profileService unit tests
describe('profileService', function () {
'use strict';
var $q, $httpBackend, profileService;
var sessionServiceMock = jasmine.createSpyObj('sessionService', [
'authenticate',
'getUserProfile'
]);
function mockServices($provide) {
$provide.factory('sessionService', function () {
return sessionServiceMock;
});
}
function services($injector) {
$q = $injector.get('$q');
$httpBackend = $injector.get('$httpBackend');
profileService = $injector.get('profileService');
}
function setUp() {
$httpBackend.whenGET(/.+(\.html|json)/).respond(200, {});
$httpBackend.whenGET('/api/profile').respond(200, {});
}
beforeEach(function () {
module('evbox', mockServices);
inject(services);
setUp();
});
describe('service methods', function () {
var authResponse = {
profile: {
name: 'John Gray'
}
};
describe('getProfile', function () {
it('returns user profile', function () {
sessionServiceMock.authenticate.and.returnValue($q.resolve(authResponse));
sessionServiceMock.getUserProfile.and.callFake(function (auth) {
return auth.profile;
});
profileService.getProfile()
.then(function onFulfilled(profile) {
expect(profile).toEqual(authResponse.profile);
});
$httpBackend.flush();
expect(sessionServiceMock.authenticate).toHaveBeenCalled();
});
});
describe('updateProfile', function () {
it('updates user profile', function () {
var profilePayload = {
name: 'Alex Stone'
};
$httpBackend.expectPATCH('/api/users/me', profilePayload).respond(200, profilePayload);
profileService.updateProfile(profilePayload)
.then(function onFulfilled(profile) {
expect(profile).toEqual(profilePayload);
});
$httpBackend.flush();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment