/*! * VisualEditor UserInterface MWWikitextStringTransferHandler tests. * * @copyright 2011-2019 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; // Random number, chosen by a fair dice roll this.randomStub = sinon.stub( Math, 'random' ).returns( 0.04 ); ve.test.utils.mwEnvironment.setup.call( this ); }, teardown: function () { this.randomStub.restore(); ve.test.utils.mwEnvironment.teardown.call( this ); } } ) ); /* 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.HashValueStore(); } 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: '
' + expectedResponse + '' } } ) ] ); } }; 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: 'some Foo text
', annotations: [ { type: 'link/mwInternal', attributes: { 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: '', annotations: [ { type: 'link/mwInternal', attributes: { 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: 'Simple template', pasteString: '{{Template}}', pasteType: 'text/plain', parsoidResponse: '