-
-
Save jabhishek/194e66c3235fcebdc873 to your computer and use it in GitHub Desktop.
This file contains 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
import StorageFactoryModule from 'app/common/services/StorageFactory'; | |
class CurrentUser { | |
constructor(StorageFactory) { | |
this.storageKey = 'appUser'; | |
this.StorageFactory = StorageFactory; | |
this.profile = initializeUser.call(this); | |
function initializeUser() { | |
var userFromStorage = StorageFactory.get(this.storageKey); | |
if (userFromStorage != null) { | |
return userFromStorage; | |
} else { | |
// return default user | |
} | |
} | |
} | |
// more methods, that are removed for brevity | |
} | |
CurrentUser.$inject = ['StorageFactory']; | |
export default angular.module('CurrentUserModule', [StorageFactoryModule.name]) | |
.service('CurrentUser', CurrentUser); |
This file contains 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
import angular from 'angular'; | |
import 'angular-mocks'; | |
import './CurrentUser'; | |
describe('CurrentUser', function () { | |
"use strict"; | |
var user = {userid: 'abc', token: 'token'}; | |
var storageFactoryMock = { | |
get: function (key) { | |
console.log('mocked get'); | |
return user; | |
}, | |
put: function (key, newUser) { | |
console.log('mocked put'); | |
user = newUser; | |
}, | |
remove: function (key) { | |
console.log('mocked remove'); | |
user = undefined; | |
} | |
}; | |
beforeEach(function () { | |
angular.module('StorageFactoryModule') | |
.value('StorageFactory', storageFactoryMock); | |
angular.mock.module('CurrentUserModule'); | |
}); | |
it('should Initialize User from local storage if already exists there', inject(function (CurrentUser) { | |
expect(CurrentUser.profile).toEqual(user); | |
})) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment