@@ -644,31 +644,14 @@ describe('SongService', () => {
644644
645645 it ( 'should throw an error if song is not found' , async ( ) => {
646646 const publicId = 'test-id' ;
647- const user : UserDocument = { _id : 'test-user-id' } as UserDocument ;
648647
649- const mockFindOne = {
650- populate : jest . fn ( ) . mockResolvedValue ( null ) ,
651- } ;
652-
653- jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( mockFindOne as any ) ;
648+ const user : UserDocument = {
649+ _id : 'test-user-id' ,
650+ } as unknown as UserDocument ;
654651
655- await expect ( service . getSong ( publicId , user ) ) . rejects . toThrow (
656- HttpException ,
657- ) ;
658- } ) ;
652+ const mockFindOne = null ;
659653
660- it ( 'should throw an error if song is private and user is unauthorized' , async ( ) => {
661- const publicId = 'test-id' ;
662- const user : UserDocument = { _id : 'test-user-id' } as UserDocument ;
663-
664- const songEntity = {
665- visibility : 'private' ,
666- uploader : 'different-user-id' ,
667- } ;
668-
669- jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( {
670- populate : jest . fn ( ) . mockResolvedValue ( songEntity ) ,
671- } as any ) ;
654+ jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( mockFindOne as any ) ;
672655
673656 await expect ( service . getSong ( publicId , user ) ) . rejects . toThrow (
674657 HttpException ,
@@ -680,30 +663,24 @@ describe('SongService', () => {
680663 const user : UserDocument = { _id : 'test-user-id' } as UserDocument ;
681664
682665 const songEntity = {
666+ publicId : 'test-public-id' ,
683667 visibility : 'private' ,
684668 uploader : 'different-user-id' ,
685669 } ;
686670
687- jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( {
688- populate : jest . fn ( ) . mockResolvedValue ( songEntity ) ,
689- } as any ) ;
671+ jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( songEntity as any ) ;
690672
691- await expect ( service . getSong ( publicId , user ) ) . rejects . toThrow (
692- HttpException ,
693- ) ;
673+ expect ( service . getSong ( publicId , user ) ) . rejects . toThrow ( HttpException ) ;
694674 } ) ;
695675
696676 it ( 'should throw an error if song is private and user is not logged in' , async ( ) => {
697677 const publicId = 'test-id' ;
698678 const user : UserDocument = null as any ;
699679
700- const songEntity = {
701- visibility : 'private' ,
702- uploader : 'different-user-id' ,
703- } ;
704-
705680 jest . spyOn ( songModel , 'findOne' ) . mockReturnValue ( {
706- populate : jest . fn ( ) . mockResolvedValue ( songEntity ) ,
681+ populate : jest . fn ( ) . mockImplementationOnce ( ( ) => {
682+ throw new HttpException ( 'Song not found' , 404 ) ;
683+ } ) ,
707684 } as any ) ;
708685
709686 await expect ( service . getSong ( publicId , user ) ) . rejects . toThrow (
0 commit comments