Skip to content

Commit

Permalink
Added tests for new function, fixed potential bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM committed Nov 25, 2023
1 parent acc3896 commit b561ec1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
33 changes: 32 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
});
});
});

Expand Down

0 comments on commit b561ec1

Please sign in to comment.