Skip to content

Commit 71a4484

Browse files
committed
Add authService to login component
1 parent 17effdf commit 71a4484

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

‎src/app/login/auth.service.spec.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import{AuthService}from'./auth.service';
2+
3+
describe('AuthService',()=>{});

‎src/app/login/auth.service.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import{Injectable}from'@angular/core';
2+
import{Observable}from'rxjs';
3+
4+
@Injectable()
5+
exportclassAuthService{
6+
constructor(){}
7+
8+
login(email: string,password: string): Observable<string>{
9+
thrownewError('not implemented');
10+
}
11+
}

‎src/app/login/login.component.spec.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import{async,ComponentFixture,TestBed}from'@angular/core/testing';
2+
import{ReactiveFormsModule}from'@angular/forms';
3+
import{of}from'rxjs';
24

35
import{LoginComponent}from'./login.component';
4-
import{ReactiveFormsModule}from'@angular/forms';
6+
import{AuthService}from'./auth.service';
57

68
describe('LoginComponent',()=>{
79
letcomponent: LoginComponent;
810
letfixture: ComponentFixture<LoginComponent>;
11+
constauthServiceStub: jasmine.SpyObj<AuthService>=jasmine.createSpyObj(
12+
'authService',
13+
['login']
14+
);
915

1016
beforeEach(async(()=>{
1117
TestBed.configureTestingModule({
1218
declarations: [LoginComponent],
13-
imports: [ReactiveFormsModule]
19+
imports: [ReactiveFormsModule],
20+
providers: [
21+
{
22+
provide: AuthService,
23+
useValue: authServiceStub
24+
}
25+
]
1426
}).compileComponents();
1527
}));
1628

@@ -96,4 +108,20 @@ describe('LoginComponent', () =>{
96108
'Please enter a valid password.'
97109
);
98110
});
111+
112+
it('should invoke auth service when form is valid',()=>{
113+
constemail=component.form.controls.email;
114+
email.setValue('[email protected]');
115+
constpassword=component.form.controls.password;
116+
password.setValue('123456');
117+
authServiceStub.login.and.returnValue(of());
118+
119+
fixture.nativeElement.querySelector('button').click();
120+
121+
expect(authServiceStub.login.calls.any()).toBeTruthy();
122+
expect(authServiceStub.login).toHaveBeenCalledWith(
123+
email.value,
124+
password.value
125+
);
126+
});
99127
});

‎src/app/login/login.component.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import{Component,OnInit}from'@angular/core';
22
import{FormGroup,FormBuilder,Validators}from'@angular/forms';
3+
import{AuthService}from'./auth.service';
34

45
@Component({
56
selector: 'app-login',
@@ -9,7 +10,10 @@ export class LoginComponent implements OnInit{
910
form: FormGroup;
1011
submitted=false;
1112

12-
constructor(privateformBuilder: FormBuilder){}
13+
constructor(
14+
privateformBuilder: FormBuilder,
15+
privateauthService: AuthService
16+
){}
1317

1418
ngOnInit(){
1519
this.form=this.formBuilder.group({
@@ -20,5 +24,14 @@ export class LoginComponent implements OnInit{
2024

2125
onSubmit(){
2226
this.submitted=true;
27+
28+
if(this.form.valid){
29+
this.authService
30+
.login(this.form.value.email,this.form.value.password)
31+
.subscribe(
32+
res=>console.log(res),
33+
error=>console.log(error)
34+
);
35+
}
2336
}
2437
}

‎src/app/login/login.module.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import{CommonModule } from '@angular/common'
44
import{LoginRoutingModule}from'./login-routing.module';
55
import{LoginComponent}from'./login.component';
66
import{ReactiveFormsModule}from'@angular/forms';
7+
import{AuthService}from'./auth.service';
78

89
@NgModule({
910
declarations: [LoginComponent],
10-
imports: [CommonModule,LoginRoutingModule,ReactiveFormsModule]
11+
imports: [CommonModule,LoginRoutingModule,ReactiveFormsModule],
12+
providers: [AuthService]
1113
})
1214
exportclassLoginModule{}

0 commit comments

Comments
(0)