|
| 1 | +import { DOCUMENT } from '@angular/common'; |
| 2 | +import { HttpErrorResponse } from '@angular/common/http'; |
| 3 | +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; |
| 4 | +import { Router } from '@angular/router'; |
| 5 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 6 | +import { of, throwError } from 'rxjs'; |
| 7 | +import { Login } from './login'; |
| 8 | +import { AuthService } from '../../services/auth'; |
| 9 | + |
| 10 | +describe('Login', () => { |
| 11 | + let component: Login; |
| 12 | + let fixture: ComponentFixture<Login>; |
| 13 | + let mockAuthService: jasmine.SpyObj<AuthService>; |
| 14 | + let router: Router; |
| 15 | + let document: Document; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + mockAuthService = jasmine.createSpyObj<AuthService>('AuthService', ['login', 'signup']); |
| 19 | + |
| 20 | + spyOn(localStorage, 'getItem').and.returnValue(null); |
| 21 | + spyOn(localStorage, 'setItem'); |
| 22 | + spyOn(window, 'matchMedia').and.returnValue({ |
| 23 | + matches: false, |
| 24 | + media: '', |
| 25 | + onchange: null, |
| 26 | + addListener: () => {}, |
| 27 | + removeListener: () => {}, |
| 28 | + addEventListener: () => {}, |
| 29 | + removeEventListener: () => {}, |
| 30 | + dispatchEvent: () => false |
| 31 | + }); |
| 32 | + |
| 33 | + await TestBed.configureTestingModule({ |
| 34 | + imports: [Login, RouterTestingModule], |
| 35 | + providers: [ |
| 36 | + { provide: AuthService, useValue: mockAuthService } |
| 37 | + ] |
| 38 | + }).compileComponents(); |
| 39 | + |
| 40 | + fixture = TestBed.createComponent(Login); |
| 41 | + component = fixture.componentInstance; |
| 42 | + router = TestBed.inject(Router); |
| 43 | + document = TestBed.inject(DOCUMENT); |
| 44 | + fixture.detectChanges(); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + document.documentElement.classList.remove('dark'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('initializes in login mode by default', () => { |
| 52 | + expect(component.activeTab).toBe('login'); |
| 53 | + expect(component.isLoginVisible).toBeTrue(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('switches to the register tab and navigates', () => { |
| 57 | + spyOn(router, 'navigate'); |
| 58 | + |
| 59 | + component.switchTab('register'); |
| 60 | + |
| 61 | + expect(component.activeTab).toBe('register'); |
| 62 | + expect(router.navigate).toHaveBeenCalledWith(['/signup']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('toggles the theme and stores the selection', () => { |
| 66 | + component.toggleTheme(); |
| 67 | + |
| 68 | + expect(component.isDarkMode).toBeTrue(); |
| 69 | + expect(document.documentElement.classList.contains('dark')).toBeTrue(); |
| 70 | + expect(localStorage.setItem).toHaveBeenCalledWith('theme', 'dark'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('shows an error when login credentials are missing', () => { |
| 74 | + component.loginEmail = ' '; |
| 75 | + component.loginPassword = ''; |
| 76 | + |
| 77 | + component.handleLogin(); |
| 78 | + |
| 79 | + expect(component.loginError).toBe('Invalid credentials'); |
| 80 | + expect(mockAuthService.login).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('rejects login when the email format is invalid', () => { |
| 84 | + component.loginEmail = 'invalid-email'; |
| 85 | + component.loginPassword = 'secret'; |
| 86 | + |
| 87 | + component.handleLogin(); |
| 88 | + |
| 89 | + expect(component.loginError).toBe('Enter a valid email address'); |
| 90 | + expect(mockAuthService.login).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('shows a success message and redirects after login', fakeAsync(() => { |
| 94 | + spyOn(router, 'navigate'); |
| 95 | + mockAuthService.login.and.returnValue(of({ token: 'token-1' })); |
| 96 | + component.loginEmail = 'user@example.com'; |
| 97 | + component.loginPassword = 'secret'; |
| 98 | + |
| 99 | + component.handleLogin(); |
| 100 | + tick(1200); |
| 101 | + |
| 102 | + expect(mockAuthService.login).toHaveBeenCalledWith('user@example.com', 'secret'); |
| 103 | + expect(component.showSuccess).toBeTrue(); |
| 104 | + expect(component.successTitle).toBe('Welcome back'); |
| 105 | + expect(router.navigate).toHaveBeenCalledWith(['/dashboard']); |
| 106 | + })); |
| 107 | + |
| 108 | + it('maps a 401 login response to invalid credentials', () => { |
| 109 | + mockAuthService.login.and.returnValue( |
| 110 | + throwError(() => new HttpErrorResponse({ status: 401, error: 'invalid credentials' })) |
| 111 | + ); |
| 112 | + component.loginEmail = 'user@example.com'; |
| 113 | + component.loginPassword = 'wrong'; |
| 114 | + |
| 115 | + component.handleLogin(); |
| 116 | + |
| 117 | + expect(component.loginError).toBe('Invalid credentials'); |
| 118 | + expect(component.isLoginSubmitting).toBeFalse(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('prevents register submission when fields are missing', () => { |
| 122 | + component.regEmail = ''; |
| 123 | + component.regPassword = ''; |
| 124 | + |
| 125 | + component.handleRegister(); |
| 126 | + |
| 127 | + expect(component.registerError).toBe('Invalid credentials'); |
| 128 | + expect(mockAuthService.signup).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('rejects registration when the email format is invalid', () => { |
| 132 | + component.activeTab = 'register'; |
| 133 | + component.regEmail = 'invalid-email'; |
| 134 | + component.regPassword = 'secret'; |
| 135 | + |
| 136 | + component.handleRegister(); |
| 137 | + |
| 138 | + expect(component.registerError).toBe('Enter a valid email address'); |
| 139 | + expect(mockAuthService.signup).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('disables registration when the email format is invalid', () => { |
| 143 | + component.regEmail = 'invalid-email'; |
| 144 | + component.regPassword = 'secret'; |
| 145 | + |
| 146 | + expect(component.isRegisterDisabled).toBeTrue(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('shows a duplicate email message when signup returns a conflict', () => { |
| 150 | + mockAuthService.signup.and.returnValue( |
| 151 | + throwError(() => new HttpErrorResponse({ status: 409, error: 'already exists' })) |
| 152 | + ); |
| 153 | + component.activeTab = 'register'; |
| 154 | + component.regEmail = 'user@example.com'; |
| 155 | + component.regPassword = 'secret'; |
| 156 | + |
| 157 | + component.handleRegister(); |
| 158 | + |
| 159 | + expect(component.registerError).toBe('Email already exists'); |
| 160 | + expect(component.activeTab).toBe('register'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('returns to login after a successful registration', fakeAsync(() => { |
| 164 | + spyOn(router, 'navigate'); |
| 165 | + mockAuthService.signup.and.returnValue(of(void 0)); |
| 166 | + component.activeTab = 'register'; |
| 167 | + component.regEmail = 'new@example.com'; |
| 168 | + component.regPassword = 'secret'; |
| 169 | + |
| 170 | + component.handleRegister(); |
| 171 | + tick(1500); |
| 172 | + |
| 173 | + expect(component.loginEmail).toBe('new@example.com'); |
| 174 | + expect(component.activeTab).toBe('login'); |
| 175 | + expect(router.navigate).toHaveBeenCalledWith(['/login']); |
| 176 | + })); |
| 177 | + |
| 178 | + it('rejects forgot-password submission when the email format is invalid', () => { |
| 179 | + component.showForgotPassword(); |
| 180 | + component.forgotEmail = 'invalid-email'; |
| 181 | + |
| 182 | + component.handleForgot(); |
| 183 | + |
| 184 | + expect(component.forgotError).toBe('Enter a valid email address'); |
| 185 | + expect(component.isForgotSubmitting).toBeFalse(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments