]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
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 | ||
f67539c2 TL |
9 | import { AuthService } from '~/app/shared/api/auth.service'; |
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 { configureTestBed, FormHelper } from '~/testing/unit-test-helper'; | |
9f95a23c TL |
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 | ||
f6b5b4d7 TL |
27 | configureTestBed({ |
28 | imports: [ | |
29 | HttpClientTestingModule, | |
30 | RouterTestingModule, | |
31 | ReactiveFormsModule, | |
32 | ComponentsModule, | |
33 | ToastrModule.forRoot(), | |
34 | SharedModule | |
35 | ], | |
f67539c2 | 36 | declarations: [LoginPasswordFormComponent] |
f6b5b4d7 | 37 | }); |
9f95a23c TL |
38 | |
39 | beforeEach(() => { | |
40 | fixture = TestBed.createComponent(LoginPasswordFormComponent); | |
41 | component = fixture.componentInstance; | |
f67539c2 TL |
42 | httpTesting = TestBed.inject(HttpTestingController); |
43 | router = TestBed.inject(Router); | |
44 | authStorageService = TestBed.inject(AuthStorageService); | |
45 | authService = TestBed.inject(AuthService); | |
9f95a23c TL |
46 | spyOn(router, 'navigate'); |
47 | fixture.detectChanges(); | |
48 | form = component.userForm; | |
49 | formHelper = new FormHelper(form); | |
50 | }); | |
51 | ||
52 | it('should create', () => { | |
53 | expect(component).toBeTruthy(); | |
54 | }); | |
55 | ||
56 | it('should submit', () => { | |
57 | spyOn(component, 'onPasswordChange').and.callThrough(); | |
58 | spyOn(authService, 'logout'); | |
59 | spyOn(authStorageService, 'getUsername').and.returnValue('test1'); | |
60 | formHelper.setMultipleValues({ | |
61 | oldpassword: 'foo', | |
62 | newpassword: 'bar' | |
63 | }); | |
64 | formHelper.setValue('confirmnewpassword', 'bar', true); | |
65 | component.onSubmit(); | |
66 | const request = httpTesting.expectOne('api/user/test1/change_password'); | |
67 | request.flush({}); | |
68 | expect(component.onPasswordChange).toHaveBeenCalled(); | |
69 | expect(authService.logout).toHaveBeenCalled(); | |
70 | }); | |
71 | ||
72 | it('should cancel', () => { | |
73 | spyOn(authService, 'logout'); | |
74 | component.onCancel(); | |
75 | expect(authService.logout).toHaveBeenCalled(); | |
76 | }); | |
77 | }); |