|
| 1 | +import { AccountVO } from '@models/account-vo'; |
| 2 | +import { |
| 3 | + savePropertyOnAccount, |
| 4 | + AccountChange, |
| 5 | +} from './account.service.helpers'; |
| 6 | + |
| 7 | +describe('savePropertyOnAccount', () => { |
| 8 | + let mockAccount: jasmine.SpyObj<AccountVO>; |
| 9 | + let mockApiService: any; |
| 10 | + let mockAccountService: any; |
| 11 | + let mockMessageService: any; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + mockAccount = jasmine.createSpyObj<AccountVO>('AccountVO', ['update'], { |
| 15 | + primaryEmail: '[email protected]', |
| 16 | + phoneStatus: 'status.auth.verified', |
| 17 | + primaryPhone: '1234567890', |
| 18 | + }); |
| 19 | + |
| 20 | + mockApiService = { |
| 21 | + account: { |
| 22 | + update: jasmine.createSpy(), |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + mockAccountService = { |
| 27 | + setAccount: jasmine.createSpy(), |
| 28 | + }; |
| 29 | + |
| 30 | + mockMessageService = { |
| 31 | + showMessage: jasmine.createSpy(), |
| 32 | + showError: jasmine.createSpy(), |
| 33 | + }; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should update the account and show success message on success', async () => { |
| 37 | + const change: AccountChange = { prop: 'primaryPhone', value: '9876543210' }; |
| 38 | + |
| 39 | + mockApiService.account.update.and.resolveTo({ |
| 40 | + primaryPhone: '9876543210', |
| 41 | + phoneStatus: 'status.auth.verified', |
| 42 | + }); |
| 43 | + |
| 44 | + await savePropertyOnAccount(mockAccount, change, { |
| 45 | + messageService: mockMessageService, |
| 46 | + apiService: mockApiService, |
| 47 | + accountService: mockAccountService, |
| 48 | + }); |
| 49 | + |
| 50 | + expect(mockAccount.update).toHaveBeenCalledWith( |
| 51 | + jasmine.objectContaining({ |
| 52 | + primaryEmail: '[email protected]', |
| 53 | + primaryPhone: '9876543210', |
| 54 | + phoneStatus: 'status.auth.verified', |
| 55 | + }), |
| 56 | + ); |
| 57 | + |
| 58 | + expect(mockApiService.account.update).toHaveBeenCalledWith( |
| 59 | + jasmine.objectContaining({ |
| 60 | + primaryEmail: '[email protected]', |
| 61 | + primaryPhone: '9876543210', |
| 62 | + phoneStatus: 'status.auth.verified', |
| 63 | + }), |
| 64 | + ); |
| 65 | + |
| 66 | + expect(mockAccountService.setAccount).toHaveBeenCalledWith(mockAccount); |
| 67 | + expect(mockMessageService.showMessage).toHaveBeenCalledWith({ |
| 68 | + message: 'Account information saved.', |
| 69 | + style: 'success', |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should revert changes and show error message on failure', async () => { |
| 74 | + const change: AccountChange = { prop: 'primaryPhone', value: '9876543210' }; |
| 75 | + |
| 76 | + mockApiService.account.update.and.rejectWith(new Error('API Error')); |
| 77 | + |
| 78 | + await savePropertyOnAccount(mockAccount, change, { |
| 79 | + messageService: mockMessageService, |
| 80 | + apiService: mockApiService, |
| 81 | + accountService: mockAccountService, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(mockAccount.update).toHaveBeenCalledWith({ |
| 85 | + [change.prop]: '1234567890', |
| 86 | + }); |
| 87 | + |
| 88 | + expect(mockMessageService.showError).toHaveBeenCalledWith({ |
| 89 | + message: 'There was a problem saving your account changes', |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should set phoneStatus to unverified when primaryPhone is cleared', async () => { |
| 94 | + const change = { |
| 95 | + prop: 'primaryPhone' as keyof AccountVO, |
| 96 | + value: '', |
| 97 | + }; |
| 98 | + |
| 99 | + mockApiService.account.update.and.returnValue( |
| 100 | + Promise.resolve({ |
| 101 | + primaryPhone: '', |
| 102 | + phoneStatus: 'status.auth.unverified', |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + await savePropertyOnAccount(mockAccount, change, { |
| 107 | + messageService: mockMessageService, |
| 108 | + apiService: mockApiService, |
| 109 | + accountService: mockAccountService, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(mockAccount.update).toHaveBeenCalledWith( |
| 113 | + jasmine.objectContaining({ |
| 114 | + primaryPhone: '', |
| 115 | + phoneStatus: 'status.auth.unverified', |
| 116 | + primaryEmail: '[email protected]', |
| 117 | + }), |
| 118 | + ); |
| 119 | + |
| 120 | + expect(mockApiService.account.update).toHaveBeenCalledWith( |
| 121 | + jasmine.objectContaining({ |
| 122 | + primaryPhone: '', |
| 123 | + phoneStatus: 'status.auth.unverified', |
| 124 | + primaryEmail: '[email protected]', |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + expect(mockAccount.update).toHaveBeenCalledWith( |
| 129 | + jasmine.objectContaining({ |
| 130 | + primaryPhone: '', |
| 131 | + phoneStatus: 'status.auth.unverified', |
| 132 | + }), |
| 133 | + ); |
| 134 | + |
| 135 | + expect(mockMessageService.showMessage).toHaveBeenCalledWith({ |
| 136 | + message: 'Account information saved.', |
| 137 | + style: 'success', |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments