Skip to content

Commit

Permalink
UserHighlighterSimple: write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Feb 19, 2025
1 parent 39de68c commit 0fb5f0e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 12 deletions.
5 changes: 5 additions & 0 deletions UserHighlighterSimple/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: 'jsdom',
setupFilesAfterEnv: [ 'mock-mediawiki' ],
testURL: 'https://test.wikipedia.org' // this is only needed if you plan to use mw.Api or mw.storage
};
2 changes: 2 additions & 0 deletions UserHighlighterSimple/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import $ from 'jquery';
global.$ = $;
12 changes: 7 additions & 5 deletions UserHighlighterSimple/modules/UserHighlighterSimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class UserHighlighterSimple {
return false;
}

const title = this.getTitle( url, urlHelper );
const title = this.getTitle( url );

// Handle edge cases such as https://web.archive.org/web/20231105033559/https://en.wikipedia.org/wiki/User:SandyGeorgia/SampleIssue, which shows up as isUserPageLink = true but isn't really a user page.
try {
Expand Down Expand Up @@ -200,7 +200,7 @@ export class UserHighlighterSimple {
*/
addDomainIfMissing( url ) {
if ( url.startsWith( '/' ) ) {
url = window.location.origin + url;
url = this.window.location.origin + url;
}
return url;
}
Expand All @@ -212,7 +212,9 @@ export class UserHighlighterSimple {
* @param {mw.Uri} urlHelper
* @return {string}
*/
getTitle( url, urlHelper ) {
getTitle( url ) {
const urlHelper = new this.mw.Uri( url );

// for links in the format /w/index.php?title=Blah
const titleParameterOfUrl = this.mw.util.getParamValue( 'title', url );
if ( titleParameterOfUrl ) {
Expand All @@ -229,8 +231,8 @@ export class UserHighlighterSimple {

notInUserOrUserTalkNamespace() {
const namespace = this.titleHelper.getNamespaceId();
const notInSpecialUserOrUserTalkNamespace = this.$.inArray( namespace, [ 2, 3 ] ) === -1;
return notInSpecialUserOrUserTalkNamespace;
// TODO: refactor $.inArray
return this.$.inArray( namespace, [ 2, 3 ] ) === -1;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions UserHighlighterSimple/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"dependencies": {
"jest": "^27.4.3",
"jquery": "^3.6.0"
"jquery": "^3.6.0",
"mock-mediawiki": "^1.4.0",
"types-mediawiki": "^1.9.1"
},
"scripts": {
"test": "jest",
"build": "babel-node src/DraftCleaner2.js -d dist"
"test": "jest"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.16.0",
Expand Down
89 changes: 85 additions & 4 deletions UserHighlighterSimple/tests/UserHighlighterSimple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,92 @@ import { UserHighlighterSimple } from '../modules/UserHighlighterSimple.js';
// Copy package.json and .babelrc from a project that already has this working
// Babel tutorial: https://www.sitepoint.com/babel-beginners-guide/

const userHighlighterSimple = new UserHighlighterSimple();
const userHighlighterSimple = new UserHighlighterSimple( $, mw, window );

describe( 'linksToAUser( url )', () => {
test( 'https://en.wikipedia.org/wiki/Main_Page', () => {
// TODO: instantiate a UserHighlighterSimple class with 3 parameters. They can be mocks.
// TODO: test linksToAUser( 'https://en.wikipedia.org/wiki/Main_Page' ) and assert that it equals fasle
test( 'normal non-userpage URL using /wiki/', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/Main_Page' ) ).toBe( false );
} );

test( 'normal userpage URL using /wiki/', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( true );
} );

test( 'normal user talk page URL using /wiki/', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User talk:Novem_Linguae' ) ).toBe( true );
} );

test( 'not http or https', () => {
expect( userHighlighterSimple.linksToAUser( 'ftp://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( false );
} );

test( 'is an anchor URL only (starts with #)', () => {
expect( userHighlighterSimple.linksToAUser( 'ftp://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( false );
} );

test( 'gibberish string', () => {
expect( userHighlighterSimple.linksToAUser( 'abcd' ) ).toBe( false );
} );

test( 'empty string', () => {
expect( userHighlighterSimple.linksToAUser( '' ) ).toBe( false );
} );

test( 'URL starts with /, needs a domain added', () => {
expect( userHighlighterSimple.linksToAUser( '/wiki/User:Novem_Linguae' ) ).toBe( true );
} );

test( 'URL starts with /, needs a domain added', () => {
expect( userHighlighterSimple.linksToAUser( '/wiki/Main_Page' ) ).toBe( false );
} );

test( 'URL with section link should not throw error', () => {
expect( userHighlighterSimple.linksToAUser( 'https://meta.wikimedia.org/wiki/Community_Wishlist_Survey_2022/Larger_suggestions#1%' ) ).toBe( false );
} );

test( 'URL with parameters that aren\'t title, action, or redlink', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae&diff=prev&oldid=1276401726' ) ).toBe( false );
} );

test( 'URL with parameters that are title, action, or redlink', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=Main_Page' ) ).toBe( false );
} );

test( 'URL with parameters that are title, action, or redlink', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae' ) ).toBe( true );
} );

test( 'archive URL of a user page', () => {
expect( userHighlighterSimple.linksToAUser( 'https://web.archive.org/web/20231105033559/https://en.wikipedia.org/wiki/User:SandyGeorgia/SampleIssue' ) ).toBe( false );
} );

test( 'wrong namespace', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/Template:Uw-test1' ) ).toBe( false );
} );

test( 'DiscussionTools section link', () => {
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User_talk:Novem_Linguae#c-Scope_creep-20250218175200-Novem_Linguae-20250218170600' ) ).toBe( false );
} );
} );

describe( 'getTitle( url )', () => {
test( 'URL using /wiki/, user namespace', () => {
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( 'User:Novem_Linguae' );
} );

test( 'URL using ?title=, user namespace', () => {
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae' ) ).toBe( 'User:Novem_Linguae' );
} );

test( 'URL using /wiki/, user talk namespace', () => {
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/wiki/User talk:Novem_Linguae' ) ).toBe( 'User talk:Novem_Linguae' );
} );

test( 'URL using ?title=, user talk namespace', () => {
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/w/index.php?title=User talk:Novem_Linguae' ) ).toBe( 'User talk:Novem_Linguae' );
} );

test( 'neither of the above', () => {
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/' ) ).toBe( '' );
} );
} );
5 changes: 5 additions & 0 deletions UserHighlighterSimple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": [
"./node_modules/types-mediawiki"
]
}

0 comments on commit 0fb5f0e

Please sign in to comment.