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 } from '~/testing/unit-test-helper';
10 import { ComponentsModule } from '~/app/shared/components/components.module';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { SharedModule } from '~/app/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;
27 HttpClientTestingModule,
31 ToastrModule.forRoot(),
34 declarations: [UserPasswordFormComponent]
38 fixture = TestBed.createComponent(UserPasswordFormComponent);
39 component = fixture.componentInstance;
40 form = component.userForm;
41 httpTesting = TestBed.inject(HttpTestingController);
42 router = TestBed.inject(Router);
43 authStorageService = TestBed.inject(AuthStorageService);
44 spyOn(router, 'navigate');
45 fixture.detectChanges();
46 formHelper = new FormHelper(form);
49 it('should create', () => {
50 expect(component).toBeTruthy();
53 it('should validate old password required', () => {
54 formHelper.expectErrorChange('oldpassword', '', 'required');
55 formHelper.expectValidChange('oldpassword', 'foo');
58 it('should validate password match', () => {
59 formHelper.setValue('newpassword', 'aaa');
60 formHelper.expectErrorChange('confirmnewpassword', 'bbb', 'match');
61 formHelper.expectValidChange('confirmnewpassword', 'aaa');
64 it('should submit', () => {
65 spyOn(component, 'onPasswordChange').and.callThrough();
66 spyOn(authStorageService, 'getUsername').and.returnValue('xyz');
67 formHelper.setMultipleValues({
71 formHelper.setValue('confirmnewpassword', 'bar', true);
73 const request = httpTesting.expectOne('api/user/xyz/change_password');
74 expect(request.request.method).toBe('POST');
75 expect(request.request.body).toEqual({
80 expect(component.onPasswordChange).toHaveBeenCalled();
81 expect(router.navigate).toHaveBeenCalledWith(['/login']);