mediawiki-extensions-Visual.../modules/ve-mw/tests/ui/datatransferhandlers/ve.ui.MWWikitextStringTransferHandler.test.js
James D. Forrester 4ce103abb9 Update VE core submodule to master (c5fddd508)
New changes:
21a5d55b8 [BREAKING CHANGE] Store metadata as block nodes in the main data list
2f9868aae Localisation updates from https://translatewiki.net.
7a224cf4b Remove <style> tags from pasteTarget ASAP
fb4e894b6 Workaround missing child selector in OOUI for menuLayout
fa4f35281 rebaser: Fix sequencing of server operations
931ee29f6 SurfaceSynchronizer: Also translate selections that are not mentioned in the change
d232b8f04 Disable undo/usurp within SurfaceSynchronizer
d1b7fc597 ve.dm.Change: Truncate stores as well as transactions on conflict
7606f684d rebaser: Only apply artificial delay to submitChange events
9473e8b42 Toolbars: Replace $.width with clientWidth/offsetWidth
abb8f2075 ve.ui.DesktopContext: Remove unused CSS
050414205 ve.ui.DesktopContext: Remove old unnecessary CSS
7b8cbb830 Failing test case for losing annotations
5447d33e4 CommentNode: Protect against call after teardown
bf98ca2b6 Rebaser: Use better hack for disabling history commands
90c07f46c Rebaser: uninitialize fully
2a59016e4 Move updatingName recursion guard out of debounce

Bug: T56299
Bug: T185532
Bug: T185544
Bug: T185599
Bug: T185716
Bug: T185747
Change-Id: I45251811e3fbb3323a25ca5cf364e4cb94aa1d01
2018-01-29 08:43:08 -08:00

256 lines
6.9 KiB
JavaScript

