forked from brandonbothell/popyt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.spec.ts
70 lines (55 loc) · 3.12 KB
/
search.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'mocha'
import { expect } from 'chai'
import YouTube, { Playlist, Video, Channel, YTComment } from '../src'
import { youtube } from './cache.spec'
const apiKey = process.env.YOUTUBE_API_KEY
if (!apiKey) {
throw new Error('No API key')
}
describe('Searching', () => {
it('should default to 10 results', async () => {
expect((await youtube.searchVideos('never gonna give you up')).results.length).to.equal(10)
expect((await youtube.searchChannels('rick astley')).results.length).to.equal(10)
expect((await youtube.searchPlaylists('music')).results.length).to.equal(10)
expect((await youtube.search([ Video, Channel, Playlist ], 'vevo')).results.length).to.equal(10)
}).timeout(20000)
it('should return an array', async () => {
expect((await youtube.searchPlaylists('music')).results).to.be.instanceOf(Array)
})
it('should reject if maxResults is < 1', async () => {
expect(await youtube.searchChannels('rick astley', 0).catch(error => { return error })).to.equal('Max results must be greater than 0 and less than or equal to 50')
})
it('should reject if maxResults is > 50', async () => {
expect(await youtube.searchVideos('never gonna give you up', 51).catch(error => { return error })).to.equal('Max results must be greater than 0 and less than or equal to 50')
})
it('should reject if api key is wrong', async () => {
const youtube = new YouTube('asda')
expect(await youtube.searchVideos('la di da').catch(error => { return error })).to.be.an.instanceOf(Error)
})
it('should set what it can with search results', async () => {
const channel = (await youtube.searchChannels('rick astley', 1)).results[0]
expect(channel.id).to.equal('UCuAXFkgsw1L7xaCfnd5JJOw')
expect(channel.country).to.equal(undefined)
expect(channel.language).to.equal(undefined)
expect(channel.views).to.equal(undefined)
expect(channel.comments).to.equal(undefined)
})
it('should be able to fetch videos of a channel search result', async () => {
Channel.part = 'contentDetails'
const channel = (await youtube.searchChannels('rick astley', 1)).results[0]
const videos = await channel.fetchVideos()
expect(videos).to.be.an.instanceOf(Playlist)
}).timeout(8000)
it('should work with multiple types', async () => {
const data = (await youtube.search([ Video, Playlist, Channel ], 'vevo', 10))
expect(data.results.find(r => r instanceof Video)).to.not.equal(undefined)
expect(data.results.find(r => r instanceof Playlist)).to.not.equal(undefined)
expect(data.results.find(r => r instanceof Channel)).to.not.equal(undefined)
})
it('should throw an error if kind is wrong', () => {
expect(() => new Video(youtube, { kind: 'notakind' })).to.throw('Invalid video type: notakind')
expect(() => new Channel(youtube, { kind: 'notakind' })).to.throw('Invalid channel type: notakind')
expect(() => new YTComment(youtube, { kind: 'notakind' }, 'video')).to.throw('Invalid comment type: notakind')
expect(() => new Playlist(youtube, { kind: 'notakind' })).to.throw('Invalid playlist type: notakind')
})
})