]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
cf21e749ad70ff4dda40841429e752df5d71970d
[ceph.git] /
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';
6
7 import { ToastrModule } from 'ngx-toastr';
8
9 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
10 import { AuthService } from '../../../shared/api/auth.service';
11 import { ComponentsModule } from '../../../shared/components/components.module';
12 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
13 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
14 import { SharedModule } from '../../../shared/shared.module';
15 import { LoginPasswordFormComponent } from './login-password-form.component';
16
17 describe('LoginPasswordFormComponent', () => {
18   let component: LoginPasswordFormComponent;
19   let fixture: ComponentFixture<LoginPasswordFormComponent>;
20   let form: CdFormGroup;
21   let formHelper: FormHelper;
22   let httpTesting: HttpTestingController;
23   let router: Router;
24   let authStorageService: AuthStorageService;
25   let authService: AuthService;
26
27   configureTestBed(
28     {
29       imports: [
30         HttpClientTestingModule,
31         RouterTestingModule,
32         ReactiveFormsModule,
33         ComponentsModule,
34         ToastrModule.forRoot(),
35         SharedModule
36       ],
37       declarations: [LoginPasswordFormComponent],
38       providers: i18nProviders
39     },
40     true
41   );
42
43   beforeEach(() => {
44     fixture = TestBed.createComponent(LoginPasswordFormComponent);
45     component = fixture.componentInstance;
46     httpTesting = TestBed.get(HttpTestingController);
47     router = TestBed.get(Router);
48     authStorageService = TestBed.get(AuthStorageService);
49     authService = TestBed.get(AuthService);
50     spyOn(router, 'navigate');
51     fixture.detectChanges();
52     form = component.userForm;
53     formHelper = new FormHelper(form);
54   });
55
56   it('should create', () => {
57     expect(component).toBeTruthy();
58   });
59
60   it('should submit', () => {
61     spyOn(component, 'onPasswordChange').and.callThrough();
62     spyOn(authService, 'logout');
63     spyOn(authStorageService, 'getUsername').and.returnValue('test1');
64     formHelper.setMultipleValues({
65       oldpassword: 'foo',
66       newpassword: 'bar'
67     });
68     formHelper.setValue('confirmnewpassword', 'bar', true);
69     component.onSubmit();
70     const request = httpTesting.expectOne('api/user/test1/change_password');
71     request.flush({});
72     expect(component.onPasswordChange).toHaveBeenCalled();
73     expect(authService.logout).toHaveBeenCalled();
74   });
75
76   it('should cancel', () => {
77     spyOn(authService, 'logout');
78     component.onCancel();
79     expect(authService.logout).toHaveBeenCalled();
80   });
81 });