Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64d4a26

Browse files
Login component unit test case
1 parent a8a13a2 commit 64d4a26

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed
Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,62 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
2+
import{Router}from'@angular/router';
33
import { LoginComponent } from './login.component';
4-
import { HttpClientTestingModule } from '@angular/common/http/testing';
54
import { AuthService } from '../auth.service';
6-
import { SocialAuthServiceConfigMock } from 'src/app/testing/social-auth.mock';
7-
import { MaterialModule } from 'src/app/shared/material.module';
85
import {
96
GoogleSigninButtonModule,
10-
SocialAuthService,
117
SocialLoginModule,
128
} from '@abacritt/angularx-social-login';
13-
import { Router } from '@angular/router';
9+
import { MaterialModule } from 'src/app/shared/material.module';
10+
import { RouterTestingModule } from '@angular/router/testing';
11+
import { HttpClientTestingModule } from '@angular/common/http/testing';
12+
import { SocialAuthServiceConfigMock } from 'src/app/testing/social-auth.mock';
13+
import { of } from 'rxjs';
1414

1515
describe('LoginComponent', () => {
1616
let component: LoginComponent;
1717
let fixture: ComponentFixture<LoginComponent>;
18-
let socialAuthService: jasmine.SpyObj<SocialAuthService>;
19-
let authService: jasmine.SpyObj<AuthService>;
20-
let router: jasmine.SpyObj<Router>;
18+
let authService: AuthService;
2119

22-
beforeEach(async () => {
23-
await TestBed.configureTestingModule({
24-
declarations: [LoginComponent],
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
2522
imports: [
23+
MaterialModule,
2624
SocialLoginModule,
2725
GoogleSigninButtonModule,
26+
RouterTestingModule,
2827
HttpClientTestingModule,
29-
MaterialModule,
30-
],
31-
providers: [
32-
HttpClientTestingModule,
33-
AuthService,
34-
SocialAuthServiceConfigMock,
3528
],
29+
declarations: [LoginComponent],
30+
providers: [AuthService, SocialAuthServiceConfigMock],
3631
}).compileComponents();
3732

3833
fixture = TestBed.createComponent(LoginComponent);
3934
component = fixture.componentInstance;
40-
41-
socialAuthService = TestBed.inject(
42-
SocialAuthService
43-
) as jasmine.SpyObj<SocialAuthService>;
44-
authService = TestBed.inject(AuthService) as jasmine.SpyObj<AuthService>;
45-
router = TestBed.inject(Router) as jasmine.SpyObj<Router>;
46-
fixture.detectChanges();
35+
authService = TestBed.inject(AuthService);
4736
});
4837

4938
it('should create', () => {
5039
expect(component).toBeTruthy();
5140
});
5241

53-
it('should navigate to /users when user is logged in via social authentication', () => {
54-
const routingSpy = spyOn(router, 'navigate');
55-
const user = { idToken: 'some-id-token' };
56-
authService.login(user.idToken);
57-
component.checkIsLogin();
58-
expect(routingSpy).toHaveBeenCalledWith(['/users']);
42+
it('should navigate to "/users" when onLoginSuccess is called', () => {
43+
let navigateSpy = spyOn(component['router'], 'navigate');
44+
component.onLoginSuccess();
45+
expect(navigateSpy).toHaveBeenCalledWith(['/users']);
5946
});
6047

61-
it('should navigate to /users when user is logged in via regular authentication', () => {
62-
const routingSpy = spyOn(router, 'navigate');
63-
const user = { idToken: 'some-id-token' };
64-
authService.login(user.idToken);
65-
66-
component.checkIsLogin();
67-
expect(routingSpy).toHaveBeenCalledWith(['/users']);
48+
it('should call onLoginSuccess when authService.isAuthenticated() returns true', () => {
49+
let navigateSpy = spyOn(component['router'], 'navigate');
50+
spyOn(authService, 'isAuthenticated').and.returnValue(true);
51+
component.ngOnInit();
52+
expect(navigateSpy).toHaveBeenCalledWith(['/users']);
6853
});
6954

70-
it('should navigate to /users when user is already authenticated', () => {
71-
constroutingSpy = spyOn(router, 'navigate');
55+
it('should call onLoginSuccess when authService.isAuthenticated$ returns true', () => {
56+
letnavigateSpy = spyOn(component['router'], 'navigate');
7257
spyOn(authService, 'isAuthenticated').and.returnValue(true);
73-
74-
component.checkIsLogin();
75-
expect(routingSpy).toHaveBeenCalledWith(['/users']);
58+
authService['checkAuthenticationStatus']();
59+
component.ngOnInit();
60+
expect(navigateSpy).toHaveBeenCalledWith(['/users']);
7661
});
7762
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /