|
1 | 1 | /* jshint -W117, -W030 */
|
2 | 2 | describe('Skill Picker Controller', function() {
|
3 | 3 | var vm;
|
| 4 | + var toasterSvc, memberCertService, profileService; |
| 5 | + var mockProfile = mockData.getMockProfile(); |
4 | 6 |
|
5 |
| - // beforeEach(function() { |
6 |
| - // bard.appModule('tc.skill-picker'); |
7 |
| - // bard.inject(this, '$controller', '$rootScope', '$q', 'userProfile'); |
| 7 | + beforeEach(function() { |
| 8 | + bard.appModule('tc.skill-picker'); |
| 9 | + bard.inject(this, '$controller', '$rootScope', '$q', 'MemberCertService', 'ProfileService', 'toaster', 'CONSTANTS'); |
8 | 10 |
|
9 |
| - // vm = $controller('SkillPickerController', { |
10 |
| - // }); |
11 |
| - // }); |
| 11 | + memberCertService = MemberCertService; |
| 12 | + profileService = ProfileService; |
| 13 | + var swiftProgramId = CONSTANTS.SWIFT_PROGRAM_ID; |
12 | 14 |
|
13 |
| - // bard.verifyNoOutstandingHttpRequests(); |
| 15 | + // mock member cert api |
| 16 | + sinon.stub(memberCertService, 'getMemberRegistration', function(userId, programId) { |
| 17 | + var deferred = $q.defer(); |
| 18 | + if (programId === swiftProgramId) { |
| 19 | + var resp = {eventId: swiftProgramId, userId: 12345}; |
| 20 | + deferred.resolve(resp); |
| 21 | + } else { |
| 22 | + deferred.resolve(); |
| 23 | + } |
| 24 | + return deferred.promise; |
| 25 | + }); |
| 26 | + |
| 27 | + sinon.stub(memberCertService, 'registerMember', function(userId, programId) { |
| 28 | + var deferred = $q.defer(); |
| 29 | + var resp = {eventId: programId, userId: 12345}; |
| 30 | + if (programId === 100) { |
| 31 | + deferred.reject(); |
| 32 | + } else { |
| 33 | + deferred.resolve(resp); |
| 34 | + } |
| 35 | + return deferred.promise; |
| 36 | + }); |
14 | 37 |
|
15 |
| - // it('should be created successfully', function() { |
16 |
| - // expect(vm).to.exist; |
17 |
| - // }); |
| 38 | + // adds spy method to mocked profile object for saving the profile |
| 39 | + mockProfile.save = sinon.spy(); |
| 40 | + |
| 41 | + // mock profile service |
| 42 | + sinon.stub(profileService, 'updateUserSkills', function(username, skills) { |
| 43 | + var deferred = $q.defer(); |
| 44 | + var resp = {}; |
| 45 | + if (skills[412]) { |
| 46 | + deferred.reject(); |
| 47 | + } else { |
| 48 | + deferred.resolve(resp); |
| 49 | + } |
| 50 | + return deferred.promise; |
| 51 | + }); |
| 52 | + |
| 53 | + // mocks the toaster service |
| 54 | + toasterSvc = toaster; |
| 55 | + bard.mockService(toaster, { |
| 56 | + pop: $q.when(true), |
| 57 | + default: $q.when(true) |
| 58 | + }); |
| 59 | + |
| 60 | + var scope = $rootScope.$new(); |
| 61 | + vm = $controller('SkillPickerController', { |
| 62 | + $scope: scope, |
| 63 | + userProfile: mockProfile, |
| 64 | + featuredSkills: [] |
| 65 | + }); |
| 66 | + $rootScope.$digest(); |
| 67 | + }); |
| 68 | + |
| 69 | + bard.verifyNoOutstandingHttpRequests(); |
| 70 | + |
| 71 | + it('should be created successfully', function() { |
| 72 | + expect(vm).to.exist; |
| 73 | + expect(vm.showCommunity).to.exist.to.false; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should have empty tracks object ', function() { |
| 77 | + expect(vm.tracks).to.exist; |
| 78 | + expect(vm.tracks.DESIGN).not.to.exist; |
| 79 | + expect(vm.tracks.DEVELOP).not.to.exist; |
| 80 | + expect(vm.tracks.DATA_SCIENCE).not.to.exist; |
| 81 | + }); |
| 82 | + |
| 83 | + it('should have correct userIdentity ', function() { |
| 84 | + expect(vm.userId).to.exist.to.equal(mockProfile.userId); |
| 85 | + expect(vm.username).to.exist.to.equal(mockProfile.handle); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not have page dirty ', function() { |
| 89 | + var dirty = vm.isPageDirty(); |
| 90 | + expect(dirty).to.equal(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should be created successfully with showCommunity being true', function() { |
| 94 | + // backup original swift program id, to restore after test execution |
| 95 | + var origSwiftProgId = CONSTANTS.SWIFT_PROGRAM_ID; |
| 96 | + // update swift program id to something which says, member is not registered |
| 97 | + CONSTANTS.SWIFT_PROGRAM_ID = 3446; |
| 98 | + vm = $controller('SkillPickerController', { |
| 99 | + $scope: $rootScope.$new(), |
| 100 | + userProfile: mockProfile, |
| 101 | + featuredSkills: [] |
| 102 | + }); |
| 103 | + $rootScope.$digest(); |
| 104 | + expect(vm).to.exist; |
| 105 | + // showCommunity flag should be set to true because we have at least one unregistered community |
| 106 | + expect(vm.showCommunity).to.exist.to.true; |
| 107 | + // restores the original swift program id |
| 108 | + CONSTANTS.SWIFT_PROGRAM_ID = origSwiftProgId; |
| 109 | + }); |
| 110 | + |
| 111 | + it('should add skill ', function() { |
| 112 | + vm.toggleSkill(409); |
| 113 | + expect(vm.mySkills).to.exist.have.length(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should remove added skill ', function() { |
| 117 | + vm.toggleSkill(409); |
| 118 | + // should add the skill |
| 119 | + expect(vm.mySkills).to.exist.have.length(1); |
| 120 | + // next call to toggleSkill with same argument, should remove that skill |
| 121 | + vm.toggleSkill(409); |
| 122 | + // should remove the skill |
| 123 | + expect(vm.mySkills).to.exist.have.length(0); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should not make any update call without any change ', function() { |
| 127 | + vm.submitSkills(); |
| 128 | + $rootScope.$digest(); |
| 129 | + expect(mockProfile.save).not.to.be.called; |
| 130 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 131 | + expect(memberCertService.registerMember).not.to.be.called; |
| 132 | + }); |
| 133 | + |
| 134 | + it('should update tracks for the member ', function() { |
| 135 | + vm.tracks.DEVELOP = true; |
| 136 | + vm.tracks.DESIGN = false; |
| 137 | + vm.submitSkills(); |
| 138 | + $rootScope.$digest(); |
| 139 | + expect(mockProfile.save).to.be.calledOnce; |
| 140 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 141 | + expect(memberCertService.registerMember).not.to.be.called; |
| 142 | + }); |
| 143 | + |
| 144 | + it('should show error popup for updating tracks ', function() { |
| 145 | + vm.tracks.DEVELOP = true; |
| 146 | + // stubs the save method to reject the promise |
| 147 | + mockProfile.save = function() {}; |
| 148 | + sinon.stub(mockProfile, 'save', function() { |
| 149 | + var deferred = $q.defer(); |
| 150 | + deferred.reject(); |
| 151 | + return deferred.promise; |
| 152 | + }); |
| 153 | + vm.submitSkills(); |
| 154 | + $rootScope.$digest(); |
| 155 | + expect(mockProfile.save).to.be.calledOnce; |
| 156 | + expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('wrong')).calledOnce; |
| 157 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 158 | + expect(memberCertService.registerMember).not.to.be.called; |
| 159 | + }); |
| 160 | + |
| 161 | + it('should update skills for the member ', function() { |
| 162 | + vm.mySkills = [409, 410]; |
| 163 | + vm.submitSkills(); |
| 164 | + $rootScope.$digest(); |
| 165 | + expect(mockProfile.save).not.to.be.called; |
| 166 | + expect(profileService.updateUserSkills).to.be.calledOnce; |
| 167 | + expect(memberCertService.registerMember).not.to.be.called; |
| 168 | + }); |
| 169 | + |
| 170 | + it('should show error popup for error in updating skills ', function() { |
| 171 | + vm.mySkills = [409, 410, 412]; |
| 172 | + vm.submitSkills(); |
| 173 | + $rootScope.$digest(); |
| 174 | + expect(mockProfile.save).not.to.be.called; |
| 175 | + expect(profileService.updateUserSkills).to.be.calledOnce; |
| 176 | + expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('wrong')).calledOnce; |
| 177 | + expect(memberCertService.registerMember).not.to.be.called; |
| 178 | + }); |
| 179 | + |
| 180 | + it('should update communities for the member ', function() { |
| 181 | + vm.communities['ios'].status = true; |
| 182 | + vm.communities['ios'].dirty = true; |
| 183 | + vm.submitSkills(); |
| 184 | + $rootScope.$digest(); |
| 185 | + expect(mockProfile.save).not.to.be.called; |
| 186 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 187 | + expect(memberCertService.registerMember).to.be.calledOnce; |
| 188 | + }); |
| 189 | + |
| 190 | + // we may need to update this test case when we want to call unregister endpoint |
| 191 | + it('should NOT update communities for the member for disabled community ', function() { |
| 192 | + vm.communities['ios'].status = false; |
| 193 | + vm.communities['ios'].dirty = true; |
| 194 | + vm.submitSkills(); |
| 195 | + $rootScope.$digest(); |
| 196 | + expect(mockProfile.save).not.to.be.called; |
| 197 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 198 | + expect(memberCertService.registerMember).not.to.be.called; |
| 199 | + }); |
| 200 | + |
| 201 | + it('should NOT update communities for the member for enabled but non dirty community ', function() { |
| 202 | + // TODO need one more community, with dirty true, to test out the non dirty community update |
| 203 | + vm.communities['ios'].status = true; |
| 204 | + vm.communities['ios'].dirty = false; |
| 205 | + vm.submitSkills(); |
| 206 | + $rootScope.$digest(); |
| 207 | + expect(mockProfile.save).not.to.be.called; |
| 208 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 209 | + expect(memberCertService.registerMember).not.to.be.called; |
| 210 | + }); |
| 211 | + |
| 212 | + it('should show error popup for error in updating communities ', function() { |
| 213 | + vm.communities['ios'].programId = 100;// to cause rejction of the promise |
| 214 | + vm.communities['ios'].status = true; |
| 215 | + vm.communities['ios'].dirty = true; |
| 216 | + vm.submitSkills(); |
| 217 | + $rootScope.$digest(); |
| 218 | + expect(mockProfile.save).not.to.be.called; |
| 219 | + expect(profileService.updateUserSkills).not.to.be.called; |
| 220 | + expect(memberCertService.registerMember).to.be.calledOnce; |
| 221 | + expect(toasterSvc.pop).to.have.been.calledWith('error', "Whoops!", sinon.match('wrong')).calledOnce; |
| 222 | + }); |
18 | 223 |
|
19 | 224 | });
|
0 commit comments