Skip to content

Commit

Permalink
Improve util tests
Browse files Browse the repository at this point in the history
Add test case for `Util.processYoutubeVideoTitle` function.
Bind functions to Util module before testing. This allows to use `this` keyword in Util module.
  • Loading branch information
alexesprit committed Jun 11, 2019
1 parent 2d3f797 commit 02e904b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/core/content/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Util = {
let [artist, track] = [null, null];

// Try to match one of the regexps
for (let regExp of Util.youtubeTitleRegExps) {
for (let regExp of this.youtubeTitleRegExps) {
let artistTrack = title.match(regExp.pattern);
if (artistTrack) {
artist = artistTrack[regExp.groups.artist];
Expand All @@ -49,11 +49,11 @@ const Util = {
}

// No match? Try splitting, then.
if (Util.isArtistTrackEmpty({ artist, track })) {
if (this.isArtistTrackEmpty({ artist, track })) {
({ artist, track } = this.splitArtistTrack(title));
}

if (Util.isArtistTrackEmpty({ artist, track })) {
if (this.isArtistTrackEmpty({ artist, track })) {
track = title;
}

Expand All @@ -70,7 +70,7 @@ const Util = {
return null;
}

let match = videoUrl.match(Util.videoIdRegExp);
let match = videoUrl.match(this.videoIdRegExp);
if (match) {
return match[7];
}
Expand Down
8 changes: 7 additions & 1 deletion tests/content/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ const PROCESS_YOUTUBE_TITLE_DATA = [{
description: 'should process tracks with seperators without leading whitespace and quotes',
source: 'Artist: "Track Name"',
expected: { artist: 'Artist', track: 'Track Name' }
}, {
description: 'should use title as track title',
source: 'Track Name',
expected: { artist: null, track: 'Track Name' }
}];

/**
Expand Down Expand Up @@ -355,9 +359,11 @@ function testIsArtistTrackEmpty() {
* @param {Array} testData Array of test data
*/
function testFunction(func, testData) {
const boundFunc = func.bind(Util);

for (let data of testData) {
let { description, source, expected } = data;
let actual = func(source);
let actual = boundFunc(source);

it(description, function() {
expect(actual).to.be.equal(expected);
Expand Down

0 comments on commit 02e904b

Please sign in to comment.