/*!
* VisualEditor UserInterface MWWikitextStringTransferHandler tests.
*
* @copyright 2011-2018 VisualEditor Team and others; see http://ve.mit-license.org
*/
window.MWWIKITEXT_MOCK_API = true;
QUnit.module( 've.ui.MWWikitextStringTransferHandler', QUnit.newMwEnvironment( {
setup: function () {
// Mock XHR for mw.Api()
this.server = window.MWWIKITEXT_MOCK_API ? this.sandbox.useFakeServer() : null;
ve.test.utils.mwEnvironment.setup.call( this );
},
teardown: ve.test.utils.mwEnvironment.teardown
} ) );
/* Tests */
ve.test.utils.runWikitextStringHandlerTest = function ( assert, server, string, mimeType, expectedResponse, expectedData, annotations, assertDom, msg ) {
var handler, i, j, name,
done = assert.async(),
item = ve.ui.DataTransferItem.static.newFromString( string, mimeType ),
doc = ve.dm.Document.static.newBlankDocument(),
mockSurface = {
getModel: function () {
return {
getDocument: function () {
return doc;
}
};
},
createProgress: function () {
return $.Deferred().promise();
}
};
// Preprocess the expectedData array
for ( i = 0; i < expectedData.length; i++ ) {
if ( Array.isArray( expectedData[ i ] ) ) {
for ( j = 0; j < expectedData[ i ][ 1 ].length; j++ ) {
if ( typeof expectedData[ i ][ 1 ][ j ] === 'number' ) {
expectedData[ i ][ 1 ][ j ] = annotations[ expectedData[ i ][ 1 ][ j ] ];
}
}
}
}
// Check we match the wikitext string handler
name = ve.ui.dataTransferHandlerFactory.getHandlerNameForItem( item );
assert.strictEqual( name, 'wikitextString', msg + ': triggers match function' );
// Invoke the handler
handler = ve.ui.dataTransferHandlerFactory.create( 'wikitextString', mockSurface, item );
handler.getInsertableData().done( function ( docOrData ) {
var actualData, store;
if ( docOrData instanceof ve.dm.Document ) {
actualData = docOrData.getData();
store = docOrData.getStore();
} else {
actualData = docOrData;
store = new ve.dm.IndexValueStore();
}
ve.dm.example.postprocessAnnotations( actualData, store );
if ( assertDom ) {
assert.equalLinearDataWithDom( store, actualData, expectedData, msg + ': data match (with DOM)' );
} else {
assert.equalLinearData( actualData, expectedData, msg + ': data match' );
}
done();
} );
if ( server && expectedResponse ) {
server.respond( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
visualeditor: {
result: 'success',
content: '<body lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body mw-body-content mediawiki" dir="ltr">' +
expectedResponse +
'</body>'
}
} ) ] );
}
};
QUnit.test( 'convert', function ( assert ) {
var i,
cases = [
{
msg: 'Simple link',
// Put link in the middle of text to verify that the
// start-of-line and end-or-line anchors on the heading
// identification pattern don't affect link identification
pasteString: 'some [[Foo]] text',
pasteType: 'text/plain',
parsoidResponse: '<p>some <a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a> text</p>',
annotations: [ {
type: 'link/mwInternal',
attributes: {
hrefPrefix: './',
lookupTitle: 'Foo',
normalizedTitle: 'Foo',
origTitle: 'Foo',
title: 'Foo'
}
} ],
expectedData: [
's',
'o',
'm',
'e',
' ',
[ 'F', [ 0 ] ],
[ 'o', [ 0 ] ],
[ 'o', [ 0 ] ],
' ',
't',
'e',
'x',
't',
{ type: 'internalList' },
{ type: '/internalList' }
]
},
{
msg: 'Simple link with no p-wrapping',
pasteString: '*[[Foo]]',
pasteType: 'text/plain',
parsoidResponse: '<ul><li><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></li></ul>',
annotations: [ {
type: 'link/mwInternal',
attributes: {
hrefPrefix: './',
lookupTitle: 'Foo',
normalizedTitle: 'Foo',
origTitle: 'Foo',
title: 'Foo'
}
} ],
expectedData: [
{
type: 'list',
attributes: { style: 'bullet' }
},
{ type: 'listItem' },
{
type: 'paragraph',
internal: { generated: 'wrapper' }
},
[ 'F', [ 0 ] ],
[ 'o', [ 0 ] ],
[ 'o', [ 0 ] ],
{ type: '/paragraph' },
{ type: '/listItem' },
{ type: '/list' },
{ type: 'internalList' },
{ type: '/internalList' }
]
},
{
msg: 'Headings, only RESTBase IDs stripped',
pasteString: '==heading==',
pasteType: 'text/plain',
parsoidResponse: '<h2 id="mwAB">foo</h2><h2 id="mw-meaningful-id">bar</h2>',
annotations: [],
assertDom: true,
expectedData: [
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2>foo</h2>' ).toArray() },
'f', 'o', 'o',
{ type: '/mwHeading' },
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2 id="mw-meaningful-id">bar</h2>' ).toArray() },
'b', 'a', 'r',
{ type: '/mwHeading' },
{ type: 'internalList' },
{ type: '/internalList' }
]
},
{
msg: 'Headings, parsoid fallback ids don\'t interfere with whitespace stripping',
pasteString: '== Tudnivalók ==',
pasteType: 'text/plain',
parsoidResponse: '<h2 id="Tudnivalók"><span id="Tudnival.C3.B3k" typeof="mw:FallbackId"></span> Tudnivalók </h2>',
annotations: [],
assertDom: true,
expectedData: [
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2 id="Tudnivalók"> Tudnivalók </h2>' ).toArray() },
'T', 'u', 'd', 'n', 'i', 'v', 'a', 'l', 'ó', 'k',
{ type: '/mwHeading' },
{ type: 'internalList' },
{ type: '/internalList' }
]
},
{
msg: 'Magic link (RFC)',
pasteString: 'RFC 1234',
pasteType: 'text/plain',
parsoidResponse: false,
annotations: [],
expectedData: [
{
type: 'link/mwMagic',
attributes: {
content: 'RFC 1234'
}
},
{
type: '/link/mwMagic'
}
]
},
{
msg: 'Magic link (PMID)',
pasteString: 'PMID 1234',
pasteType: 'text/plain',
parsoidResponse: false,
annotations: [],
expectedData: [
{
type: 'link/mwMagic',
attributes: {
content: 'PMID 1234'
}
},
{
type: '/link/mwMagic'
}
]
},
{
msg: 'Magic link (ISBN)',
pasteString: 'ISBN 123456789X',
pasteType: 'text/plain',
parsoidResponse: false,
annotations: [],
expectedData: [
{
type: 'link/mwMagic',
attributes: {
content: 'ISBN 123456789X'
}
},
{
type: '/link/mwMagic'
}
]
}
];
for ( i = 0; i < cases.length; i++ ) {
ve.test.utils.runWikitextStringHandlerTest(
assert, this.server, cases[ i ].pasteString, cases[ i ].pasteType, cases[ i ].parsoidResponse,
cases[ i ].expectedData, cases[ i ].annotations, cases[ i ].assertDom, cases[ i ].msg
);
}
} );