|
1 | 1 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
2 | 2 |
|
3 | 3 | import { LoginComponent } from './login.component';
|
| 4 | +import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 5 | +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 | +import { |
| 9 | + GoogleSigninButtonModule, |
| 10 | + SocialAuthService, |
| 11 | + SocialLoginModule, |
| 12 | +} from '@abacritt/angularx-social-login'; |
| 13 | +import { Router } from '@angular/router'; |
4 | 14 |
|
5 | 15 | describe('LoginComponent', () => {
|
6 | 16 | let component: LoginComponent;
|
7 | 17 | let fixture: ComponentFixture<LoginComponent>;
|
| 18 | + let socialAuthService: jasmine.SpyObj<SocialAuthService>; |
| 19 | + let authService: jasmine.SpyObj<AuthService>; |
| 20 | + let router: jasmine.SpyObj<Router>; |
8 | 21 |
|
9 | 22 | beforeEach(async () => {
|
10 | 23 | await TestBed.configureTestingModule({
|
11 | | - declarations: [ LoginComponent ] |
12 | | - }) |
13 | | - .compileComponents(); |
| 24 | + declarations: [LoginComponent], |
| 25 | + imports: [ |
| 26 | + SocialLoginModule, |
| 27 | + GoogleSigninButtonModule, |
| 28 | + HttpClientTestingModule, |
| 29 | + MaterialModule, |
| 30 | + ], |
| 31 | + providers: [ |
| 32 | + HttpClientTestingModule, |
| 33 | + AuthService, |
| 34 | + SocialAuthServiceConfigMock, |
| 35 | + ], |
| 36 | + }).compileComponents(); |
14 | 37 |
|
15 | 38 | fixture = TestBed.createComponent(LoginComponent);
|
16 | 39 | 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>; |
17 | 46 | fixture.detectChanges();
|
18 | 47 | });
|
19 | 48 |
|
20 | 49 | it('should create', () => {
|
21 | 50 | expect(component).toBeTruthy();
|
22 | 51 | });
|
| 52 | + |
| 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']); |
| 59 | + }); |
| 60 | + |
| 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']); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should navigate to /users when user is already authenticated', () => { |
| 71 | + const routingSpy = spyOn(router, 'navigate'); |
| 72 | + spyOn(authService, 'isAuthenticated').and.returnValue(true); |
| 73 | + |
| 74 | + component.checkIsLogin(); |
| 75 | + expect(routingSpy).toHaveBeenCalledWith(['/users']); |
| 76 | + }); |
23 | 77 | });
|
0 commit comments