import { match , restore , SinonStub , stub } from 'sinon' ; import { ITrackDownloadResult } from 'src/ts/download/download-result' ; import { PlaylistDownloadService } from 'src/ts/download/playlist-download-service' ; import { IPlaylistInfo , ITrackInfo , IUserInfo , ResourceType } from 'src/ts/download/resource/resource-info' ; import { TrackDownloadService } from 'src/ts/download/track-download-service' ; import { configureChai } from 'test/ts/test-initializers' ; const expect = configureChai ( ) ; describe ( 'playlist download service' , ( ) => { const fixture = PlaylistDownloadService ; const trackOneInfo = { title : 'foo' } as ITrackInfo ; const trackTwoInfo = { title : 'bar' } as ITrackInfo ; const playlistInfo = { kind : ResourceType . Playlist , permalink_url : 'permalinkUrl' , title : 'some*playlist?with>special\\characters' , tracks : [ trackOneInfo , trackTwoInfo ] , user : { username : 'some<username|with?special/characters' , } as IUserInfo , } ; const trackOneDlResult = { trackInfo : trackOneInfo } as ITrackDownloadResult ; const trackTwoDlResult = { trackInfo : trackTwoInfo } as ITrackDownloadResult ; let stubDownloadTrack ; beforeEach ( ( ) => { stubDownloadTrack = stub ( TrackDownloadService , 'download' ) ; stubDownloadTrack . withArgs ( trackOneInfo , match . any ) . returns ( trackOneDlResult ) ; stubDownloadTrack . withArgs ( trackTwoInfo , match . any ) . returns ( trackTwoDlResult ) ; } ) ; afterEach ( ( ) => { restore ( ) ; } ) ; describe ( 'downloading a playlist' , ( ) => { it ( 'should download each track in the playlist' , ( ) => { fixture . download ( playlistInfo ) ; expect ( stubDownloadTrack ) . to . have . been . calledTwice ; expect ( stubDownloadTrack . getCall ( 0 ) . args [ 0 ] ) . to . be . equal ( trackOneInfo ) ; expect ( stubDownloadTrack . getCall ( 1 ) . args [ 0 ] ) . to . be . equal ( trackTwoInfo ) ; } ) ; it ( 'should download to the correct download location' , ( ) => { const expectedDlLocation = 'some_username_with_special_characters - some_playlist_with_special_characters' ; fixture . download ( playlistInfo ) ; expect ( stubDownloadTrack ) . to . have . been . calledTwice ; stubDownloadTrack . getCalls ( ) . forEach ( ( spyCall ) => expect ( spyCall . args [ 1 ] ) . to . be . equal ( expectedDlLocation ) ) ; } ) ; it ( 'should return a playlist download result' , ( ) => { const actual = fixture . download ( playlistInfo ) ; expect ( actual ) . to . be . deep . equal ( { kind : ResourceType . Playlist , playlistInfo , tracks : [ trackOneDlResult , trackTwoDlResult ] , } ) ; } ) ; } ) ; } ) ;