|
1 | 1 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
2 | | - |
| 2 | +import{Router}from'@angular/router'; |
3 | 3 | import { LoginComponent } from './login.component';
|
4 | | -import { HttpClientTestingModule } from '@angular/common/http/testing'; |
5 | 4 | import { AuthService } from '../auth.service';
|
6 | | -import { SocialAuthServiceConfigMock } from 'src/app/testing/social-auth.mock'; |
7 | | -import { MaterialModule } from 'src/app/shared/material.module'; |
8 | 5 | import {
|
9 | 6 | GoogleSigninButtonModule,
|
10 | | - SocialAuthService, |
11 | 7 | SocialLoginModule,
|
12 | 8 | } 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'; |
14 | 14 |
|
15 | 15 | describe('LoginComponent', () => {
|
16 | 16 | let component: LoginComponent;
|
17 | 17 | 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; |
21 | 19 |
|
22 | | - beforeEach(async () => { |
23 | | - await TestBed.configureTestingModule({ |
24 | | - declarations: [LoginComponent], |
| 20 | + beforeEach(() => { |
| 21 | + TestBed.configureTestingModule({ |
25 | 22 | imports: [
|
| 23 | + MaterialModule, |
26 | 24 | SocialLoginModule,
|
27 | 25 | GoogleSigninButtonModule,
|
| 26 | + RouterTestingModule, |
28 | 27 | HttpClientTestingModule,
|
29 | | - MaterialModule, |
30 | | - ], |
31 | | - providers: [ |
32 | | - HttpClientTestingModule, |
33 | | - AuthService, |
34 | | - SocialAuthServiceConfigMock, |
35 | 28 | ],
|
| 29 | + declarations: [LoginComponent], |
| 30 | + providers: [AuthService, SocialAuthServiceConfigMock], |
36 | 31 | }).compileComponents();
|
37 | 32 |
|
38 | 33 | fixture = TestBed.createComponent(LoginComponent);
|
39 | 34 | 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); |
47 | 36 | });
|
48 | 37 |
|
49 | 38 | it('should create', () => {
|
50 | 39 | expect(component).toBeTruthy();
|
51 | 40 | });
|
52 | 41 |
|
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']); |
59 | 46 | });
|
60 | 47 |
|
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']); |
68 | 53 | });
|
69 | 54 |
|
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'); |
72 | 57 | 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']); |
76 | 61 | });
|
77 | 62 | });
|
0 commit comments