1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { ToastrModule } from 'ngx-toastr';
9 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
10 import { ComponentsModule } from '../../../shared/components/components.module';
11 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
12 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
13 import { SharedModule } from '../../../shared/shared.module';
14 import { UserPasswordFormComponent } from './user-password-form.component';
16 describe('UserPasswordFormComponent', () => {
17 let component: UserPasswordFormComponent;
18 let fixture: ComponentFixture<UserPasswordFormComponent>;
19 let form: CdFormGroup;
20 let formHelper: FormHelper;
21 let httpTesting: HttpTestingController;
23 let authStorageService: AuthStorageService;
28 HttpClientTestingModule,
32 ToastrModule.forRoot(),
35 declarations: [UserPasswordFormComponent],
36 providers: i18nProviders
42 fixture = TestBed.createComponent(UserPasswordFormComponent);
43 component = fixture.componentInstance;
44 form = component.userForm;
45 httpTesting = TestBed.get(HttpTestingController);
46 router = TestBed.get(Router);
47 authStorageService = TestBed.get(AuthStorageService);
48 spyOn(router, 'navigate');
49 fixture.detectChanges();
50 formHelper = new FormHelper(form);
53 it('should create', () => {
54 expect(component).toBeTruthy();
57 it('should validate old password required', () => {
58 formHelper.expectErrorChange('oldpassword', '', 'required');
59 formHelper.expectValidChange('oldpassword', 'foo');
62 it('should validate password match', () => {
63 formHelper.setValue('newpassword', 'aaa');
64 formHelper.expectErrorChange('confirmnewpassword', 'bbb', 'match');
65 formHelper.expectValidChange('confirmnewpassword', 'aaa');
68 it('should submit', () => {
69 spyOn(component, 'onPasswordChange').and.callThrough();
70 spyOn(authStorageService, 'getUsername').and.returnValue('xyz');
71 formHelper.setMultipleValues({
75 formHelper.setValue('confirmnewpassword', 'bar', true);
77 const request = httpTesting.expectOne('api/user/xyz/change_password');
78 expect(request.request.method).toBe('POST');
79 expect(request.request.body).toEqual({
84 expect(component.onPasswordChange).toHaveBeenCalled();
85 expect(router.navigate).toHaveBeenCalledWith(['/logout']);