Skip to content

Instantly share code, notes, and snippets.

@junderhill
Created April 20, 2015 08:59
Show Gist options
  • Save junderhill/e181ce866ab1ebb1f805 to your computer and use it in GitHub Desktop.
Save junderhill/e181ce866ab1ebb1f805 to your computer and use it in GitHub Desktop.
Activity controller injection
angular.module('dailysummary')
.controller('activityController', activityController);
activityController.$inject = ['roleService', 'activityService', '$scope'];
function activityController(roleService, activityService, $scope) {
var vm = this;
vm.selectedDate = new Date();
var currentRole = roleService.getCurrentRole();
var currentTeam = roleService.getCurrentTeam();
activityService.getActivity(vm.selectedDate, vm.selectedDate, roleService.getCurrentUser(), currentRole['AssignmentID'], currentRole['UserType'], currentTeam['ID'])
.then(function(data) {
console.log(data);
}, function() {
console.log('Error getting activity');
});
};
'use strict';
describe('Activity controller', function(){
var activityController;
var $httpBackend, $scope;
var mockRoleService;
beforeEach(module('dailysummary'));
beforeEach(inject(function($rootScope, _$httpBackend_, roleService){
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
var stubTeamResponse = { "ClientID": 1, "CostCentre": "-1", "CountryID": 1, "Description": "Demonstration", "EndDate": null, "EtmsDescription": "Care 2", "EtmsID": 2, "ID": 3, "IsActive": false, "IsStockModuleEnabled": true, "NumberOfWeeksCare1": 20, "PharmaDatabase": "OneKey", "Stylesheet": "ai2f01.css", "TeamTypeID": 7, "UserID": "100201", "WorkingDays": 7, "DatabaseID": 0, "CompanyID": 1, "ExpensesCurrencyID": 3228 };
$httpBackend.when('GET', /^(http:\/\/)[a-z.:0-9]*(\/api\/User\/GetTeamForAssignment\?assignmentId=\d+&assignmentType=\d)$/).respond(200, stubTeamResponse);
mockRoleService = roleService;
mockRoleService.setCurrentRole({"AssignmentID":21,"EndDate":"2049-12-31T00:00:00","StartDate":"2000-01-01T00:00:00","UserType":1,"AccessLevel":"00000000-0000-0000-0000-000000000000","Description":"Demonstration Territory 1","TeamID":null});
mockRoleService.setCurrentUser('100607');
}));
describe("on initialisation", function () {
beforeEach(inject(function($controller) {
console.log(activityController);
activityController = $controller('activityController', {
'$scope': $scope,
'roleService' : mockRoleService
});
console.log("TEST");
}));
it("selected date is not null", function(){
expect(activityController.selectedDate).not.toBeNull();
});
it("sets the selected date to current date", function(){
var currentDate = new Date();
expect(activityController.selectedDate).toEqual(currentDate);
});
it("calls the web service", function(){
$httpBackend.expectGET(/^(http:\/\/)[a-z.:0-9]*(\/api\/Activity\/GetActivitiesForDateRange\?from=.{20}&to=.{20}&userId=\d{6}&roleId=\d{1,4}&roleTypeId=\d{1}&teamId=\d{1,4})$/);
$httpBackend.flush();
});
});
});
angular.module('dailysummary').service('activityService', activityService);
function activityService($http, $q){
}
angular.module('dailysummary').service('roleService', roleService);
function roleService($http, $q) {
var currentRole = null;
var currentTeam = null;
var currentUser = null;
this.getRolesForUser = function(userId){
var deferred = $q.defer();
$http.get("http://localhost:14938/api/User/GetRoles?userId=" + userId)
.success(function(data){
deferred.resolve(data);
});
return deferred.promise;
}
this.setCurrentRole = function(role){
currentRole = role;
$http.get("http://localhost:14938/api/User/GetTeamForAssignment?assignmentId=" + role["AssignmentID"] + "&assignmentType=" + role["UserType"])
.success(function (data) {
currentTeam = data;
});
}
this.getCurrentRole = function(){
return currentRole;
}
this.setCurrentTeam = function(team){
currentTeam = team;
}
this.getCurrentTeam = function(){
return currentTeam;
}
this.setCurrentUser = function(user){
currentUser = user;
}
this.getCurrentUser = function(){
return currentUser;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment