Add tests

This commit is contained in:
Hongbo Wu
2023-01-30 19:10:27 +08:00
parent ec7344b923
commit 61bf89b9ad
2 changed files with 34 additions and 4 deletions

View File

@ -18,7 +18,7 @@ export const getYoutubeVideoId = (url: string) => {
return videoId
}
const getYoutubePlaylistId = (url: string) => {
export const getYoutubePlaylistId = (url: string) => {
const u = new URL(url)
return u.searchParams.get('list')
}

View File

@ -1,6 +1,6 @@
import { expect } from 'chai'
import 'mocha'
import { escapeTitle, getYoutubeVideoId } from '../src/websites/youtube-handler'
import { expect } from "chai";
import "mocha";
import { escapeTitle, getYoutubePlaylistId, getYoutubeVideoId } from "../src/websites/youtube-handler";
describe('getYoutubeVideoId', () => {
it('should parse video id out of a URL', async () => {
@ -24,6 +24,36 @@ describe('getYoutubeVideoId', () => {
})
})
describe('getYoutubePlaylistId', () => {
it('should parse playlist id out of a URL', async () => {
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/watch?v=BnSUk0je6oo&t=269s&list=PL6D4F6A0D6B0C6A5E'
)
)
expect('RDvFD2gu007dc').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/watch?v=vFD2gu007dc&list=RDvFD2gu007dc&start_radio=1'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E&feature=share'
)
)
expect('PL6D4F6A0D6B0C6A5E').to.eq(
getYoutubePlaylistId(
'https://www.youtube.com/playlist?list=PL6D4F6A0D6B0C6A5E&feature=share'
)
)
})
})
describe('escapeTitle', () => {
it('escapes the special characters in the title', async () => {
expect(escapeTitle("The Stanley's Parable")).to.eq(