From b561ec1ad9984f64bb3ab3c843f4e50d8d4cba2d Mon Sep 17 00:00:00 2001 From: NikkelM <57323886+NikkelM@users.noreply.github.com> Date: Sat, 25 Nov 2023 19:16:46 +0000 Subject: [PATCH] Added tests for new function, fixed potential bugs --- src/utils.js | 4 +++- test/utils.test.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index 52dcedd7..a3d28c57 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,13 +18,15 @@ export function isVideoUrl(url) { if (!url) return false; const urlParts = url.split("/"); + if (!urlParts[2]?.includes("youtube")) return false; return urlParts[3]?.startsWith("watch?v=") ?? false; } export function getPageTypeFromURL(url) { - if (!url) return false; + if (!url) return null; const urlParts = url.split("/"); + if (!urlParts[2]?.includes("youtube")) return null; if (urlParts[3]?.startsWith("watch?v=")) { return "video"; } else if (urlParts[3]?.startsWith("shorts")) { diff --git a/test/utils.test.js b/test/utils.test.js index c4c22d0d..8702c15c 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -2,7 +2,7 @@ import expect from 'expect.js'; import sinon from 'sinon'; import { JSDOM } from 'jsdom'; -import { isVideoUrl, isEmpty, getLength, addHours, delay, setDOMTextWithDelay, RandomYoutubeVideoError, YoutubeAPIError } from '../src/utils.js'; +import { isVideoUrl, getPageTypeFromURL, isEmpty, getLength, addHours, delay, setDOMTextWithDelay, RandomYoutubeVideoError, YoutubeAPIError } from '../src/utils.js'; describe('utils.js', function () { context('URL helpers', function () { @@ -29,8 +29,39 @@ describe('utils.js', function () { expect(isVideoUrl("https://www.google.com")).to.be(false); expect(isVideoUrl("https://www.google.com/search?q=youtube")).to.be(false); expect(isVideoUrl("about:blank")).to.be(false); + expect(isVideoUrl("edge://extensions/")).to.be(false); }); + }); + context('getPageTypeFromURL()', function () { + it('should not break if no URL is provided', function () { + expect(getPageTypeFromURL(null)).to.be(null); + expect(getPageTypeFromURL(undefined)).to.be(null); + expect(getPageTypeFromURL("")).to.be(null); + expect(getPageTypeFromURL(0)).to.be(null); + }); + + it('should identify a YouTube video URL', function () { + expect(getPageTypeFromURL("https://www.youtube.com/watch?v=12345678901")).to.be("video"); + }); + + it('should identify a YouTube shorts URL', function () { + expect(getPageTypeFromURL("https://www.youtube.com/shorts/12345678901")).to.be("short"); + }); + + it('should identify a YouTube non-video/non-shorts URL', function () { + expect(getPageTypeFromURL("https://www.youtube.com/channel/myChannelID")).to.be("channel"); + expect(getPageTypeFromURL("https://www.youtube.com/@Username")).to.be("channel"); + expect(getPageTypeFromURL("https://www.youtube.com")).to.be("channel"); + expect(getPageTypeFromURL("https://www.youtube.com/playlist?list=PL1234567890")).to.be("channel"); + }); + + it('should identify a non-YouTube URL', function () { + expect(getPageTypeFromURL("https://www.google.com")).to.be(null); + expect(getPageTypeFromURL("https://www.google.com/search?q=youtube")).to.be(null); + expect(getPageTypeFromURL("about:blank")).to.be(null); + expect(getPageTypeFromURL("edge://extensions/")).to.be(null); + }); }); });