mediawiki-extensions-Popups/tests/node-qunit/formatter.test.js
Piotr Miazga f2fbef6ec7 Implement html/rest.js gateway which handles HTML Restbase responses
Refactor existing Restbase gateway and extract shared logic into
shared Restbase provider. Also introduced new createNullModel()
which defines an empty preview model.

Additionally improve naming in new gateways/formatter so function
names are more explicity.
 * Htmlize() became formatPlainTextExtract() as it should be used
   only with plain text extracts
 * removeEllipsis() became  removeTrailingEllipsis() as it removes
   only trailing ellipsis.
 * src/gateway/index.js defines gateways by configuration name stored
   in extension.json

Bug: T165018
Change-Id: Ibe54dddfc1080e94814d1562d41e85cb6b43bfc1
Depends-On: I4f42c61b155a37c5dd42bc40034583865abd5d7a
2017-06-13 20:19:05 +02:00

95 lines
2.3 KiB
JavaScript

var $ = jQuery,
formatter = require( '../../src/formatter' );
QUnit.module( 'ext.popups.formatter', {
beforeEach: function () {
window.mediaWiki.RegExp = {
escape: this.sandbox.spy( function ( str ) {
return str.replace( /([\\{}()|.?*+\-\^$\[\]])/g, '\\$1' );
} )
};
},
afterEach: function () {
window.mediaWiki.RegExp = null;
}
} );
QUnit.test( 'Title is bold', function ( assert ) {
var cases = [
[
'Isaac Newton was born in', 'Isaac Newton',
'<b>Isaac Newton</b> was born in',
'Title as first word'
],
[
'The C* language not to be confused with C# or C', 'C*',
'The <b>C*</b> language not to be confused with C# or C',
'Title containing *'
],
[
'I like trains', 'Train',
'I like <b>train</b>s',
'Make the simple plural bold'
],
[
'Foo\'s pub is a pub in Bar', 'Foo\'s pub',
'<b>Foo\'s pub</b> is a pub in Bar',
'Correct escaping'
],
[
'\"Heroes\" is a David Bowie album', '\"Heroes\"',
'<b>\"Heroes\"</b> is a David Bowie album',
'Quotes in title'
],
[
'*Testing if Things are correctly identified', 'Things',
'*Testing if <b>Things</b> are correctly identified',
'Article that begins with asterisk'
],
[
'Testing if repeated words are not matched when repeated', 'Repeated',
'Testing if <b>repeated</b> words are not matched when repeated',
'Repeated title'
]
];
function test( extract, title, expected, msg ) {
var $div = $( '<div>' ).append(
formatter.formatPlainTextExtract( extract, title )
);
assert.equal( $div.html(), expected, msg );
}
cases.forEach( function ( case_ ) {
test( case_[ 0 ], case_[ 1 ], case_[ 2 ], case_[ 3 ] );
} );
} );
QUnit.test( 'it strips ellipsis and parentheticals', function ( assert ) {
var i, testCase, cases = [
// removeEllipsis
[ 'Extract...', 'Extract' ],
[ 'Extract.', 'Extract.' ],
[ '..Extract..', '..Extract..' ],
[ '...', '' ],
// removeParentheticals
[ 'Foo', 'Foo' ],
[ 'Foo (', 'Foo (' ],
[ 'Foo (Bar)', 'Foo' ],
[ 'Foo (Bar))', 'Foo (Bar))' ],
[ 'Foo )(Bar)', 'Foo )(Bar)' ],
[ '(Bar)', '' ]
], $div;
for ( i = 0; i < cases.length; i++ ) {
testCase = cases[ i ];
$div = $( '<div>' ).append(
formatter.formatPlainTextExtract( testCase[ 0 ], 'Test' )
);
assert.equal( $div.html(), testCase[ 1 ] );
}
} );