-
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
a6f5ed1
commit fc60df3
Showing
4 changed files
with
139 additions
and
158 deletions.
There are no files selected for viewing
50 changes: 34 additions & 16 deletions
50
Frontend/src/app/components/organisms/bottom-nav/bottom-nav.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
5 changes: 4 additions & 1 deletion
5
Frontend/src/app/components/templates/mobile/mobilehome/mobilehome.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
237 changes: 97 additions & 140 deletions
237
Frontend/src/app/pages/profile/profile.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,177 +1,134 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ProfileComponent } from './profile.component'; | ||
import { AuthService } from '../../services/auth.service'; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { of } from 'rxjs'; | ||
import { Router } from '@angular/router'; | ||
import { MatDialog, MatDialogRef } from '@angular/material/dialog'; | ||
import { MatDialog } from '@angular/material/dialog'; | ||
import { AuthService } from '../../services/auth.service'; | ||
import { ScreenSizeService } from '../../services/screen-size-service.service'; | ||
import { SpotifyService } from '../../services/spotify.service'; | ||
import { ProviderService } from '../../services/provider.service'; | ||
import { MoodService } from '../../services/mood-service.service'; | ||
import { of } from 'rxjs'; | ||
import { EditProfileModalComponent } from '../../components/organisms/edit-profile-modal/edit-profile-modal.component'; | ||
import { ProfileComponent } from './profile.component'; | ||
|
||
const authServiceMock = (): jest.Mocked<AuthService> => ({ | ||
currentUser: jest.fn().mockReturnValue(of({ | ||
user: { | ||
user_metadata: { name: 'mockname', username: 'Test User', picture: 'path/to/picture' } | ||
} | ||
})) | ||
}) as any; | ||
|
||
// Mock Router | ||
const routerMock = (): jest.Mocked<Router> => ({ | ||
navigate: jest.fn() | ||
}) as any; | ||
|
||
// Mock MatDialog | ||
const matDialogMock = (): jest.Mocked<MatDialog> => ({ | ||
open: jest.fn() | ||
}) as any; | ||
|
||
// Mock ScreenSizeService | ||
const screenSizeServiceMock = (): jest.Mocked<ScreenSizeService> => ({ | ||
screenSize$: of('large') | ||
}) as any; | ||
|
||
// Mock SpotifyService | ||
const spotifyServiceMock = (): jest.Mocked<SpotifyService> => ({ | ||
getTopTracks: jest.fn().mockResolvedValue([ | ||
{ id: '1', text: 'Track 1', albumName: 'Album 1', imageUrl: '', secondaryText: '', previewUrl: '', spotifyUrl: '', explicit: false } | ||
]), | ||
getTopArtists: jest.fn().mockResolvedValue([ | ||
{ id: '1', name: 'Artist 1', imageUrl: '', spotifyUrl: '' } | ||
]), | ||
playTrackById: jest.fn() | ||
}) as any; | ||
|
||
// Mock ProviderService | ||
const providerServiceMock = (): jest.Mocked<ProviderService> => ({ | ||
getProviderName: jest.fn().mockReturnValue('spotify') | ||
}) as any; | ||
|
||
// Mock MoodService | ||
const moodServiceMock = (): jest.Mocked<MoodService> => ({ | ||
getCurrentMood: jest.fn().mockReturnValue('happy'), | ||
getComponentMoodClasses: jest.fn().mockReturnValue({ | ||
happy: 'happy-class', | ||
sad: 'sad-class' | ||
}) | ||
}) as any; | ||
|
||
describe('ProfileComponent', () => { | ||
let component: ProfileComponent; | ||
let fixture: ComponentFixture<ProfileComponent>; | ||
let authServiceMock: any; | ||
let spotifyServiceMock: any; | ||
let providerServiceMock: any; | ||
let routerMock: any; | ||
let dialogMock: any; | ||
let moodServiceMock: any; | ||
let screenSizeServiceMock: any; | ||
let dialogRefMock: Partial<MatDialogRef<EditProfileModalComponent, any>>; | ||
|
||
let authService: jest.Mocked<AuthService>; | ||
let router: jest.Mocked<Router>; | ||
let matDialog: jest.Mocked<MatDialog>; | ||
let screenSizeService: jest.Mocked<ScreenSizeService>; | ||
let spotifyService: jest.Mocked<SpotifyService>; | ||
let providerService: jest.Mocked<ProviderService>; | ||
let moodService: jest.Mocked<MoodService>; | ||
|
||
beforeEach(async () => { | ||
const dialogRefMock: Partial<MatDialogRef<EditProfileModalComponent, any>> = { | ||
afterClosed: jest.fn().mockReturnValue(of(true)), // simulate afterClosed() returning a resolved value | ||
close: jest.fn(), // mock the close method | ||
componentInstance: {} as EditProfileModalComponent // mock the component instance if needed | ||
}; | ||
authServiceMock = { | ||
currentUser: jest.fn(() => of({ user: { user_metadata: { name: 'Test User', picture: 'test-pic.jpg' }}})) | ||
}; | ||
spotifyServiceMock = { | ||
getTopTracks: jest.fn(() => Promise.resolve([{ id: '1', text: 'Track 1', albumName: 'Album 1', imageUrl: '', secondaryText: '', previewUrl: '', spotifyUrl: '', explicit: false }])), | ||
getTopArtists: jest.fn(() => Promise.resolve([{ id: '1', name: 'Artist 1', imageUrl: '', spotifyUrl: '' }])), | ||
playTrackById: jest.fn() | ||
}; | ||
providerServiceMock = { | ||
getProviderName: jest.fn(() => 'spotify') | ||
}; | ||
routerMock = { navigate: jest.fn() }; | ||
dialogMock = { | ||
open: jest.fn().mockReturnValue(dialogRefMock) | ||
}; | ||
moodServiceMock = { getCurrentMood: jest.fn(() => 'happy'), getComponentMoodClasses: jest.fn(() => ({})) }; | ||
screenSizeServiceMock = { screenSize$: of('large') }; | ||
authService = authServiceMock(); | ||
router = routerMock(); | ||
matDialog = matDialogMock(); | ||
screenSizeService = screenSizeServiceMock(); | ||
spotifyService = spotifyServiceMock(); | ||
providerService = providerServiceMock(); | ||
moodService = moodServiceMock(); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [ProfileComponent], | ||
providers: [ | ||
{ provide: AuthService, useValue: authServiceMock }, | ||
{ provide: SpotifyService, useValue: spotifyServiceMock }, | ||
{ provide: ProviderService, useValue: providerServiceMock }, | ||
{ provide: Router, useValue: routerMock }, | ||
{ provide: MatDialog, useValue: dialogMock }, | ||
{ provide: MoodService, useValue: moodServiceMock }, | ||
{ provide: ScreenSizeService, useValue: screenSizeServiceMock } | ||
{ provide: AuthService, useValue: authService }, | ||
{ provide: Router, useValue: router }, | ||
{ provide: MatDialog, useValue: matDialog }, | ||
{ provide: ScreenSizeService, useValue: screenSizeService }, | ||
{ provide: SpotifyService, useValue: spotifyService }, | ||
{ provide: ProviderService, useValue: providerService }, | ||
{ provide: MoodService, useValue: moodService }, | ||
] | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ProfileComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create the component', () => { | ||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should set initial mood and classes from MoodService', () => { | ||
expect(moodServiceMock.getCurrentMood).toHaveBeenCalled(); | ||
expect(component.currentMood).toBe('happy'); | ||
expect(component.moodComponentClasses).toEqual({}); | ||
}); | ||
|
||
it('should get the provider name and user info on AfterViewInit', () => { | ||
it('should fetch user data on ngAfterViewInit if provider is Spotify', () => { | ||
component.ngAfterViewInit(); | ||
|
||
expect(providerServiceMock.getProviderName).toHaveBeenCalled(); | ||
expect(authServiceMock.currentUser).toHaveBeenCalled(); | ||
expect(component.username).toBe('Test User'); | ||
expect(component.imgpath).toBe('test-pic.jpg'); | ||
}); | ||
|
||
it('should load top tracks and top artists on AfterViewInit', async () => { | ||
await component.ngAfterViewInit(); | ||
|
||
expect(spotifyServiceMock.getTopTracks).toHaveBeenCalled(); | ||
expect(spotifyServiceMock.getTopArtists).toHaveBeenCalled(); | ||
expect(component.topTracks.length).toBe(1); | ||
expect(component.topArtists.length).toBe(1); | ||
}); | ||
|
||
it('should update screen size on subscription in ngOnInit', () => { | ||
component.ngOnInit(); | ||
|
||
expect(screenSizeServiceMock.screenSize$).toBeTruthy(); | ||
expect(component.screenSize).toBe('large'); | ||
expect(authService.currentUser).toHaveBeenCalled(); | ||
expect(component.username).toEqual('mockname'); | ||
expect(component.imgpath).toEqual('path/to/picture'); | ||
expect(spotifyService.getTopTracks).toHaveBeenCalled(); | ||
expect(spotifyService.getTopArtists).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should open the edit profile dialog when openDialog is called', () => { | ||
component['dialog'] = { | ||
open: jest.fn().mockReturnValue({ | ||
afterClosed: jest.fn().mockReturnValue(of({})), // or another observable if required | ||
}), | ||
} as unknown as MatDialog; | ||
component.openDialog(); | ||
|
||
expect(component['dialog'].open).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should save image path from localStorage if present', () => { | ||
localStorage.setItem('path', 'new-path.jpg'); | ||
component.save(); | ||
|
||
expect(component.imgpath).toBe('new-path.jpg'); | ||
}); | ||
|
||
it('should refresh user data if provider is Spotify', () => { | ||
providerServiceMock.getProviderName.mockResolvedValue('spotify'); | ||
component.refresh(); | ||
|
||
expect(providerServiceMock.getProviderName).toHaveBeenCalled(); | ||
expect(authServiceMock.currentUser).toHaveBeenCalled(); | ||
expect(component.username).toBe('Test User'); | ||
}); | ||
|
||
it('should navigate to settings page when settings is called', () => { | ||
it('should navigate to /settings when settings is called', () => { | ||
component.settings(); | ||
|
||
expect(routerMock.navigate).toHaveBeenCalledWith(['/settings']); | ||
expect(router.navigate).toHaveBeenCalledWith(['/settings']); | ||
}); | ||
|
||
it('should call SpotifyService to play a track when playTrack is called', () => { | ||
component.playTrack('1'); | ||
|
||
expect(spotifyServiceMock.playTrackById).toHaveBeenCalledWith('1'); | ||
it('should play the track by id', () => { | ||
const trackId = '1'; | ||
component.playTrack(trackId); | ||
expect(spotifyService.playTrackById).toHaveBeenCalledWith(trackId); | ||
}); | ||
|
||
it('should set the username if provider is spotify', (done) => { | ||
// Arrange | ||
const mockResponse = { | ||
user: { | ||
user_metadata: { | ||
username: 'test_username' | ||
} | ||
} | ||
}; | ||
|
||
jest.spyOn(providerServiceMock, 'getProviderName').mockReturnValue('spotify'); | ||
jest.spyOn(authServiceMock, 'currentUser').mockReturnValue(of(mockResponse)); // Mock the Observable response | ||
|
||
// Act | ||
component.refresh(); | ||
|
||
// Assert | ||
authServiceMock.currentUser().subscribe(() => { | ||
expect(component.username).toBe('test_username'); | ||
done(); // Make sure the async test completes | ||
}); | ||
it('should subscribe to screenSize on ngOnInit', () => { | ||
component.ngOnInit(); | ||
expect(component.screenSize).toBe('large'); | ||
}); | ||
/* | ||
it('should not set the username if provider is not spotify', () => { | ||
// Arrange | ||
providerServiceMock.getProviderName.mockImplementation(() => 'pootify'); | ||
|
||
// Act | ||
it('should update username on refresh if provider is Spotify', fakeAsync(() => { | ||
providerService.getProviderName.mockReturnValue('spotify'); | ||
component.refresh(); | ||
// Assert | ||
//expect(authServiceMock.currentUser).not.toHaveBeenCalled(); // currentUser() shouldn't be called | ||
expect(component.username).toBeUndefined(); // Username shouldn't be set | ||
});*/ | ||
tick(); | ||
expect(authService.currentUser).toHaveBeenCalled(); | ||
expect(component.username).toEqual('Test User'); | ||
})); | ||
}); |
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