|
| 1 | +/* jshint -W117, -W030 */ |
| 2 | +describe('External Link Deletion Controller', function() { |
| 3 | + var scope; |
| 4 | + var element; |
| 5 | + var toasterSvc, extLinkSvc, ngDialogSvc; |
| 6 | + var mockLinkedAccounts = [ |
| 7 | + { |
| 8 | + provider: 'github', |
| 9 | + data: { |
| 10 | + handle: "github-handle", |
| 11 | + followers: 1, |
| 12 | + publicRepos: 1 |
| 13 | + } |
| 14 | + }, |
| 15 | + { provider: 'stackoverflow', |
| 16 | + data: { |
| 17 | + handle: 'so-handle', |
| 18 | + reputation: 2, |
| 19 | + answers: 2 |
| 20 | + } |
| 21 | + }, |
| 22 | + { |
| 23 | + provider: 'behance', |
| 24 | + data: { |
| 25 | + name: 'behance name', |
| 26 | + projectViews: 3, |
| 27 | + projectAppreciations: 3 |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + provider: 'dribbble', |
| 32 | + data: { |
| 33 | + handle: 'dribbble-handle', |
| 34 | + followers: 4, |
| 35 | + likes: 4 |
| 36 | + } |
| 37 | + }, |
| 38 | + { |
| 39 | + provider: 'bitbucket', |
| 40 | + data: { |
| 41 | + username: 'bitbucket-username', |
| 42 | + followers: 5, |
| 43 | + repositories: 5 |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + provider: 'twitter', |
| 48 | + data: { |
| 49 | + handle: 'twitter-handle', |
| 50 | + noOfTweets: 6, |
| 51 | + followers: 6 |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + provider: 'linkedin', |
| 56 | + data: { |
| 57 | + status: 'pending' |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + provider: 'weblink', |
| 62 | + key: 'somekey' |
| 63 | + } |
| 64 | + ]; |
| 65 | + var createController = function(toDelete, linkedAccounts) { |
| 66 | + return $controller('ExternalLinkDeletionController', { |
| 67 | + ExternalWebLinksService : extLinkSvc, |
| 68 | + toaster: toasterSvc, |
| 69 | + userHandle: 'test', |
| 70 | + account: toDelete, |
| 71 | + linkedAccountsData: linkedAccounts |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + beforeEach(function() { |
| 76 | + bard.appModule('topcoder'); |
| 77 | + bard.inject(this, '$compile', '$rootScope', 'toaster', 'ExternalWebLinksService', '$q', 'ngDialog', '$controller'); |
| 78 | + scope = $rootScope.$new(); |
| 79 | + |
| 80 | + extLinkSvc = ExternalWebLinksService; |
| 81 | + |
| 82 | + sinon.stub(extLinkSvc, 'removeLink', function(handle, key) { |
| 83 | + var $deferred = $q.defer(); |
| 84 | + if (key === 'throwNotExistsError') { |
| 85 | + $deferred.reject({ |
| 86 | + status: 'WEBLINK_NOT_EXIST', |
| 87 | + msg: 'profile not exists' |
| 88 | + }); |
| 89 | + } else if(key === 'throwFatalError') { |
| 90 | + $deferred.reject({ |
| 91 | + status: 'FATAL_ERROR', |
| 92 | + msg: 'fatal error' |
| 93 | + }); |
| 94 | + } else { |
| 95 | + $deferred.resolve({ |
| 96 | + status: 'SUCCESS' |
| 97 | + }); |
| 98 | + } |
| 99 | + return $deferred.promise; |
| 100 | + }); |
| 101 | + |
| 102 | + toasterSvc = toaster; |
| 103 | + bard.mockService(toaster, { |
| 104 | + pop: $q.when(true), |
| 105 | + default: $q.when(true) |
| 106 | + }); |
| 107 | + |
| 108 | + ngDialogSvc = ngDialog; |
| 109 | + sinon.stub(ngDialog, 'open', function() { |
| 110 | + ngDialog.deferredClose = $q.defer(); |
| 111 | + return { closePromise : ngDialog.deferredClose.promise }; |
| 112 | + }); |
| 113 | + sinon.stub(ngDialog, 'close', function() { |
| 114 | + ngDialog.deferredClose.resolve('closing'); |
| 115 | + return |
| 116 | + }) |
| 117 | + }); |
| 118 | + |
| 119 | + bard.verifyNoOutstandingHttpRequests(); |
| 120 | + |
| 121 | + describe('Linked external accounts', function() { |
| 122 | + var linkedAccounts = null; |
| 123 | + var externalLinksData; |
| 124 | + |
| 125 | + beforeEach(function() { |
| 126 | + linkedAccounts = angular.copy(mockLinkedAccounts); |
| 127 | + }); |
| 128 | + |
| 129 | + afterEach(function() { |
| 130 | + linkedAccounts = angular.copy(mockLinkedAccounts); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should remove weblink ', function() { |
| 134 | + var toDelete = {key: 'somekey', provider: 'weblink'}; |
| 135 | + ctrl = createController(toDelete, linkedAccounts); |
| 136 | + ctrl.deleteAccount(); |
| 137 | + $rootScope.$apply(); |
| 138 | + expect(toasterSvc.pop).to.have.been.calledWith('success').calledOnce; |
| 139 | + expect(linkedAccounts).to.have.length(7); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should show success if controller doesn\'t have weblink but API returns success ', function() { |
| 143 | + var toDelete = {key: 'somekey1', provider: 'weblink'}; |
| 144 | + ctrl = createController(toDelete, linkedAccounts); |
| 145 | + ctrl.deleteAccount(); |
| 146 | + $rootScope.$apply(); |
| 147 | + expect(toasterSvc.pop).to.have.been.calledWith('success').calledOnce; |
| 148 | + expect(linkedAccounts).to.have.length(8); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should NOT remove weblink with fatal error ', function() { |
| 152 | + var toDelete = {key: 'throwFatalError', provider: 'weblink'}; |
| 153 | + ctrl = createController(toDelete, linkedAccounts); |
| 154 | + ctrl.deleteAccount(); |
| 155 | + $rootScope.$apply(); |
| 156 | + expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('Sorry!')).calledOnce; |
| 157 | + expect(linkedAccounts).to.have.length(8); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should NOT remove weblink with already removed weblink ', function() { |
| 161 | + var toDelete = {key: 'throwNotExistsError', provider: 'weblink'}; |
| 162 | + ctrl = createController(toDelete, linkedAccounts); |
| 163 | + ctrl.deleteAccount(); |
| 164 | + $rootScope.$apply(); |
| 165 | + expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('not linked')).calledOnce; |
| 166 | + expect(linkedAccounts).to.have.length(8); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should not do any thing when already a deletion is in progress ', function() { |
| 170 | + var toDelete = {key: 'somekey', provider: 'weblink', deletingAccount: true}; |
| 171 | + ctrl = createController(toDelete, linkedAccounts); |
| 172 | + ctrl.deleteAccount(); |
| 173 | + $rootScope.$apply(); |
| 174 | + expect(extLinkSvc.removeLink).not.to.be.called; |
| 175 | + expect(toasterSvc.pop).not.to.be.called; |
| 176 | + expect(linkedAccounts).to.have.length(8); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should not do any thing for non weblink provider ', function() { |
| 180 | + var toDelete = {key: 'somekey', provider: 'stackoverflow'}; |
| 181 | + ctrl = createController(toDelete, linkedAccounts); |
| 182 | + ctrl.deleteAccount(); |
| 183 | + $rootScope.$apply(); |
| 184 | + expect(extLinkSvc.removeLink).not.to.be.called; |
| 185 | + expect(toasterSvc.pop).not.to.be.called; |
| 186 | + expect(linkedAccounts).to.have.length(8); |
| 187 | + }); |
| 188 | + |
| 189 | + }); |
| 190 | +}); |
0 commit comments