Skip to content

Commit 0fb5f0e

Browse files
committed
UserHighlighterSimple: write tests
1 parent 39de68c commit 0fb5f0e

File tree

6 files changed

+108
-12
lines changed

6 files changed

+108
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testEnvironment: 'jsdom',
3+
setupFilesAfterEnv: [ 'mock-mediawiki' ],
4+
testURL: 'https://test.wikipedia.org' // this is only needed if you plan to use mw.Api or mw.storage
5+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import $ from 'jquery';
2+
global.$ = $;

UserHighlighterSimple/modules/UserHighlighterSimple.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class UserHighlighterSimple {
157157
return false;
158158
}
159159

160-
const title = this.getTitle( url, urlHelper );
160+
const title = this.getTitle( url );
161161

162162
// 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.
163163
try {
@@ -200,7 +200,7 @@ export class UserHighlighterSimple {
200200
*/
201201
addDomainIfMissing( url ) {
202202
if ( url.startsWith( '/' ) ) {
203-
url = window.location.origin + url;
203+
url = this.window.location.origin + url;
204204
}
205205
return url;
206206
}
@@ -212,7 +212,9 @@ export class UserHighlighterSimple {
212212
* @param {mw.Uri} urlHelper
213213
* @return {string}
214214
*/
215-
getTitle( url, urlHelper ) {
215+
getTitle( url ) {
216+
const urlHelper = new this.mw.Uri( url );
217+
216218
// for links in the format /w/index.php?title=Blah
217219
const titleParameterOfUrl = this.mw.util.getParamValue( 'title', url );
218220
if ( titleParameterOfUrl ) {
@@ -229,8 +231,8 @@ export class UserHighlighterSimple {
229231

230232
notInUserOrUserTalkNamespace() {
231233
const namespace = this.titleHelper.getNamespaceId();
232-
const notInSpecialUserOrUserTalkNamespace = this.$.inArray( namespace, [ 2, 3 ] ) === -1;
233-
return notInSpecialUserOrUserTalkNamespace;
234+
// TODO: refactor $.inArray
235+
return this.$.inArray( namespace, [ 2, 3 ] ) === -1;
234236
}
235237

236238
/**

UserHighlighterSimple/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"dependencies": {
33
"jest": "^27.4.3",
4-
"jquery": "^3.6.0"
4+
"jquery": "^3.6.0",
5+
"mock-mediawiki": "^1.4.0",
6+
"types-mediawiki": "^1.9.1"
57
},
68
"scripts": {
7-
"test": "jest",
8-
"build": "babel-node src/DraftCleaner2.js -d dist"
9+
"test": "jest"
910
},
1011
"devDependencies": {
1112
"@babel/plugin-transform-modules-commonjs": "^7.16.0",

UserHighlighterSimple/tests/UserHighlighterSimple.test.js

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,92 @@ import { UserHighlighterSimple } from '../modules/UserHighlighterSimple.js';
66
// Copy package.json and .babelrc from a project that already has this working
77
// Babel tutorial: https://www.sitepoint.com/babel-beginners-guide/
88

9-
const userHighlighterSimple = new UserHighlighterSimple();
9+
const userHighlighterSimple = new UserHighlighterSimple( $, mw, window );
1010

1111
describe( 'linksToAUser( url )', () => {
12-
test( 'https://en.wikipedia.org/wiki/Main_Page', () => {
13-
// TODO: instantiate a UserHighlighterSimple class with 3 parameters. They can be mocks.
14-
// TODO: test linksToAUser( 'https://en.wikipedia.org/wiki/Main_Page' ) and assert that it equals fasle
12+
test( 'normal non-userpage URL using /wiki/', () => {
13+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/Main_Page' ) ).toBe( false );
14+
} );
15+
16+
test( 'normal userpage URL using /wiki/', () => {
17+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( true );
18+
} );
19+
20+
test( 'normal user talk page URL using /wiki/', () => {
21+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User talk:Novem_Linguae' ) ).toBe( true );
22+
} );
23+
24+
test( 'not http or https', () => {
25+
expect( userHighlighterSimple.linksToAUser( 'ftp://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( false );
26+
} );
27+
28+
test( 'is an anchor URL only (starts with #)', () => {
29+
expect( userHighlighterSimple.linksToAUser( 'ftp://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( false );
30+
} );
31+
32+
test( 'gibberish string', () => {
33+
expect( userHighlighterSimple.linksToAUser( 'abcd' ) ).toBe( false );
34+
} );
35+
36+
test( 'empty string', () => {
37+
expect( userHighlighterSimple.linksToAUser( '' ) ).toBe( false );
38+
} );
39+
40+
test( 'URL starts with /, needs a domain added', () => {
41+
expect( userHighlighterSimple.linksToAUser( '/wiki/User:Novem_Linguae' ) ).toBe( true );
42+
} );
43+
44+
test( 'URL starts with /, needs a domain added', () => {
45+
expect( userHighlighterSimple.linksToAUser( '/wiki/Main_Page' ) ).toBe( false );
46+
} );
47+
48+
test( 'URL with section link should not throw error', () => {
49+
expect( userHighlighterSimple.linksToAUser( 'https://meta.wikimedia.org/wiki/Community_Wishlist_Survey_2022/Larger_suggestions#1%' ) ).toBe( false );
50+
} );
51+
52+
test( 'URL with parameters that aren\'t title, action, or redlink', () => {
53+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae&diff=prev&oldid=1276401726' ) ).toBe( false );
54+
} );
55+
56+
test( 'URL with parameters that are title, action, or redlink', () => {
57+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=Main_Page' ) ).toBe( false );
58+
} );
59+
60+
test( 'URL with parameters that are title, action, or redlink', () => {
61+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae' ) ).toBe( true );
62+
} );
63+
64+
test( 'archive URL of a user page', () => {
65+
expect( userHighlighterSimple.linksToAUser( 'https://web.archive.org/web/20231105033559/https://en.wikipedia.org/wiki/User:SandyGeorgia/SampleIssue' ) ).toBe( false );
66+
} );
67+
68+
test( 'wrong namespace', () => {
69+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/Template:Uw-test1' ) ).toBe( false );
70+
} );
71+
72+
test( 'DiscussionTools section link', () => {
73+
expect( userHighlighterSimple.linksToAUser( 'https://en.wikipedia.org/wiki/User_talk:Novem_Linguae#c-Scope_creep-20250218175200-Novem_Linguae-20250218170600' ) ).toBe( false );
74+
} );
75+
} );
76+
77+
describe( 'getTitle( url )', () => {
78+
test( 'URL using /wiki/, user namespace', () => {
79+
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/wiki/User:Novem_Linguae' ) ).toBe( 'User:Novem_Linguae' );
80+
} );
81+
82+
test( 'URL using ?title=, user namespace', () => {
83+
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/w/index.php?title=User:Novem_Linguae' ) ).toBe( 'User:Novem_Linguae' );
84+
} );
85+
86+
test( 'URL using /wiki/, user talk namespace', () => {
87+
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/wiki/User talk:Novem_Linguae' ) ).toBe( 'User talk:Novem_Linguae' );
88+
} );
89+
90+
test( 'URL using ?title=, user talk namespace', () => {
91+
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/w/index.php?title=User talk:Novem_Linguae' ) ).toBe( 'User talk:Novem_Linguae' );
92+
} );
93+
94+
test( 'neither of the above', () => {
95+
expect( userHighlighterSimple.getTitle( 'https://en.wikipedia.org/' ) ).toBe( '' );
1596
} );
1697
} );
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"include": [
3+
"./node_modules/types-mediawiki"
4+
]
5+
}

0 commit comments

Comments
 (0)