|
| 1 | +import * as ChallengesService from 'services/challenges'; |
| 2 | +import * as MembersService from 'services/members'; |
| 3 | +import * as UserService from 'services/user'; |
| 4 | + |
| 5 | +import actions from 'actions/profile'; |
| 6 | + |
| 7 | +const handle = 'tcscoder'; |
| 8 | +const tokenV3 = 'tokenV3'; |
| 9 | +const profile = { userId: 12345, handle }; |
| 10 | +const skill = { tagId: 123, tagName: 'Node.js' }; |
| 11 | +const weblink = 'https://www.google.com'; |
| 12 | +const linkedAccounts = [{ |
| 13 | + providerType: 'github', |
| 14 | + social: true, |
| 15 | + userId: '623633', |
| 16 | +}]; |
| 17 | + |
| 18 | +// Mock services |
| 19 | +const mockChanllengesService = { |
| 20 | + getUserChallenges: jest.fn().mockReturnValue(Promise.resolve({ totalCount: 3 })), |
| 21 | + getUserMarathonMatches: jest.fn().mockReturnValue(Promise.resolve({ totalCount: 5 })), |
| 22 | +}; |
| 23 | +ChallengesService.getService = jest.fn().mockReturnValue(mockChanllengesService); |
| 24 | + |
| 25 | +const mockMembersService = { |
| 26 | + getPresignedUrl: jest.fn().mockReturnValue(Promise.resolve()), |
| 27 | + uploadFileToS3: jest.fn().mockReturnValue(Promise.resolve()), |
| 28 | + updateMemberPhoto: jest.fn().mockReturnValue(Promise.resolve('url-of-photo')), |
| 29 | + updateMemberProfile: jest.fn().mockReturnValue(Promise.resolve(profile)), |
| 30 | + addSkill: jest.fn().mockReturnValue(Promise.resolve({ skills: [skill] })), |
| 31 | + hideSkill: jest.fn().mockReturnValue(Promise.resolve({ skills: [] })), |
| 32 | + addWebLink: jest.fn().mockReturnValue(Promise.resolve(weblink)), |
| 33 | + deleteWebLink: jest.fn().mockReturnValue(Promise.resolve(weblink)), |
| 34 | +}; |
| 35 | +MembersService.getService = jest.fn().mockReturnValue(mockMembersService); |
| 36 | + |
| 37 | +const mockUserService = { |
| 38 | + linkExternalAccount: jest.fn().mockReturnValue(Promise.resolve(linkedAccounts[0])), |
| 39 | + unlinkExternalAccount: jest.fn().mockReturnValue(Promise.resolve('unlinked')), |
| 40 | + getLinkedAccounts: jest.fn().mockReturnValue(Promise.resolve({ profiles: linkedAccounts })), |
| 41 | + getCredential: jest.fn().mockReturnValue(Promise.resolve({ credential: { hasPassword: true } })), |
| 42 | + getEmailPreferences: |
| 43 | + jest.fn().mockReturnValue(Promise.resolve({ subscriptions: { TOPCODER_NL_DATA: true } })), |
| 44 | + saveEmailPreferences: |
| 45 | + jest.fn().mockReturnValue(Promise.resolve({ subscriptions: { TOPCODER_NL_DATA: true } })), |
| 46 | + updatePassword: jest.fn().mockReturnValue(Promise.resolve({ update: true })), |
| 47 | +}; |
| 48 | +UserService.getService = jest.fn().mockReturnValue(mockUserService); |
| 49 | + |
| 50 | + |
| 51 | +describe('profile.getActiveChallengesCountDone', () => { |
| 52 | + const a = actions.profile.getActiveChallengesCountDone(handle, tokenV3); |
| 53 | + |
| 54 | + test('has expected type', () => { |
| 55 | + expect(a.type).toBe('PROFILE/GET_ACTIVE_CHALLENGES_COUNT_DONE'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('Sum of challenges and marathon matches should be returned', () => |
| 59 | + a.payload.then((res) => { |
| 60 | + expect(res).toBe(8); |
| 61 | + expect(mockChanllengesService.getUserChallenges).toBeCalled(); |
| 62 | + expect(mockChanllengesService.getUserMarathonMatches).toBeCalled(); |
| 63 | + })); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('profile.uploadPhotoDone', () => { |
| 67 | + const a = actions.profile.uploadPhotoDone(handle, tokenV3); |
| 68 | + |
| 69 | + test('has expected type', () => { |
| 70 | + expect(a.type).toBe('PROFILE/UPLOAD_PHOTO_DONE'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Photo URL should be returned', () => |
| 74 | + a.payload.then((res) => { |
| 75 | + expect(res).toEqual({ |
| 76 | + handle, |
| 77 | + photoURL: 'url-of-photo', |
| 78 | + }); |
| 79 | + expect(mockMembersService.getPresignedUrl).toBeCalled(); |
| 80 | + expect(mockMembersService.uploadFileToS3).toBeCalled(); |
| 81 | + expect(mockMembersService.updateMemberPhoto).toBeCalled(); |
| 82 | + })); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('profile.updateProfileDone', () => { |
| 86 | + const a = actions.profile.updateProfileDone(profile, tokenV3); |
| 87 | + |
| 88 | + test('has expected type', () => { |
| 89 | + expect(a.type).toBe('PROFILE/UPDATE_PROFILE_DONE'); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Profile should be updated', () => |
| 93 | + a.payload.then((res) => { |
| 94 | + expect(res).toEqual(profile); |
| 95 | + expect(mockMembersService.updateMemberProfile).toBeCalled(); |
| 96 | + })); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('profile.addSkillDone', () => { |
| 100 | + const a = actions.profile.addSkillDone(handle, tokenV3, skill); |
| 101 | + |
| 102 | + test('has expected type', () => { |
| 103 | + expect(a.type).toBe('PROFILE/ADD_SKILL_DONE'); |
| 104 | + }); |
| 105 | + |
| 106 | + test('Skill should be added', () => |
| 107 | + a.payload.then((res) => { |
| 108 | + expect(res).toEqual({ skills: [skill], handle, skill }); |
| 109 | + expect(mockMembersService.addSkill).toBeCalled(); |
| 110 | + })); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('profile.hideSkillDone', () => { |
| 114 | + const a = actions.profile.hideSkillDone(handle, tokenV3, skill); |
| 115 | + |
| 116 | + test('has expected type', () => { |
| 117 | + expect(a.type).toBe('PROFILE/HIDE_SKILL_DONE'); |
| 118 | + }); |
| 119 | + |
| 120 | + test('Skill should be removed', () => |
| 121 | + a.payload.then((res) => { |
| 122 | + expect(res).toEqual({ skills: [], handle, skill }); |
| 123 | + expect(mockMembersService.hideSkill).toBeCalled(); |
| 124 | + })); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('profile.addWebLinkDone', () => { |
| 128 | + const a = actions.profile.addWebLinkDone(handle, tokenV3, weblink); |
| 129 | + |
| 130 | + test('has expected type', () => { |
| 131 | + expect(a.type).toBe('PROFILE/ADD_WEB_LINK_DONE'); |
| 132 | + }); |
| 133 | + |
| 134 | + test('Web link should be added', () => |
| 135 | + a.payload.then((res) => { |
| 136 | + expect(res).toEqual({ data: weblink, handle }); |
| 137 | + expect(mockMembersService.addWebLink).toBeCalled(); |
| 138 | + })); |
| 139 | +}); |
| 140 | + |
| 141 | +describe('profile.deleteWebLinkDone', () => { |
| 142 | + const a = actions.profile.deleteWebLinkDone(handle, tokenV3, weblink); |
| 143 | + |
| 144 | + test('has expected type', () => { |
| 145 | + expect(a.type).toBe('PROFILE/DELETE_WEB_LINK_DONE'); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Web link should be deleted', () => |
| 149 | + a.payload.then((res) => { |
| 150 | + expect(res).toEqual({ data: weblink, handle }); |
| 151 | + expect(mockMembersService.deleteWebLink).toBeCalled(); |
| 152 | + })); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('profile.linkExternalAccountDone', () => { |
| 156 | + const a = actions.profile.linkExternalAccountDone(profile, tokenV3, 'github'); |
| 157 | + |
| 158 | + test('has expected type', () => { |
| 159 | + expect(a.type).toBe('PROFILE/LINK_EXTERNAL_ACCOUNT_DONE'); |
| 160 | + }); |
| 161 | + |
| 162 | + test('External account should be linked', () => |
| 163 | + a.payload.then((res) => { |
| 164 | + expect(res).toEqual({ data: linkedAccounts[0], handle }); |
| 165 | + expect(mockUserService.linkExternalAccount).toBeCalled(); |
| 166 | + })); |
| 167 | +}); |
| 168 | + |
| 169 | +describe('profile.unlinkExternalAccountDone', () => { |
| 170 | + const a = actions.profile.unlinkExternalAccountDone(profile, tokenV3, 'github'); |
| 171 | + |
| 172 | + test('has expected type', () => { |
| 173 | + expect(a.type).toBe('PROFILE/UNLINK_EXTERNAL_ACCOUNT_DONE'); |
| 174 | + }); |
| 175 | + |
| 176 | + test('External account should be unlinked', () => |
| 177 | + a.payload.then((res) => { |
| 178 | + expect(res).toEqual({ handle, providerType: 'github' }); |
| 179 | + expect(mockUserService.unlinkExternalAccount).toBeCalled(); |
| 180 | + })); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('profile.getLinkedAccountsDone', () => { |
| 184 | + const a = actions.profile.getLinkedAccountsDone(profile, tokenV3); |
| 185 | + |
| 186 | + test('has expected type', () => { |
| 187 | + expect(a.type).toBe('PROFILE/GET_LINKED_ACCOUNTS_DONE'); |
| 188 | + }); |
| 189 | + |
| 190 | + test('Linked account should be returned', () => |
| 191 | + a.payload.then((res) => { |
| 192 | + expect(res).toEqual({ profiles: linkedAccounts }); |
| 193 | + expect(mockUserService.getLinkedAccounts).toBeCalled(); |
| 194 | + })); |
| 195 | +}); |
| 196 | + |
| 197 | +describe('profile.getCredentialDone', () => { |
| 198 | + const a = actions.profile.getCredentialDone(profile, tokenV3); |
| 199 | + |
| 200 | + test('has expected type', () => { |
| 201 | + expect(a.type).toBe('PROFILE/GET_CREDENTIAL_DONE'); |
| 202 | + }); |
| 203 | + |
| 204 | + test('Credential should be returned', () => |
| 205 | + a.payload.then((res) => { |
| 206 | + expect(res).toEqual({ credential: { hasPassword: true } }); |
| 207 | + expect(mockUserService.getCredential).toBeCalled(); |
| 208 | + })); |
| 209 | +}); |
| 210 | + |
| 211 | +describe('profile.getEmailPreferencesDone', () => { |
| 212 | + const a = actions.profile.getEmailPreferencesDone(profile, tokenV3); |
| 213 | + |
| 214 | + test('has expected type', () => { |
| 215 | + expect(a.type).toBe('PROFILE/GET_EMAIL_PREFERENCES_DONE'); |
| 216 | + }); |
| 217 | + |
| 218 | + test('Email preferences should be returned', () => |
| 219 | + a.payload.then((res) => { |
| 220 | + expect(res).toEqual({ subscriptions: { TOPCODER_NL_DATA: true } }); |
| 221 | + expect(mockUserService.getEmailPreferences).toBeCalled(); |
| 222 | + })); |
| 223 | +}); |
| 224 | + |
| 225 | +describe('profile.saveEmailPreferencesDone', () => { |
| 226 | + const a = actions.profile.saveEmailPreferencesDone(profile, tokenV3, {}); |
| 227 | + |
| 228 | + test('has expected type', () => { |
| 229 | + expect(a.type).toBe('PROFILE/SAVE_EMAIL_PREFERENCES_DONE'); |
| 230 | + }); |
| 231 | + |
| 232 | + test('Email preferences should be updated', () => |
| 233 | + a.payload.then((res) => { |
| 234 | + expect(res).toEqual({ handle, data: { subscriptions: { TOPCODER_NL_DATA: true } } }); |
| 235 | + expect(mockUserService.saveEmailPreferences).toBeCalled(); |
| 236 | + })); |
| 237 | +}); |
| 238 | + |
| 239 | +describe('profile.updatePasswordDone', () => { |
| 240 | + const a = actions.profile.updatePasswordDone(profile, tokenV3, 'newPassword', 'oldPassword'); |
| 241 | + |
| 242 | + test('has expected type', () => { |
| 243 | + expect(a.type).toBe('PROFILE/UPDATE_PASSWORD_DONE'); |
| 244 | + }); |
| 245 | + |
| 246 | + test('User password should be updated', () => |
| 247 | + a.payload.then((res) => { |
| 248 | + expect(res).toEqual({ handle, data: { update: true } }); |
| 249 | + expect(mockUserService.updatePassword).toBeCalled(); |
| 250 | + })); |
| 251 | +}); |
| 252 | + |
0 commit comments