2017-07-28 17:32:46 +00:00
|
|
|
import { getTitle, isValid } from '../../src/title';
|
2017-04-27 17:38:20 +00:00
|
|
|
|
|
|
|
/* global Map */
|
|
|
|
|
2017-06-08 10:08:58 +00:00
|
|
|
QUnit.module( 'title#getTitle', {
|
2018-03-14 22:04:59 +00:00
|
|
|
beforeEach() {
|
2017-04-27 17:38:20 +00:00
|
|
|
this.config = new Map();
|
|
|
|
this.config.set( 'wgArticlePath', '/wiki/$1' );
|
|
|
|
|
|
|
|
this.location = global.location = { hostname: 'en.wikipedia.org' };
|
|
|
|
|
|
|
|
window.mediaWiki.RegExp = {
|
2018-03-14 23:50:09 +00:00
|
|
|
escape: this.sandbox.spy( ( str ) => {
|
2017-10-09 14:56:15 +00:00
|
|
|
return str.replace( /([\\{}()|.?*+\-^$[\]])/g, '\\$1' );
|
2017-04-27 17:38:20 +00:00
|
|
|
} )
|
|
|
|
};
|
|
|
|
|
|
|
|
window.mediaWiki.Uri = this.sandbox.stub().throws( 'UNIMPLEMENTED' );
|
|
|
|
},
|
2018-03-14 22:04:59 +00:00
|
|
|
afterEach() {
|
2017-04-27 17:38:20 +00:00
|
|
|
global.location = null;
|
|
|
|
window.mediaWiki.RegExp = null;
|
|
|
|
window.mediaWiki.Uri = null;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should return the title of a url with a title query param', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const href = '/w/index.php?title=Foo';
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: this.location.hostname,
|
|
|
|
query: {
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), 'Foo' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should return the title of a pretty url if it conforms wgArticlePath', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const href = '/wiki/Foo';
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: this.location.hostname,
|
|
|
|
path: href,
|
|
|
|
query: {}
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), 'Foo' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should return the title of a pretty url properly decoded', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const href = '/wiki/%E6%B8%AC%E8%A9%A6';
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: this.location.hostname,
|
|
|
|
path: href,
|
|
|
|
query: {}
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), '測試' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should skip urls that mw.Uri cannot parse', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const href = 'javascript:void(0);'; // eslint-disable-line no-script-url
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).throws(
|
|
|
|
new Error( 'Cannot parse' )
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), undefined );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should skip urls that are external', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: 'www.youtube.com',
|
|
|
|
path: '/watch',
|
|
|
|
query: { v: 'dQw4w9WgXcQ' }
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), undefined );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should skip urls not on article path without one title query param', function ( assert ) {
|
|
|
|
// No params
|
2018-03-15 16:30:11 +00:00
|
|
|
let href = '/Foo';
|
2017-04-27 17:38:20 +00:00
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: this.location.hostname,
|
|
|
|
path: '/Foo',
|
|
|
|
query: {}
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), undefined );
|
|
|
|
|
|
|
|
// Multiple query params
|
|
|
|
href = '/Foo?a=1&title=Foo';
|
|
|
|
window.mediaWiki.Uri.withArgs( href ).returns( {
|
|
|
|
host: this.location.hostname,
|
|
|
|
path: '/Foo',
|
|
|
|
query: { a: 1, title: 'Foo' }
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal( getTitle( href, this.config ), undefined );
|
|
|
|
} );
|
2017-06-08 10:45:11 +00:00
|
|
|
|
|
|
|
QUnit.module( 'title#isValid', {
|
2018-03-14 22:04:59 +00:00
|
|
|
beforeEach() {
|
2017-06-08 10:45:11 +00:00
|
|
|
window.mediaWiki.Title = {
|
|
|
|
newFromText: this.sandbox.stub().throws( 'UNIMPLEMENTED' )
|
|
|
|
};
|
|
|
|
},
|
2018-03-14 22:04:59 +00:00
|
|
|
afterEach() {
|
2017-06-08 10:45:11 +00:00
|
|
|
window.mediaWiki.Title = null;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
QUnit.test( 'it should return null if the title is empty', ( assert ) => {
|
2017-06-08 10:45:11 +00:00
|
|
|
assert.equal( isValid(), null, 'Doesn\'t accept null titles' );
|
|
|
|
assert.equal( isValid( '' ), null, 'Doesn\'t accept empty titles' );
|
|
|
|
} );
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
QUnit.test( 'it should return null if the title can\'t be parsed properly', ( assert ) => {
|
2017-06-08 10:45:11 +00:00
|
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( null );
|
|
|
|
assert.equal( isValid( 'title' ), null );
|
|
|
|
} );
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
QUnit.test( 'it should return null if the title can\'t be parsed properly', ( assert ) => {
|
2017-06-08 10:45:11 +00:00
|
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( null );
|
|
|
|
assert.equal( isValid( 'title' ), null );
|
2018-01-18 18:48:16 +00:00
|
|
|
assert.equal(
|
|
|
|
window.mediaWiki.Title.newFromText.callCount, 1,
|
|
|
|
'mediaWiki.Title.newFromText called for parsing the title' );
|
2017-06-08 10:45:11 +00:00
|
|
|
} );
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
QUnit.test( 'it should return null if the title is not from a content namespace', ( assert ) => {
|
2017-06-08 10:45:11 +00:00
|
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( {
|
|
|
|
namespace: 1
|
|
|
|
} );
|
|
|
|
assert.equal( isValid( 'title', [ 5 ] ), null );
|
|
|
|
} );
|
|
|
|
|
2018-03-14 23:50:09 +00:00
|
|
|
QUnit.test( 'it should return the title object if the title is from a content namespace', ( assert ) => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const mwTitle = {
|
2017-06-08 10:45:11 +00:00
|
|
|
namespace: 3
|
|
|
|
};
|
|
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( mwTitle );
|
|
|
|
assert.strictEqual( isValid( 'title', [ 1, 3, 5 ] ), mwTitle );
|
|
|
|
} );
|