Skip to content

Commit b89a87f

Browse files
committed
test: enhance SongService tests to validate featured songs retrieval and handle empty results
1 parent 141ac08 commit b89a87f

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

apps/backend/src/song/song.service.spec.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { UserDocument } from '@nbw/database';
22
import {
3+
FeaturedSongsDto,
34
SongDocument,
45
Song as SongEntity,
56
SongPreviewDto,
@@ -1052,25 +1053,65 @@ describe('SongService', () => {
10521053
it('should return featured songs', async () => {
10531054
const songWithUser: SongWithUser = {
10541055
title: 'Test Song',
1056+
publicId: 'test-id',
10551057
uploader: { username: 'testuser', profileImage: 'testimage' },
1058+
description: 'Test Description',
1059+
originalAuthor: 'Test Author',
10561060
stats: {
10571061
duration: 100,
10581062
noteCount: 100,
10591063
},
1064+
thumbnailUrl: 'test-thumbnail-url',
1065+
createdAt: new Date(),
1066+
updatedAt: new Date(),
1067+
playCount: 0,
1068+
visibility: 'public',
10601069
} as any;
10611070

10621071
jest
1063-
.spyOn(songService, 'getSongsForTimespan')
1072+
.spyOn(service, 'getSongsForTimespan')
10641073
.mockResolvedValue([songWithUser]);
10651074

10661075
jest
1067-
.spyOn(songService, 'getSongsBeforeTimespan')
1068-
.mockResolvedValue([songWithUser]);
1076+
.spyOn(service, 'getSongsBeforeTimespan')
1077+
.mockResolvedValue([]);
1078+
1079+
const result = await service.getFeaturedSongs();
1080+
1081+
expect(service.getSongsForTimespan).toHaveBeenCalledTimes(6); // Called for each timespan
1082+
expect(result).toBeInstanceOf(Object);
1083+
expect(result).toHaveProperty('hour');
1084+
expect(result).toHaveProperty('day');
1085+
expect(result).toHaveProperty('week');
1086+
expect(result).toHaveProperty('month');
1087+
expect(result).toHaveProperty('year');
1088+
expect(result).toHaveProperty('all');
1089+
expect(Array.isArray(result.hour)).toBe(true);
1090+
expect(Array.isArray(result.day)).toBe(true);
1091+
expect(Array.isArray(result.week)).toBe(true);
1092+
expect(Array.isArray(result.month)).toBe(true);
1093+
expect(Array.isArray(result.year)).toBe(true);
1094+
expect(Array.isArray(result.all)).toBe(true);
1095+
});
10691096

1070-
await service.getFeaturedSongs();
1097+
it('should handle empty results gracefully', async () => {
1098+
jest
1099+
.spyOn(service, 'getSongsForTimespan')
1100+
.mockResolvedValue([]);
10711101

1072-
expect(songService.getSongsForTimespan).toHaveBeenCalled();
1073-
expect(songService.getSongsBeforeTimespan).toHaveBeenCalled();
1102+
jest
1103+
.spyOn(service, 'getSongsBeforeTimespan')
1104+
.mockResolvedValue([]);
1105+
1106+
const result = await service.getFeaturedSongs();
1107+
1108+
expect(result).toBeInstanceOf(Object);
1109+
expect(result.hour).toEqual([]);
1110+
expect(result.day).toEqual([]);
1111+
expect(result.week).toEqual([]);
1112+
expect(result.month).toEqual([]);
1113+
expect(result.year).toEqual([]);
1114+
expect(result.all).toEqual([]);
10741115
});
10751116
});
10761117
});

0 commit comments

Comments
 (0)