Skip to content

Commit

Permalink
test(admin): refactored and fixed admin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Oct 6, 2013
1 parent 2b59a63 commit 5a94c88
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 317 deletions.
73 changes: 7 additions & 66 deletions client/src/app/admin/users/admin-users-edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
angular.module('admin-users-edit',['services.crud', 'services.i18nNotifications', 'resources.users'])
angular.module('admin-users-edit',[
'services.crud',
'services.i18nNotifications',
'admin-users-edit-uniqueEmail',
'admin-users-edit-validateEquals'
])

.controller('UsersEditCtrl', ['$scope', '$location', 'i18nNotifications', 'user', function ($scope, $location, i18nNotifications, user) {

Expand All @@ -19,68 +24,4 @@ angular.module('admin-users-edit',['services.crud', 'services.i18nNotifications'
$location.path('/admin/users');
};

}])

/**
* A validation directive to ensure that the model contains a unique email address
* @param Users service to provide access to the server's user database
*/
.directive('uniqueEmail', ["Users", function (Users) {
return {
require:'ngModel',
restrict:'A',
link:function (scope, el, attrs, ctrl) {

//TODO: We need to check that the value is different to the original

//using push() here to run it as the last parser, after we are sure that other validators were run
ctrl.$parsers.push(function (viewValue) {

if (viewValue) {
Users.query({email:viewValue}, function (users) {
if (users.length === 0) {
ctrl.$setValidity('uniqueEmail', true);
} else {
ctrl.$setValidity('uniqueEmail', false);
}
});
return viewValue;
}
});
}
};
}])

/**
* A validation directive to ensure that this model has the same value as some other
*/
.directive('validateEquals', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {

function validateEqual(myValue, otherValue) {
if (myValue === otherValue) {
ctrl.$setValidity('equal', true);
return myValue;
} else {
ctrl.$setValidity('equal', false);
return undefined;
}
}

scope.$watch(attrs.validateEquals, function(otherModelValue) {
ctrl.$setValidity('equal', ctrl.$viewValue === otherModelValue);
});

ctrl.$parsers.push(function(viewValue) {
return validateEqual(viewValue, scope.$eval(attrs.validateEquals));
});

ctrl.$formatters.push(function(modelValue) {
return validateEqual(modelValue, scope.$eval(attrs.validateEquals));
});
}
};
});
}]);
25 changes: 25 additions & 0 deletions client/src/app/admin/users/admin-users-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
angular.module('admin-users-list', [
'servics.crud',
'services.i18nNotifications'
])

.controller('UsersListCtrl', ['$scope', 'crudListMethods', 'users', 'i18nNotifications', function ($scope, crudListMethods, users, i18nNotifications) {
$scope.users = users;

angular.extend($scope, crudListMethods('/admin/users'));

$scope.remove = function(user, $index, $event) {
// Don't let the click bubble up to the ng-click on the enclosing div, which will try to trigger
// an edit of this item.
$event.stopPropagation();

// Remove this user
user.$remove(function() {
// It is gone from the DB so we can remove it from the local list too
$scope.users.splice($index,1);
i18nNotifications.pushForCurrentRoute('crud.user.remove.success', 'success', {id : user.$id()});
}, function() {
i18nNotifications.pushForCurrentRoute('crud.user.remove.error', 'error', {id : user.$id()});
});
};
}]);
30 changes: 8 additions & 22 deletions client/src/app/admin/users/admin-users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
angular.module('admin-users', ['admin-users-edit', 'services.crud', 'security.authorization', 'services.i18nNotifications', 'directives.gravatar'])
angular.module('admin-users', [
'admin-users-list',
'admin-users-edit',

'services.crud',
'security.authorization',
'directives.gravatar'
])

.config(['crudRouteProvider', 'securityAuthorizationProvider', function (crudRouteProvider, securityAuthorizationProvider) {

Expand All @@ -17,25 +24,4 @@ angular.module('admin-users', ['admin-users-edit', 'services.crud', 'security.au
}],
currentUser: securityAuthorizationProvider.requireAdminUser
});
}])

.controller('UsersListCtrl', ['$scope', 'crudListMethods', 'users', 'i18nNotifications', function ($scope, crudListMethods, users, i18nNotifications) {
$scope.users = users;

angular.extend($scope, crudListMethods('/admin/users'));

$scope.remove = function(user, $index, $event) {
// Don't let the click bubble up to the ng-click on the enclosing div, which will try to trigger
// an edit of this item.
$event.stopPropagation();

// Remove this user
user.$remove(function() {
// It is gone from the DB so we can remove it from the local list too
$scope.users.splice($index,1);
i18nNotifications.pushForCurrentRoute('crud.user.remove.success', 'success', {id : user.$id()});
}, function() {
i18nNotifications.pushForCurrentRoute('crud.user.remove.error', 'error', {id : user.$id()});
});
};
}]);
31 changes: 31 additions & 0 deletions client/src/app/admin/users/uniqueEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
angular.module('admin-users-edit-uniqueEmail', ['resources.users'])

/**
* A validation directive to ensure that the model contains a unique email address
* @param Users service to provide access to the server's user database
*/
.directive('uniqueEmail', ["Users", function (Users) {
return {
require:'ngModel',
restrict:'A',
link:function (scope, el, attrs, ctrl) {

//TODO: We need to check that the value is different to the original

//using push() here to run it as the last parser, after we are sure that other validators were run
ctrl.$parsers.push(function (viewValue) {

if (viewValue) {
Users.query({email:viewValue}, function (users) {
if (users.length === 0) {
ctrl.$setValidity('uniqueEmail', true);
} else {
ctrl.$setValidity('uniqueEmail', false);
}
});
return viewValue;
}
});
}
};
}]);
35 changes: 35 additions & 0 deletions client/src/app/admin/users/validateEquals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
angular.module('admin-users-edit-validateEquals', [])

/**
* A validation directive to ensure that this model has the same value as some other
*/
.directive('validateEquals', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {

function validateEqual(myValue, otherValue) {
if (myValue === otherValue) {
ctrl.$setValidity('equal', true);
return myValue;
} else {
ctrl.$setValidity('equal', false);
return undefined;
}
}

scope.$watch(attrs.validateEquals, function(otherModelValue) {
ctrl.$setValidity('equal', ctrl.$viewValue === otherModelValue);
});

ctrl.$parsers.push(function(viewValue) {
return validateEqual(viewValue, scope.$eval(attrs.validateEquals));
});

ctrl.$formatters.push(function(modelValue) {
return validateEqual(modelValue, scope.$eval(attrs.validateEquals));
});
}
};
});
Loading

0 comments on commit 5a94c88

Please sign in to comment.