mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-24 07:34:11 +00:00
e6081106f1
Why: Because they are the approved standard by TC39 and Ecma for JavaScript modules. Changes: * Wrap mw-node-qunit in run.js to register babel to transpile modules for node v6 * Change all sources in src/ to use ES modules * Change constants.js to be able to run without jQuery.bracketedDevicePixelRatio given ES modules are hoisted to the top by spec so we can't patch globals before importing it * Change all tests in tests/node-qunit/ to use ES modules * Drop usage of mock-require given ES modules are easy to stub with sinon Additional changes: * Rename tests/node-qunit/renderer.js to renderer.test.js to follow the convention of all the other files * Make npm run test:node run only .test.js test files so that it doesn't run the stubs.js or run.js file. Bug: T171951 Change-Id: I17a0b76041d5e2fd18e2d54950d9d7c0db99a941
144 lines
4.2 KiB
JavaScript
144 lines
4.2 KiB
JavaScript
import { getTitle, isValid } from '../../src/title';
|
|
|
|
/* global Map */
|
|
|
|
QUnit.module( 'title#getTitle', {
|
|
beforeEach: function () {
|
|
this.config = new Map();
|
|
this.config.set( 'wgArticlePath', '/wiki/$1' );
|
|
|
|
this.location = global.location = { hostname: 'en.wikipedia.org' };
|
|
|
|
window.mediaWiki.RegExp = {
|
|
escape: this.sandbox.spy( function ( str ) {
|
|
return str.replace( /([\\{}()|.?*+\-\^$\[\]])/g, '\\$1' );
|
|
} )
|
|
};
|
|
|
|
window.mediaWiki.Uri = this.sandbox.stub().throws( 'UNIMPLEMENTED' );
|
|
},
|
|
afterEach: function () {
|
|
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 ) {
|
|
var href = '/w/index.php?title=Foo';
|
|
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 ) {
|
|
var href = '/wiki/Foo';
|
|
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 ) {
|
|
var href = '/wiki/%E6%B8%AC%E8%A9%A6';
|
|
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 ) {
|
|
var href = 'javascript:void(0);'; // eslint-disable-line no-script-url
|
|
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 ) {
|
|
var href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
|
|
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
|
|
var href = '/Foo';
|
|
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 );
|
|
} );
|
|
|
|
QUnit.module( 'title#isValid', {
|
|
beforeEach: function () {
|
|
window.mediaWiki.Title = {
|
|
newFromText: this.sandbox.stub().throws( 'UNIMPLEMENTED' )
|
|
};
|
|
},
|
|
afterEach: function () {
|
|
window.mediaWiki.Title = null;
|
|
}
|
|
} );
|
|
|
|
QUnit.test( 'it should return null if the title is empty', function ( assert ) {
|
|
assert.equal( isValid(), null, 'Doesn\'t accept null titles' );
|
|
assert.equal( isValid( '' ), null, 'Doesn\'t accept empty titles' );
|
|
} );
|
|
|
|
QUnit.test( 'it should return null if the title can\'t be parsed properly', function ( assert ) {
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( null );
|
|
assert.equal( isValid( 'title' ), null );
|
|
} );
|
|
|
|
QUnit.test( 'it should return null if the title can\'t be parsed properly', function ( assert ) {
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( null );
|
|
assert.equal( isValid( 'title' ), null );
|
|
assert.equal( window.mediaWiki.Title.newFromText.callCount, 1, 'mediaWiki.Title.newFromText called for parsing the title' );
|
|
} );
|
|
|
|
QUnit.test( 'it should return null if the title is not from a content namespace', function ( assert ) {
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( {
|
|
namespace: 1
|
|
} );
|
|
assert.equal( isValid( 'title', [ 5 ] ), null );
|
|
} );
|
|
|
|
QUnit.test( 'it should return the title object if the title is from a content namespace', function ( assert ) {
|
|
var mwTitle = {
|
|
namespace: 3
|
|
};
|
|
window.mediaWiki.Title.newFromText.withArgs( 'title' ).returns( mwTitle );
|
|
assert.strictEqual( isValid( 'title', [ 1, 3, 5 ] ), mwTitle );
|
|
} );
|