-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from Trustroots/add-remove-contact-tests
Add contact remove controller client tests
- Loading branch information
Showing
4 changed files
with
167 additions
and
62 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
modules/contacts/client/controllers/remove-contact.client.controller.js
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
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'); | ||
} | ||
|
||
} | ||
|
||
})(); |
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
File renamed without changes.
95 changes: 95 additions & 0 deletions
95
modules/contacts/tests/client/remove-contact.client.controller.tests.js
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
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 }); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
}()); |