Some checks failed
Verify / Install node dependencies (push) Failing after 2s
Verify / Build packages (push) Has been skipped
Verify / Check linting & formatting (push) Has been skipped
Verify / Run unit tests (push) Has been skipped
Verify / Run e2e tests (shard 1/4) (push) Has been skipped
Verify / Run e2e tests (shard 2/4) (push) Has been skipped
Verify / Run e2e tests (shard 3/4) (push) Has been skipped
Verify / Run e2e tests (shard 4/4) (push) Has been skipped
Publish / Resolve publish config (push) Failing after 2s
Publish / Send notification to Slack (push) Has been skipped
Publish / Send failure notification to Slack (push) Failing after 8s
Verify / Merge e2e reports (push) Has been skipped
Publish / Build (push) Has been skipped
Publish / Bump Versions OR Release to Registry (push) Has been skipped
164 lines
5.1 KiB
TypeScript
164 lines
5.1 KiB
TypeScript
import { describe, expect, it } from 'vite-plus/test'
|
|
|
|
import {
|
|
getAttributesFromYoutubeEmbedUrl,
|
|
getEmbedUrlFromYoutubeUrl,
|
|
isValidYoutubeUrl,
|
|
} from './utils.js'
|
|
|
|
describe('YouTube Live URL handling', () => {
|
|
it('generates correct embed URL for a YouTube Live URL', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/live/EkRHhOCdZjw',
|
|
controls: true,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw')
|
|
})
|
|
|
|
it('generates correct embed URL for a YouTube Live URL without www prefix', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://youtube.com/live/EkRHhOCdZjw',
|
|
controls: true,
|
|
autoplay: true,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw?autoplay=1')
|
|
})
|
|
|
|
it('generates correct embed URL for a YouTube Live URL with multiple parameters', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/live/EkRHhOCdZjw',
|
|
autoplay: true,
|
|
controls: false,
|
|
rel: 0,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw?autoplay=1&controls=0&rel=0')
|
|
})
|
|
|
|
it('generates correct embed URL for a YouTube Live URL with nocookie option', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/live/EkRHhOCdZjw',
|
|
nocookie: true,
|
|
controls: true,
|
|
rel: 1,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube-nocookie.com/embed/EkRHhOCdZjw?rel=1')
|
|
})
|
|
|
|
it('keeps a live URL with a query string on the video id', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/live/EkRHhOCdZjw?feature=share',
|
|
controls: true,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw')
|
|
})
|
|
})
|
|
|
|
describe('YouTube path-like near misses', () => {
|
|
const nearMisses = [
|
|
'https://www.youtube.com/notlive/EkRHhOCdZjw',
|
|
'https://www.youtube.com/notshorts/EkRHhOCdZjw',
|
|
'https://www.youtube.com/watch?redirect=live/EkRHhOCdZjw',
|
|
]
|
|
|
|
nearMisses.forEach(url => {
|
|
it(`does not embed a video id for ${url}`, () => {
|
|
expect(getEmbedUrlFromYoutubeUrl({ url, controls: true })).toBe(null)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('YouTube Shorts URL handling', () => {
|
|
it('generates correct embed URL for YouTube Shorts with rel parameter', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/shorts/EkRHhOCdZjw',
|
|
controls: true,
|
|
rel: 1,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw?rel=1')
|
|
})
|
|
|
|
it('generates correct embed URL for YouTube Shorts without www prefix', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://youtube.com/shorts/EkRHhOCdZjw',
|
|
controls: true,
|
|
autoplay: true,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw?autoplay=1')
|
|
})
|
|
|
|
it('generates correct embed URL for YouTube Shorts with multiple parameters', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/shorts/EkRHhOCdZjw',
|
|
autoplay: true,
|
|
controls: false,
|
|
rel: 0,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw?autoplay=1&controls=0&rel=0')
|
|
})
|
|
|
|
it('generates correct embed URL for YouTube Shorts with nocookie option', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/shorts/EkRHhOCdZjw',
|
|
nocookie: true,
|
|
controls: true,
|
|
rel: 1,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube-nocookie.com/embed/EkRHhOCdZjw?rel=1')
|
|
})
|
|
|
|
it('generates correct embed URL for YouTube Shorts without extra parameters', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/shorts/EkRHhOCdZjw',
|
|
controls: true,
|
|
})
|
|
|
|
expect(result).toBe('https://www.youtube.com/embed/EkRHhOCdZjw')
|
|
})
|
|
})
|
|
|
|
describe('YouTube Playlist URL handling', () => {
|
|
it('generates correct embed URL for playlist with additional parameters using & separator', () => {
|
|
const result = getEmbedUrlFromYoutubeUrl({
|
|
url: 'https://www.youtube.com/playlist?list=PLrAXtmErZgOeiKm4sgNOknGvNjby9efdf',
|
|
controls: true,
|
|
autoplay: true,
|
|
})
|
|
|
|
// Playlist URLs use ?list= in base URL, so additional params should use &
|
|
expect(result).toBe(
|
|
'https://www.youtube-nocookie.com/embed/videoseries?list=PLrAXtmErZgOeiKm4sgNOknGvNjby9efdf&autoplay=1',
|
|
)
|
|
})
|
|
})
|
|
|
|
it.each([
|
|
'https://www.youtube.com/embed/',
|
|
'https://www.youtube.com/embed/videoseries',
|
|
'https://example.com/embed/dQw4w9WgXcQ',
|
|
])('returns null for unsupported youtube embed urls: %s', embedUrl => {
|
|
expect(getAttributesFromYoutubeEmbedUrl(embedUrl)).toBeNull()
|
|
})
|
|
|
|
describe('missing URLs', () => {
|
|
it('accepts a well-formed youtube url', () => {
|
|
expect(isValidYoutubeUrl('https://www.youtube.com/watch?v=EkRHhOCdZjw')).toBeTruthy()
|
|
})
|
|
|
|
it('returns null for a missing url instead of throwing', () => {
|
|
expect(isValidYoutubeUrl(null)).toBe(null)
|
|
})
|
|
|
|
it('returns null from getEmbedUrlFromYoutubeUrl when the url is missing', () => {
|
|
expect(getEmbedUrlFromYoutubeUrl({ url: null as unknown as string })).toBe(null)
|
|
})
|
|
})
|