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