import { datatype } from 'faker' import { factory } from '../../../test/factories' import { HttpService } from '../http' import { Youtube } from './youtube' globalThis.secret = { firebase_config: { api_key: datatype.uuid(), }, } as any describe('Youtube', () => { let subject: Youtube let stubFetch: jest.SpyInstance beforeAll(() => { subject = new Youtube(new HttpService()) stubFetch = jest.spyOn(subject, 'fetch') }) describe('#getPlaylistById', () => { let youtubePlaylistsList: any let id: string beforeAll(async () => { id = datatype.uuid() youtubePlaylistsList = await factory.build('youtube_playlists_list_data') }) beforeEach(() => { stubFetch.mockResolvedValue(youtubePlaylistsList) }) it('calls list method with id gived', async () => { await subject.getPlaylistById(id) expect(stubFetch).toBeCalledWith('playlists', expect.objectContaining({ value: id })) }) it('returns YoutubePlaylist type', async () => { await expect(subject.getPlaylistById(id)).resolves.toEqual( expect.objectContaining({ id: youtubePlaylistsList.items[0].id, snippet: { ...youtubePlaylistsList.items[0].snippet, publishedAt: new Date(youtubePlaylistsList.items[0].snippet.publishedAt), }, }), ) }) }) describe('#getVideoById', () => { let youtubeVideosList: any let id: string beforeAll(async () => { id = datatype.uuid() youtubeVideosList = await factory.build('youtube_videos_list_data') }) beforeEach(() => { stubFetch.mockResolvedValue(youtubeVideosList) }) it('calls list method with id gived', async () => { await subject.getVideoById(id) expect(stubFetch).toBeCalledWith('videos', expect.objectContaining({ value: id })) }) it('returns YoutubeVideo type', async () => { await expect(subject.getVideoById(id)).resolves.toEqual( expect.objectContaining({ id: youtubeVideosList.items[0].id, snippet: { ...youtubeVideosList.items[0].snippet, publishedAt: new Date(youtubeVideosList.items[0].snippet.publishedAt), }, }), ) }) }) describe('#findPlaylistsByChannelId', () => { let youtubePlaylistsList: any let id: string beforeAll(async () => { id = datatype.uuid() youtubePlaylistsList = await factory.build('youtube_playlists_list_data') }) beforeEach(() => { stubFetch.mockResolvedValue(youtubePlaylistsList) }) it('calls list method with channelId gived', async () => { await subject.findPlaylistsByChannelId(id) expect(stubFetch).toBeCalledWith('playlists', expect.objectContaining({ value: id })) }) it('returns YoutubePlaylist array', async () => { await expect(subject.findPlaylistsByChannelId(id)).resolves.toEqual( youtubePlaylistsList.items.map(playlist => expect.objectContaining({ id: playlist.id, snippet: { ...playlist.snippet, publishedAt: new Date(playlist.snippet.publishedAt), }, }), ), ) }) }) describe('#findVideosByPlaylistId', () => { let youtubePlaylistItemsList: any let id: string beforeAll(async () => { id = datatype.uuid() youtubePlaylistItemsList = await factory.build('youtube_playlistitems_list_data') }) beforeEach(() => { stubFetch.mockResolvedValue(youtubePlaylistItemsList) }) it('calls list method with playlistId gived', async () => { await subject.findVideosByPlaylistId(id) expect(stubFetch).toBeCalledWith('playlistItems', expect.objectContaining({ value: id })) }) it('returns YoutubeVideo array', async () => { await expect(subject.findVideosByPlaylistId(id)).resolves.toEqual( youtubePlaylistItemsList.items.map(item => expect.objectContaining({ id: item.snippet.resourceId.videoId, snippet: { ...item.snippet, publishedAt: new Date(item.snippet.publishedAt), }, }), ), ) }) }) })