import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { BaresTestModule } from '../../../test.module'; import { PasswordComponent } from 'app/account/password/password.component'; import { PasswordService } from 'app/account/password/password.service'; import { Principal } from 'app/core/auth/principal.service'; import { AccountService } from 'app/core/auth/account.service'; import { JhiTrackerService } from 'app/core/tracker/tracker.service'; import { MockTrackerService } from '../../../helpers/mock-tracker.service'; describe('Component Tests', () => { describe('PasswordComponent', () => { let comp: PasswordComponent; let fixture: ComponentFixture; let service: PasswordService; beforeEach( async(() => { TestBed.configureTestingModule({ imports: [BaresTestModule], declarations: [PasswordComponent], providers: [ Principal, AccountService, { provide: JhiTrackerService, useClass: MockTrackerService }, PasswordService ] }) .overrideTemplate(PasswordComponent, '') .compileComponents(); }) ); beforeEach(() => { fixture = TestBed.createComponent(PasswordComponent); comp = fixture.componentInstance; service = fixture.debugElement.injector.get(PasswordService); }); it('should show error if passwords do not match', () => { // GIVEN comp.newPassword = 'password1'; comp.confirmPassword = 'password2'; // WHEN comp.changePassword(); // THEN expect(comp.doNotMatch).toBe('ERROR'); expect(comp.error).toBeNull(); expect(comp.success).toBeNull(); }); it('should call Auth.changePassword when passwords match', () => { // GIVEN const passwordValues = { currentPassword: 'oldPassword', newPassword: 'myPassword' }; spyOn(service, 'save').and.returnValue(Observable.of(new HttpResponse({ body: true }))); comp.currentPassword = passwordValues.currentPassword; comp.newPassword = comp.confirmPassword = passwordValues.newPassword; // WHEN comp.changePassword(); // THEN expect(service.save).toHaveBeenCalledWith(passwordValues.newPassword, passwordValues.currentPassword); }); it('should set success to OK upon success', function() { // GIVEN spyOn(service, 'save').and.returnValue(Observable.of(new HttpResponse({ body: true }))); comp.newPassword = comp.confirmPassword = 'myPassword'; // WHEN comp.changePassword(); // THEN expect(comp.doNotMatch).toBeNull(); expect(comp.error).toBeNull(); expect(comp.success).toBe('OK'); }); it('should notify of error if change password fails', function() { // GIVEN spyOn(service, 'save').and.returnValue(Observable.throw('ERROR')); comp.newPassword = comp.confirmPassword = 'myPassword'; // WHEN comp.changePassword(); // THEN expect(comp.doNotMatch).toBeNull(); expect(comp.success).toBeNull(); expect(comp.error).toBe('ERROR'); }); }); });