This is login.component.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { LoginUser } from './loginUser.model'
import { UserService } from './user.service';
import { LoaderService } from '../shared/loader.service';
import 'rxjs/add/operator/toPromise';
@Component({
templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {
errorMessage: string;
loginForm: FormGroup;
loginObj = new LoginUser();
constructor(private userService: UserService, private loaderService: LoaderService, private router: Router, fb: FormBuilder) {
this.loginForm = fb.group({
userName: [null, Validators.required],
password: [null, Validators.required]
})
console.log(this.loginForm);
}
test() : void{
console.log("This is a test");
}
login(loginObjValue: any): void {
if (loginObjValue.userName == null || loginObjValue.password == null) {
console.log('error');
} else {
this.loginObj.userName = loginObjValue.userName;
this.loginObj.password = loginObjValue.password;
this.loaderService.displayLoader(true);
this.userService.login(this.loginObj)
.then((res) => {
console.log("data", res);
console.log("$localStorage.currentUser", localStorage);
let link = ['customercare/customer-ticket'];
this.loaderService.displayLoader(false);
this.router.navigate(link);
})
.catch(error => this.handleError(error));
}
}
private handleError(error: any): Promise<any> {
this.loaderService.displayLoader(false);
if (error._body) {
this.errorMessage = JSON.parse(error._body).error_description;
}
console.log('An error occurred', error);
return Promise.reject(error.message || error);
}
ngOnInit(): void {
}
}
@Component({
templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {
errorMessage: string;
loginForm: FormGroup;
loginObj = new LoginUser();
constructor(private userService: UserService, private loaderService: LoaderService, private router: Router, fb: FormBuilder) {
this.loginForm = fb.group({
userName: [null, Validators.required],
password: [null, Validators.required]
})
console.log(this.loginForm);
}
login(loginObjValue: any): void {
if (loginObjValue.userName == null || loginObjValue.password == null) {
console.log('error');
} else {
this.loginObj.userName = loginObjValue.userName;
this.loginObj.password = loginObjValue.password;
this.loaderService.displayLoader(true);
this.userService.login(this.loginObj)
.then((res) => {
console.log("data", res);
console.log("$localStorage.currentUser", localStorage);
let link = ['customercare/customer-ticket'];
this.loaderService.displayLoader(false);
this.router.navigate(link);
})
.catch(error => this.handleError(error));
}
}
}
Partial Code of userservice.ts . Please refer this code . Partial Code of userservice.ts . Please refer this code . Partial Code of userservice.ts . Please refer this code . Partial Code of userservice.ts . Please refer this code . Partial Code of userservice.ts . Please refer this code .
@Injectable()
export class UserService {
private URL = "";
constructor(private http: Http) { }
login(loginObj: LoginUser) {
let body = 'userName=' + loginObj.userName + '&password=' + loginObj.password + '&grant_type=password';
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(this.URL + '/token', body, { headers: headers })
.toPromise()
.then((res: Response) => {
let data = res.json();
if (data && data.access_token) {
localStorage.setItem('currentUser', JSON.stringify(data));
}
return data;
})
}
}
SO FAR I HAVE WRITTEN : I am unable to call login function . Please guide me .
**
describe('LoginComponent', () => {
let component: LoginComponent;
let UserService:UserService;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
{ provide: UserService, useValue: UserService },
]
})
it('should call the login method from the UserService',
inject([TestBed, UserService], fakeAsync((tcb: TestBed, mockUserService: UserService) => {
spyOn(mockUserService, 'login');
tcb
.createComponent(LoginComponent)
.then((fixture: ComponentFixture<LoginComponent>) => {
tick();
fixture.detectChanges();
expect(mockUserService.login).toHaveBeenCalled();
});
}))
);
});
**