Skip to content

Commit

Permalink
Merge pull request #375 from Trustroots/add-remove-contact-tests
Browse files Browse the repository at this point in the history
Add contact remove controller client tests
  • Loading branch information
simison authored Jul 25, 2016
2 parents 21cb6ff + 09eced9 commit e4dd93f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(function() {
'use strict';

angular
.module('contacts')
.controller('ContactRemoveController', ContactRemoveController);

/* @ngInject */
function ContactRemoveController($scope, $rootScope, $uibModalInstance, $timeout, messageCenterService, Contact, Authentication) {

var contactToRemove = $scope.contactToRemove;

var vm = this;
vm.isLoading = false;
vm.contact = contactToRemove;
vm.removeContact = removeContact;
vm.cancelContactRemoval = cancelContactRemoval;

// Different confirm button label and modal title depending on situation

// User is cancelling a request
if(angular.isDefined(contactToRemove.confirmed) && contactToRemove.confirmed === false && Authentication.user._id === contactToRemove.users[1]._id) {
vm.labelConfirm = 'Yes, revoke request';
vm.labelTitle = 'Revoke contact request?';
vm.labelTime = 'Requested';
}
// Decline received request
else if(angular.isDefined(contactToRemove.confirmed) && contactToRemove.confirmed === false) {
vm.labelConfirm = 'Yes, decline request';
vm.labelTitle = 'Decline contact request?';
vm.labelTime = 'Requested';
}
// Removing confirmed contact
else {
vm.labelConfirm = 'Yes, remove contact';
vm.labelTitle = 'Remove contact?';
vm.labelTime = 'Connected since';
}

function removeContact() {
vm.isLoading = true;

// contact comes from the parent link()
Contact.delete({contactId: contactToRemove._id},
// Success
function() {

// Let other controllers know that this was removed, so that they can react
$rootScope.$broadcast('contactRemoved', contactToRemove);

$uibModalInstance.dismiss('cancel');
},
// Error
function() {
vm.isLoading = false;
$uibModalInstance.dismiss('cancel');
messageCenterService.add('danger', 'Oops! Something went wrong. Try again later.', { timeout: 7000 });
}
);
}

// Close modal
function cancelContactRemoval() {
$uibModalInstance.dismiss('cancel');
}

}

})();
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,20 @@
.directive('trContactRemove', trContactRemoveDirective);

/* @ngInject */
function trContactRemoveDirective($uibModal, Authentication) {
function trContactRemoveDirective($uibModal) {
return {
restrict: 'A',
scope: {
contactToRemove: '=trContactRemove'
},
link: function(scope, element, attrs) {

var contactToRemove = scope.contactToRemove;

function openModal() {

$uibModal.open({
templateUrl: '/modules/contacts/views/remove-contact.client.modal.html',
controllerAs: 'removeContactModal',
/* @ngInject */
controller: function($rootScope, $uibModalInstance, $timeout, messageCenterService, Contact) {

var vm = this;
vm.isLoading = false;
vm.contact = contactToRemove;
vm.removeContact = removeContact;
vm.cancelContactRemoval = cancelContactRemoval;

// Different confirm button label and modal title depending on situation

// User is cancelling a request
if(angular.isDefined(contactToRemove.confirmed) && contactToRemove.confirmed === false && Authentication.user._id === contactToRemove.users[1]._id) {
vm.labelConfirm = 'Yes, revoke request';
vm.labelTitle = 'Revoke contact request?';
vm.labelTime = 'Requested';
}
// Decline received request
else if(angular.isDefined(contactToRemove.confirmed) && contactToRemove.confirmed === false) {
vm.labelConfirm = 'Yes, decline request';
vm.labelTitle = 'Decline contact request?';
vm.labelTime = 'Requested';
}
// Removing confirmed contact
else {
vm.labelConfirm = 'Yes, remove contact';
vm.labelTitle = 'Remove contact?';
vm.labelTime = 'Connected since';
}

function removeContact() {
vm.isLoading = true;

// contact comes from the parent link()
Contact.delete({contactId: contactToRemove._id},
// Success
function() {

// Let other controllers know that this was removed, so that they can react
$rootScope.$broadcast('contactRemoved', contactToRemove);

$uibModalInstance.dismiss('cancel');
},
// Error
function() {
vm.isLoading = false;
$uibModalInstance.dismiss('cancel');
messageCenterService.add('danger', 'Oops! Something went wrong. Try again later.', { timeout: 7000 });
}
);
}

// Close modal
function cancelContactRemoval() {
$uibModalInstance.dismiss('cancel');
}

}
controller: 'ContactRemoveController',
scope: scope
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
(function() {
'use strict';

describe('ContactRemoveController', function() {
// Initialize global variables
var $templateCache,
$httpBackend,
Authentication,
$rootScope,
$scope,
$uibModalInstance,
messageCenterService,
ContactRemoveController;

var user1 = {
_id: 'user1',
displayName: 'User One'
};

var contactToRemove = {
_id: 'contact1'
};

// Load the main application module
beforeEach(module(AppConfig.appModuleName));

beforeEach(inject(function($templateCache, _$httpBackend_, _Authentication_, _$rootScope_, _messageCenterService_){
$httpBackend = _$httpBackend_;
Authentication = _Authentication_;

$rootScope = _$rootScope_;
spyOn($rootScope, '$broadcast').and.callThrough();

messageCenterService = _messageCenterService_;
spyOn(messageCenterService, 'add').and.callThrough();

$uibModalInstance = jasmine.createSpyObj('$uibModalInstance', ['close', 'dismiss']);

$scope = $rootScope.$new();
$scope.contactToRemove = contactToRemove;

$templateCache.put('/modules/pages/views/home.client.view.html', '');
}));

afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

describe('logged in', function(){

beforeEach(function(done){
inject(function($controller){
Authentication.user = user1;
ContactRemoveController = $controller('ContactRemoveController', {
$scope: $scope,
$uibModalInstance: $uibModalInstance,
messageCenterService: messageCenterService
});
done();
});
});

it('sets the contact', function() {
expect(ContactRemoveController.contact).toBe(contactToRemove);
});

it('can remove the contact', function() {
$httpBackend.expect('DELETE', '/api/contact/' + contactToRemove._id).respond(200);
expect(ContactRemoveController.removeContact).toBeDefined();
ContactRemoveController.removeContact();
$httpBackend.flush();
expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
expect($rootScope.$broadcast).toHaveBeenCalledWith('contactRemoved', contactToRemove);
});

it('can be cancelled', function() {
expect(ContactRemoveController.cancelContactRemoval).toBeDefined();
ContactRemoveController.cancelContactRemoval();
expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
expect($rootScope.$broadcast).not.toHaveBeenCalled();
});

it('handles backend errors gracefully', function() {
$httpBackend.expect('DELETE', '/api/contact/' + contactToRemove._id).respond(400);
ContactRemoveController.removeContact();
$httpBackend.flush();
expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
expect(messageCenterService.add).toHaveBeenCalledWith('danger', 'Oops! Something went wrong. Try again later.', { timeout: 7000 });
});

});

});
}());

0 comments on commit e4dd93f

Please sign in to comment.