-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66076a4
commit 0c1a641
Showing
2 changed files
with
298 additions
and
38 deletions.
There are no files selected for viewing
159 changes: 140 additions & 19 deletions
159
Frontend/src/app/components/templates/desktop/desk-register/desk-register.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,148 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { DeskRegisterComponent } from './desk-register.component'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
import { AuthService } from '../../../../services/auth.service'; | ||
import { Router } from '@angular/router'; | ||
import { of, throwError } from 'rxjs'; | ||
import { ToastComponent } from '../../../../components/organisms/toast/toast.component'; | ||
|
||
describe('DeskRegisterComponent', () => { | ||
let component: DeskRegisterComponent; | ||
let fixture: ComponentFixture<DeskRegisterComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [DeskRegisterComponent], | ||
providers: [ | ||
provideHttpClient(), | ||
] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(DeskRegisterComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
let authServiceMock: jest.Mocked<AuthService>; | ||
let routerMock: jest.Mocked<Router>; | ||
|
||
beforeEach(() => { | ||
// Mocking AuthService and Router | ||
authServiceMock = { | ||
signUp: jest.fn(), | ||
} as unknown as jest.Mocked<AuthService>; | ||
|
||
routerMock = { | ||
navigate: jest.fn(), | ||
} as unknown as jest.Mocked<Router>; | ||
|
||
// Creating component instance with mocks | ||
component = new DeskRegisterComponent(authServiceMock, routerMock); | ||
component.toastComponent = { showToast: jest.fn() } as unknown as ToastComponent; // Mocking ToastComponent | ||
|
||
jest.clearAllMocks(); // Resetting mocks before each test | ||
}); | ||
|
||
describe('register', () => { | ||
it('should show an alert if fields are empty', async () => { | ||
window.alert = jest.fn(); // Mocking global alert | ||
component.username = ''; | ||
component.email = ''; | ||
component.password = ''; | ||
|
||
await component.register(); | ||
|
||
expect(window.alert).toHaveBeenCalledWith('Please fill in all fields'); | ||
}); | ||
|
||
it('should call authService.signUp and navigate to home on success', async () => { | ||
component.username = 'testuser'; | ||
component.email = 'test@example.com'; | ||
component.password = 'Test123!'; | ||
authServiceMock.signUp.mockReturnValue(of({})); | ||
|
||
await component.register(); | ||
|
||
expect(authServiceMock.signUp).toHaveBeenCalledWith('test@example.com', 'Test123!', { | ||
username: 'testuser', | ||
name: 'testuser', | ||
}); | ||
expect(routerMock.navigate).toHaveBeenCalledWith(['/home']); | ||
}); | ||
|
||
it('should call toastComponent.showToast on signUp error', async () => { | ||
component.username = 'testuser'; | ||
component.email = 'test@example.com'; | ||
component.password = 'Test123!'; | ||
authServiceMock.signUp.mockReturnValue(throwError(() => new Error('Sign up error'))); | ||
|
||
await component.register(); | ||
|
||
expect(authServiceMock.signUp).toHaveBeenCalledWith('test@example.com', 'Test123!', { | ||
username: 'testuser', | ||
name: 'testuser', | ||
}); | ||
expect(component.toastComponent.showToast).toHaveBeenCalledWith( | ||
'Ensure password contains at least one lower case letter, one capital letter, one number, and one symbol.', | ||
'error' | ||
); | ||
}); | ||
}); | ||
|
||
describe('navigation', () => { | ||
it('should navigate to the login page when navigateTologin is called', () => { | ||
component.navigateTologin(); | ||
expect(routerMock.navigate).toHaveBeenCalledWith(['/login']); | ||
}); | ||
}); | ||
|
||
describe('modal toggles', () => { | ||
it('should toggle showModal', () => { | ||
expect(component.showModal).toBe(false); | ||
component.toggleModal(); | ||
expect(component.showModal).toBe(true); | ||
component.toggleModal(); | ||
expect(component.showModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showAboutModal', () => { | ||
expect(component.showAboutModal).toBe(false); | ||
component.toggleAboutModal(); | ||
expect(component.showAboutModal).toBe(true); | ||
component.toggleAboutModal(); | ||
expect(component.showAboutModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showContactModal', () => { | ||
expect(component.showContactModal).toBe(false); | ||
component.toggleContactModal(); | ||
expect(component.showContactModal).toBe(true); | ||
component.toggleContactModal(); | ||
expect(component.showContactModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showPrivacyModal', () => { | ||
expect(component.showPrivacyModal).toBe(false); | ||
component.togglePrivacyModal(); | ||
expect(component.showPrivacyModal).toBe(true); | ||
component.togglePrivacyModal(); | ||
expect(component.showPrivacyModal).toBe(false); | ||
}); | ||
|
||
it('should close all modals when closeModal is called', () => { | ||
component.showModal = true; | ||
component.showAboutModal = true; | ||
component.showContactModal = true; | ||
component.showPrivacyModal = true; | ||
|
||
component.closeModal(); | ||
|
||
expect(component.showModal).toBe(false); | ||
expect(component.showAboutModal).toBe(false); | ||
expect(component.showContactModal).toBe(false); | ||
expect(component.showPrivacyModal).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
describe('spotify login', () => { | ||
it('should redirect to the correct URL when spotify is called', () => { | ||
const originalLocation = window.location; | ||
|
||
// Mocking window.location.href | ||
Object.defineProperty(window, 'location', { | ||
value: { href: '' }, | ||
writable: true, | ||
}); | ||
|
||
component.spotify(); | ||
|
||
expect(window.location.href).toBe('http://localhost:3000/api/auth/oauth-signin'); | ||
|
||
// Restore the original window.location after the test | ||
window.location = originalLocation; | ||
}); | ||
}); | ||
}); |
177 changes: 158 additions & 19 deletions
177
Frontend/src/app/components/templates/desktop/deskLogin/desk-login.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,166 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { DeskLoginComponent } from './desk-login.component'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
import { AuthService } from '../../../../services/auth.service'; | ||
import { Router } from '@angular/router'; | ||
import { ProviderService } from '../../../../services/provider.service'; | ||
import { YouTubeService } from '../../../../services/youtube.service'; | ||
import { of, throwError } from 'rxjs'; | ||
import { ToastComponent } from '../../../../components/organisms/toast/toast.component'; | ||
|
||
describe('DeskLoginComponent', () => { | ||
let component: DeskLoginComponent; | ||
let fixture: ComponentFixture<DeskLoginComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [DeskLoginComponent], | ||
providers: [ | ||
provideHttpClient() | ||
] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(DeskLoginComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
let authServiceMock: jest.Mocked<AuthService>; | ||
let routerMock: jest.Mocked<Router>; | ||
let providerServiceMock: jest.Mocked<ProviderService>; | ||
let youtubeServiceMock: jest.Mocked<YouTubeService>; | ||
|
||
beforeEach(() => { | ||
// Mocking dependencies | ||
authServiceMock = { | ||
signIn: jest.fn(), | ||
signInWithOAuth: jest.fn(), | ||
} as unknown as jest.Mocked<AuthService>; | ||
|
||
routerMock = { | ||
navigate: jest.fn(), | ||
} as unknown as jest.Mocked<Router>; | ||
|
||
providerServiceMock = { | ||
setProviderName: jest.fn(), | ||
} as unknown as jest.Mocked<ProviderService>; | ||
|
||
youtubeServiceMock = { | ||
init: jest.fn().mockResolvedValue(null), | ||
} as unknown as jest.Mocked<YouTubeService>; | ||
|
||
// Creating the component instance with mocks | ||
component = new DeskLoginComponent(authServiceMock, routerMock, providerServiceMock, youtubeServiceMock); | ||
component.toastComponent = { showToast: jest.fn() } as unknown as ToastComponent; // Mocking ToastComponent | ||
|
||
jest.clearAllMocks(); // Reset mocks before each test | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
describe('ngOnInit', () => { | ||
it('should initialize the component', () => { | ||
component.ngOnInit(); | ||
// There's no functionality in ngOnInit, just checking no errors are thrown | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('spotify', () => { | ||
it('should call authService.signInWithOAuth when spotify method is called', async () => { | ||
await component.spotify(); | ||
expect(authServiceMock.signInWithOAuth).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('navigateToRegister', () => { | ||
it('should navigate to the register page', () => { | ||
component.navigateToRegister(); | ||
expect(routerMock.navigate).toHaveBeenCalledWith(['/register']); | ||
}); | ||
}); | ||
|
||
describe('login', () => { | ||
beforeEach(() => { | ||
component.email = 'test@example.com'; | ||
component.password = 'password123'; | ||
}); | ||
|
||
it('should call providerService.setProviderName and authService.signIn', () => { | ||
authServiceMock.signIn.mockReturnValue(of({ user: true })); | ||
|
||
component.login(); | ||
|
||
expect(providerServiceMock.setProviderName).toHaveBeenCalledWith('email'); | ||
expect(authServiceMock.signIn).toHaveBeenCalledWith('test@example.com', 'password123'); | ||
}); | ||
|
||
it('should show success toast and navigate to home on successful login', async () => { | ||
authServiceMock.signIn.mockReturnValue(of({ user: true })); | ||
|
||
component.login(); | ||
|
||
expect(component.toastComponent.showToast).toHaveBeenCalledWith('User logged in successfully', 'success'); | ||
expect(localStorage.getItem('username')).toBe('test@example.com'); | ||
|
||
setTimeout(() => { | ||
expect(youtubeServiceMock.init).toHaveBeenCalled(); | ||
expect(routerMock.navigate).toHaveBeenCalledWith(['/home']); | ||
}, 1000); | ||
}); | ||
|
||
it('should show info toast on invalid login credentials', () => { | ||
authServiceMock.signIn.mockReturnValue(of({ user: false })); | ||
|
||
component.login(); | ||
|
||
expect(component.toastComponent.showToast).toHaveBeenCalledWith('Invalid username or password', 'info'); | ||
}); | ||
|
||
it('should show error toast on login failure', () => { | ||
authServiceMock.signIn.mockReturnValue(throwError(() => new Error('Login error'))); | ||
|
||
component.login(); | ||
|
||
expect(component.toastComponent.showToast).toHaveBeenCalledWith('There was an issue logging in', 'error'); | ||
}); | ||
}); | ||
|
||
describe('modal toggles', () => { | ||
it('should toggle showModal', () => { | ||
expect(component.showModal).toBe(false); | ||
component.toggleModal(); | ||
expect(component.showModal).toBe(true); | ||
component.toggleModal(); | ||
expect(component.showModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showAboutModal', () => { | ||
expect(component.showAboutModal).toBe(false); | ||
component.toggleAboutModal(); | ||
expect(component.showAboutModal).toBe(true); | ||
component.toggleAboutModal(); | ||
expect(component.showAboutModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showContactModal', () => { | ||
expect(component.showContactModal).toBe(false); | ||
component.toggleContactModal(); | ||
expect(component.showContactModal).toBe(true); | ||
component.toggleContactModal(); | ||
expect(component.showContactModal).toBe(false); | ||
}); | ||
|
||
it('should toggle showPrivacyModal', () => { | ||
expect(component.showPrivacyModal).toBe(false); | ||
component.togglePrivacyModal(); | ||
expect(component.showPrivacyModal).toBe(true); | ||
component.togglePrivacyModal(); | ||
expect(component.showPrivacyModal).toBe(false); | ||
}); | ||
|
||
it('should close all modals when closeModal is called', () => { | ||
component.showModal = true; | ||
component.showAboutModal = true; | ||
component.showContactModal = true; | ||
component.showPrivacyModal = true; | ||
|
||
component.closeModal(); | ||
|
||
expect(component.showModal).toBe(false); | ||
expect(component.showAboutModal).toBe(false); | ||
expect(component.showContactModal).toBe(false); | ||
expect(component.showPrivacyModal).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('google', () => { | ||
it('should initialize YouTube service and set provider to google', async () => { | ||
await component.google(); | ||
|
||
expect(youtubeServiceMock.init).toHaveBeenCalled(); | ||
expect(providerServiceMock.setProviderName).toHaveBeenCalledWith('google'); | ||
}); | ||
}); | ||
}); |