|
| 1 | +import 'mocha' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { |
| 4 | + isYouTubeVideoURL, |
| 5 | + videoIdFromYouTubeUrl, |
| 6 | +} from '../../src/utils/youtube' |
| 7 | + |
| 8 | +describe('videoIdFromYouTubeUrl', () => { |
| 9 | + it('Returns video id for video with playlist id', () => { |
| 10 | + const result = videoIdFromYouTubeUrl( |
| 11 | + 'https://www.youtube.com/watch?v=kfchvCyHmsc&list=PLDyKn8uKYtRalFdBWtv_EjDtUo2UEbu-a' |
| 12 | + ) |
| 13 | + expect(result).to.eq('kfchvCyHmsc') |
| 14 | + }) |
| 15 | + |
| 16 | + it('Returns video id for direct url', () => { |
| 17 | + const result = videoIdFromYouTubeUrl( |
| 18 | + 'https://www.youtube.com/v/vLfAtCbE_Jc' |
| 19 | + ) |
| 20 | + expect(result).to.eq('vLfAtCbE_Jc') |
| 21 | + }) |
| 22 | + |
| 23 | + it('Returns video id for standard url', () => { |
| 24 | + const result = videoIdFromYouTubeUrl( |
| 25 | + 'https://www.youtube.com/watch?v=vLfAtCbE_Jc' |
| 26 | + ) |
| 27 | + expect(result).to.eq('vLfAtCbE_Jc') |
| 28 | + }) |
| 29 | + |
| 30 | + it('Returns video id for short url', () => { |
| 31 | + const result = videoIdFromYouTubeUrl('https://youtu.be/vLfAtCbE_Jc') |
| 32 | + expect(result).to.eq('vLfAtCbE_Jc') |
| 33 | + }) |
| 34 | + |
| 35 | + it('Returns video id for short url with share id', () => { |
| 36 | + const result = videoIdFromYouTubeUrl( |
| 37 | + 'https://youtu.be/iZxR7rPdvuQ?si=ad73DTmmXL_lbn31' |
| 38 | + ) |
| 39 | + expect(result).to.eq('iZxR7rPdvuQ') |
| 40 | + }) |
| 41 | + |
| 42 | + it('Returns video id for embed url', () => { |
| 43 | + const result = videoIdFromYouTubeUrl( |
| 44 | + 'https://www.youtube.com/embed/vLfAtCbE_Jc' |
| 45 | + ) |
| 46 | + expect(result).to.eq('vLfAtCbE_Jc') |
| 47 | + }) |
| 48 | + |
| 49 | + it('Returns undefined for non-youtube url', () => { |
| 50 | + const result = videoIdFromYouTubeUrl( |
| 51 | + 'https://omnivore.app/iZxR7rPdvuQ?si=ad73DTmmXL_lbn31' |
| 52 | + ) |
| 53 | + expect(result).to.eq(undefined) |
| 54 | + }) |
| 55 | + |
| 56 | + it('Returns undefined for non-youtube short url', () => { |
| 57 | + const result = videoIdFromYouTubeUrl('https://omnivore.app/?v=iZxR7rPdvuQ') |
| 58 | + expect(result).to.eq(undefined) |
| 59 | + }) |
| 60 | + |
| 61 | + it('Returns video id when port is added', () => { |
| 62 | + const result = videoIdFromYouTubeUrl( |
| 63 | + 'https://www.youtube.com:443/watch?v=kfchvCyHmsc' |
| 64 | + ) |
| 65 | + expect(result).to.eq('kfchvCyHmsc') |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +describe('isYouTubeVideoURL', () => { |
| 70 | + it('Returns false for a shorts URL', () => { |
| 71 | + const result = isYouTubeVideoURL( |
| 72 | + 'https://www.youtube.com/shorts/ZsQKYwXbo4s' |
| 73 | + ) |
| 74 | + expect(result).to.eq(false) |
| 75 | + }) |
| 76 | + it('Returns false for a non-youtube URL', () => { |
| 77 | + const result = isYouTubeVideoURL('https://omnivore.app/about') |
| 78 | + expect(result).to.eq(false) |
| 79 | + }) |
| 80 | + it('Returns true for a video URL', () => { |
| 81 | + const result = isYouTubeVideoURL( |
| 82 | + 'https://www.youtube.com/watch?v=p4YOXmm839c' |
| 83 | + ) |
| 84 | + expect(result).to.eq(true) |
| 85 | + }) |
| 86 | +}) |
0 commit comments