/* global $ */ const jQuery = require( '../utils/jQuery' ), dom = require( '../utils/dom' ), sinon = require( 'sinon' ); let sandbox, identifyLeadParagraph; QUnit.module( 'MobileFrontend mobile.editor.overlay/identifyLeadParagraph', { beforeEach: function () { sandbox = sinon.sandbox.create(); dom.setUp( sandbox, global ); jQuery.setUp( sandbox, global ); identifyLeadParagraph = require( '../../../src/mobile.editor.overlay/identifyLeadParagraph' ); }, afterEach: function () { jQuery.tearDown(); sandbox.restore(); } } ); // Keep in sync with MoveLeadParagraphTransformTest::provideTransform() function provideCases() { const infobox = '1
', coordinates = '0;0', anotherInfobox = '2
', stackInfobox = `
${ infobox }
`, emptyStack = '
Empty
', multiStackInfobox = `
${ infobox }${ anotherInfobox }
`, paragraph = '

First paragraph with info that links to a ' + '\n Page and some more content

', emptyP = '

', // The paragraphWithWhitespacesOnly has not only whitespaces (space,new line,tab) // , but also contains a span with whitespaces paragraphWithWhitespacesOnly = '

\t' + '\n \t\r\n

', collapsibleInfobox = '
', collapsibleNotInfobox = '' + '
'; return [ [ `${ collapsibleNotInfobox }

one

`, 'p', 'Collapsible mf-infoboxes are not moved.' ], [ `${ collapsibleInfobox }

one

`, 'p', 'Collapsible infoboxes are moved.' ], [ '

one

', 'p' ], [ `${ infobox }${ paragraph }`, 'p' ], [ `${ emptyP }${ emptyP }${ infobox }${ paragraph }`, 'p:last-of-type', 'Empty paragraphs are ignored' ], [ `${ paragraphWithWhitespacesOnly }${ infobox }${ paragraph }`, 'p:last-of-type', 'T199282: lead paragraph should move when there is empty paragraph before infobox' ], [ `${ infobox }${ paragraphWithWhitespacesOnly }`, null, 'T199282: the empty paragraph should not be treated as lead paragraph' ], [ `${ paragraph }${ emptyP }${ infobox }${ paragraph }`, 'p:first-of-type', 'T188825: Infobox has to be first non-empty element' ], [ `${ stackInfobox }${ paragraph }`, 'p', 'T170006: If the infobox is wrapped in a known container it can be moved' ], [ `${ emptyStack }${ paragraph }`, 'p', 'T170006: However if no infobox inside don\'t move.' ], [ `${ infobox }${ emptyStack }${ paragraph }`, 'p', 'T170006: When a stack and an infobox, ignore mw-stack' ], [ `${ multiStackInfobox }${ paragraph }`, 'p', 'T170006: Multiple infoboxes will also be moved' ], [ `${ infobox }

${ coordinates }

First paragraph

`, 'p:last-of-type', 'Paragraph with just coordinates in it is ignored' ], [ `${ infobox }

First paragraph with ${ coordinates }

Second paragraph

`, 'p:first-of-type', 'Paragraph with coordinates in it is still the first paragraph' ], [ `${ infobox }

foo${ coordinates }

Second paragraph

`, 'p:first-of-type', 'Paragraph with non-empty nested child and coordinates in it is still the first paragraph' ], [ `${ infobox }

Lead ${ coordinates } para

Not lead

`, 'p:first-of-type', 'Paragraph with nested coordinates is still the first paragraph' ] ]; } QUnit.test( 'identifyLeadParagraph', function ( assert ) { const cases = provideCases(); for ( let i = 0; i < cases.length; i++ ) { const [ html, expectedSel, message ] = cases[i]; const $dom = $( '
' ).html( html ); const expectedNode = expectedSel ? $dom.find( expectedSel )[0] : null; assert.strictEqual( expectedNode, identifyLeadParagraph( $dom ), message ); } } );