import './index'; describe('Component VnClientWebAccess', () => { let $httpBackend; let $scope; let vnApp; let controller; beforeEach(ngModule('client')); beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _vnApp_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; vnApp = _vnApp_; jest.spyOn(vnApp, 'showError'); const $element = angular.element(''); controller = $componentController('vnClientWebAccess', {$element, $scope}); })); describe('$onChanges()', () => { it(`should pass client's account data to account then call isCustomer function`, () => { jest.spyOn(controller, 'isCustomer'); controller.client = {client: 'Bruce Wayne', account: 'Wayne Industries'}; controller.account = {}; controller.$onChanges(); expect(controller.account).toBe('Wayne Industries'); expect(controller.isCustomer).toHaveBeenCalledWith(); }); }); describe('isCustomer()', () => { it('should return true if the password can be modified', () => { controller.client = {id: '1234'}; $httpBackend.expectGET(`Clients/${controller.client.id}/hasCustomerRole`).respond({isCustomer: true}); controller.isCustomer(); $httpBackend.flush(); expect(controller.canChangePassword).toBeTruthy(); }); it(`should return a false if the password can't be modified`, () => { controller.client = {id: '1234'}; $httpBackend.expectGET(`Clients/${controller.client.id}/hasCustomerRole`).respond({isCustomer: false}); controller.isCustomer(); $httpBackend.flush(); expect(controller.canChangePassword).toBeFalsy(); }); }); describe('checkConditions()', () => { it(`should perform a query to check if the client is valid and then store a boolean into the controller`, () => { controller.client = {id: '1234'}; expect(controller.canEnableCheckBox).toBeTruthy(); $httpBackend.when('GET', `Clients/${controller.client.id}/isValidClient`).respond(false); $httpBackend.expectGET(`Clients/${controller.client.id}/isValidClient`); controller.checkConditions(); $httpBackend.flush(); expect(controller.canEnableCheckBox).toBeFalsy(); }); }); describe('onPassOpen()', () => { it('should set passwords to empty values', () => { controller.newPassword = 'm24x8'; controller.repeatPassword = 'm24x8'; controller.onPassOpen(); expect(controller.newPassword).toBe(''); expect(controller.repeatPassword).toBe(''); }); }); describe('onPassChange()', () => { it('should request to update the password', () => { controller.client = {id: '1234'}; controller.newPassword = 'm24x8'; controller.repeatPassword = 'm24x8'; controller.canChangePassword = true; $httpBackend.when('PATCH', 'Accounts/1234').respond('done'); $httpBackend.expectPATCH('Accounts/1234', {password: 'm24x8'}); controller.onPassChange('accept'); $httpBackend.flush(); }); describe(`when password is empty`, () => { it(`should throw 'You must enter a new password' error`, () => { controller.client = {id: '1234'}; controller.newPassword = ''; controller.canChangePassword = true; controller.onPassChange('accept'); expect(vnApp.showError).toHaveBeenCalledWith(`You must enter a new password`); }); }); describe(`when passwords don't match`, () => { it(`should throw Passwords don't match error`, () => { controller.client = {id: '1234'}; controller.newPassword = 'm24x8'; controller.canChangePassword = true; controller.repeatPassword = 'notMatchingPassword'; controller.onPassChange('accept'); expect(vnApp.showError).toHaveBeenCalledWith(`Passwords don't match`); }); }); }); });