mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-18 12:55:51 +00:00
7bc89d6f17
Change-Id: I83c213f402c8a39c9d22dc9a6fe3fa9359e6ae37
141 lines
3.5 KiB
JavaScript
141 lines
3.5 KiB
JavaScript
( function ( mw, $ ) {
|
|
|
|
/**
|
|
* processLinks QUnit tests remain integration tests given their dependency
|
|
* on a running MediaWiki instance with properly configured variables for
|
|
* 'wgContentNamespaces', 'wgArticlePath', and other configuration variables
|
|
* accessed by `mw.Title`.
|
|
*/
|
|
|
|
QUnit.module( 'ext.popups/processLinks @integration', {
|
|
setup: function () {
|
|
this.config = new mw.Map();
|
|
this.config.set( {
|
|
wgArticlePath: '/wiki/$1',
|
|
wgContentNamespaces: [ 0 ]
|
|
} );
|
|
|
|
this.blacklist = [
|
|
'.extiw',
|
|
'.image',
|
|
'.new',
|
|
'.internal',
|
|
'.external',
|
|
'.oo-ui-buttonedElement-button',
|
|
'.cancelLink a'
|
|
];
|
|
}
|
|
} );
|
|
|
|
QUnit.test( 'it should only return eligible links', 1, function ( assert ) {
|
|
var $container = $( '<div>' ),
|
|
$cancelLink = $( '<span>', {
|
|
'class': 'cancelLink'
|
|
} );
|
|
|
|
// Add links that should be filtered.
|
|
$.each( [ 'extiw', 'new', 'external' ], function ( i, className ) {
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
title: 'link title',
|
|
'class': className,
|
|
href: '/wiki/Popups'
|
|
} ).appendTo( $container );
|
|
} );
|
|
|
|
// Add a link inside of a container.
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
title: 'link title',
|
|
href: '/wiki/Popups'
|
|
} ).appendTo( $cancelLink );
|
|
$cancelLink.appendTo( $container );
|
|
|
|
// Add a link without an href, which means the link doesn't point to a valid
|
|
// page.
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
title: 'link title'
|
|
} ).appendTo( $container );
|
|
|
|
// Add a link that's not in a content namespace.
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
title: 'foo.jpg',
|
|
href: '/wiki/File:foo.jpg'
|
|
} ).appendTo( $container );
|
|
|
|
// Add a link without a title.
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
href: '/wiki/File:foo.jpg'
|
|
} ).appendTo( $container );
|
|
|
|
// Add a link that should have a Page Preview.
|
|
$( '<a>', {
|
|
text: 'link with tooltip',
|
|
title: 'link title',
|
|
href: '/wiki/Popups'
|
|
} ).appendTo( $container );
|
|
|
|
assert.equal(
|
|
mw.popups.processLinks( $container, this.blacklist, this.config ).length,
|
|
1,
|
|
'#processLinks should have only returned the eligible link'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( 'it should get the title of local pages', function ( assert ) {
|
|
var cases,
|
|
$container = $( '<div>' ),
|
|
$processedLinks;
|
|
|
|
cases = [
|
|
[ '/wiki/Foo', 'Foo' ],
|
|
[ '/wiki/Foo#Bar', 'Foo' ],
|
|
[ '/wiki/Foo?oldid=1', undefined ],
|
|
[ '/wiki/%E6%B8%AC%E8%A9%A6', '測試' ],
|
|
[ '/w/index.php?title=Foo', 'Foo' ],
|
|
[ '/w/index.php?title=Foo#Bar', 'Foo' ],
|
|
[ '/w/Foo?title=Foo&action=edit', undefined ],
|
|
[ '/w/index.php?title=%E6%B8%AC%E8%A9%A6', '測試' ],
|
|
[ '/w/index.php?oldid=1', undefined ],
|
|
[ '/Foo', undefined ],
|
|
// eslint-disable-next-line no-script-url
|
|
[ 'javascript:void(0);', undefined ],
|
|
|
|
[ 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', undefined ]
|
|
];
|
|
|
|
$.each( cases, function ( _, testCase ) {
|
|
$( '<a>', {
|
|
href: testCase[ 0 ],
|
|
title: 'link title'
|
|
} ).appendTo( $container );
|
|
} );
|
|
|
|
$processedLinks = mw.popups.processLinks( $container, this.blacklist, this.config );
|
|
|
|
// Now let's make some assertions!
|
|
cases = $.grep( cases, function ( testCase ) {
|
|
return testCase[ 1 ];
|
|
} );
|
|
|
|
QUnit.expect( cases.length + 1 );
|
|
|
|
assert.strictEqual(
|
|
$processedLinks.length,
|
|
cases.length,
|
|
'Links with titles that can\'t be extracted are filtered.'
|
|
);
|
|
|
|
$.each( cases, function ( i, testCase ) {
|
|
assert.strictEqual(
|
|
$processedLinks.eq( i ).data( 'page-previews-title' ),
|
|
testCase[ 1 ]
|
|
);
|
|
} );
|
|
} );
|
|
|
|
}( mediaWiki, jQuery ) );
|