I'm starting to do unit testing for my Angular application, and I had some questions about how to create a test folder structure. I mainly used the yoman Angular generator, so it comes with pre-configured Jasmine and Karma.
Here is the scenario of what I'm trying to check ...
I have a "PageHeaderDirective" that displays the username and email address (for example, a welcome message), as well as a logout link. The code for the page header directive is inconsequential, but I need to hit the "/ user" endpoint from the backend to get the user's details. Here is the code for UserService that is entered in PageHeaderDirective:
(function () {
'use strict';
angular.module('Common').service('UserService', UserService);
UserService.$inject = ['User', 'Restangular'];
function UserService(User, Restangular) {
var userPromise;
return {
getUser: getUser
};
function getUser() {
if(!userPromise) {
userPromise = Restangular.one('user').get().then(function(data) {
return User.factory(data);
});
}
return userPromise;
}
}
})();
PageHeaderDirective:
describe('Pageheader Tests', function() {
'use strict';
var scope;
var element;
beforeEach(module('templates'));
beforeEach(module('Common'));
beforeEach(inject(function(_$rootScope_, $compile) {
scope = _$rootScope_.$new();
scope.message = 'Test message';
element = '<ft-page-header message="message" page="home"></ft-page-header>';
element = $compile(element)(scope);
scope.$digest();
}));
it('should render a page header with the logo and username', function() {
expect(element.find('.logo-text').length).toBe(1);
var isolateScope = element.isolateScope();
expect(isolateScope.name).toBe('test');
});
});
, , , , " : RestangularProvider < - Restangular < - UserService < - pageHeaderDirective", .
, - beforeEach(function(){ module(function($provide) { $provide.service('UserService', function() { ... }})}); , , / UserService. UserService.mock.js? , "UserService.mock.js" ?
-, Restangular PageHeaderDirective (Restangular.one('logout').get().then...). ( API)?
, , ($ document, $localStorage, $window), ? , ?
!