2020-01-21 22:01:11 +00:00
|
|
|
var
|
2020-03-19 18:43:51 +00:00
|
|
|
testUtils = require( './testUtils.js' ),
|
2020-02-25 02:10:27 +00:00
|
|
|
parser = require( 'ext.discussionTools.init' ).parser,
|
|
|
|
modifier = require( 'ext.discussionTools.init' ).modifier;
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-03-19 18:43:51 +00:00
|
|
|
QUnit.module( 'mw.dt.modifier', testUtils.newEnvironment() );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-15 00:23:50 +00:00
|
|
|
QUnit.test( '#addListItem/#removeAddedListItem', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/modified.json' ),
|
2020-05-11 14:25:01 +00:00
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var actualHtml, expectedHtml, reverseActualHtml, reverseExpectedHtml,
|
|
|
|
i, comments, nodes, node,
|
|
|
|
dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
|
|
|
expected = mw.template.get( 'test.DiscussionTools', caseItem.expected ).render(),
|
|
|
|
config = require( caseItem.config ),
|
|
|
|
data = require( caseItem.data );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
|
|
|
testUtils.overrideParserData( data );
|
2020-01-21 22:01:11 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( expected );
|
2020-01-21 22:01:11 +00:00
|
|
|
expectedHtml = fixture.innerHTML;
|
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( dom.clone() );
|
2020-02-24 21:58:51 +00:00
|
|
|
reverseExpectedHtml = fixture.innerHTML;
|
|
|
|
|
2020-01-21 22:01:11 +00:00
|
|
|
comments = parser.getComments( fixture );
|
|
|
|
parser.groupThreads( comments );
|
|
|
|
|
|
|
|
// Add a reply to every comment. Note that this inserts *all* of the replies, unlike the real
|
|
|
|
// thing, which only deals with one at a time. This isn't ideal but resetting everything after
|
|
|
|
// every reply would be super slow.
|
2020-02-24 21:58:51 +00:00
|
|
|
nodes = [];
|
2020-05-11 14:25:01 +00:00
|
|
|
for ( i = 0; i < comments.length; i++ ) {
|
|
|
|
if ( comments[ i ].type === 'heading' ) {
|
2020-01-21 22:01:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-11 14:25:01 +00:00
|
|
|
node = modifier.addListItem( comments[ i ] );
|
|
|
|
node.textContent = 'Reply to ' + comments[ i ].id;
|
2020-02-24 21:58:51 +00:00
|
|
|
nodes.push( node );
|
2020-01-21 22:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the "modified HTML" files, for copy/paste:
|
2020-01-21 22:01:11 +00:00
|
|
|
// console.log( fixture.innerHTML );
|
|
|
|
|
|
|
|
actualHtml = fixture.innerHTML.trim();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
actualHtml,
|
|
|
|
expectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name
|
2020-01-21 22:01:11 +00:00
|
|
|
);
|
2020-02-24 21:58:51 +00:00
|
|
|
|
|
|
|
// Now discard the replies and verify we get the original document back.
|
2020-05-11 14:25:01 +00:00
|
|
|
for ( i = 0; i < nodes.length; i++ ) {
|
2020-05-15 00:23:50 +00:00
|
|
|
modifier.removeAddedListItem( nodes[ i ] );
|
2020-02-24 21:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reverseActualHtml = fixture.innerHTML;
|
|
|
|
assert.strictEqual(
|
|
|
|
reverseActualHtml,
|
|
|
|
reverseExpectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name + ' (discard replies)'
|
2020-02-24 21:58:51 +00:00
|
|
|
);
|
2020-05-11 14:25:01 +00:00
|
|
|
} );
|
2020-01-21 22:01:11 +00:00
|
|
|
} );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
|
|
|
QUnit.test( '#addReplyLink', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/reply.json' ),
|
2020-05-11 14:25:01 +00:00
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var actualHtml, expectedHtml,
|
|
|
|
i, comments, linkNode,
|
|
|
|
dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
|
|
|
expected = mw.template.get( 'test.DiscussionTools', caseItem.expected ).render(),
|
|
|
|
config = require( caseItem.config ),
|
|
|
|
data = require( caseItem.data );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
|
|
|
testUtils.overrideParserData( data );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( expected );
|
2020-03-02 18:50:36 +00:00
|
|
|
expectedHtml = fixture.innerHTML;
|
|
|
|
|
2020-05-11 14:25:01 +00:00
|
|
|
$( fixture ).empty().append( dom.clone() );
|
2020-03-02 18:50:36 +00:00
|
|
|
|
|
|
|
comments = parser.getComments( fixture );
|
|
|
|
parser.groupThreads( comments );
|
|
|
|
|
|
|
|
// Add a reply link to every comment.
|
2020-05-11 14:25:01 +00:00
|
|
|
for ( i = 0; i < comments.length; i++ ) {
|
|
|
|
if ( comments[ i ].type === 'heading' ) {
|
2020-03-02 18:50:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
linkNode = document.createElement( 'a' );
|
|
|
|
linkNode.textContent = 'Reply';
|
|
|
|
linkNode.href = '#';
|
2020-05-11 14:25:01 +00:00
|
|
|
modifier.addReplyLink( comments[ i ], linkNode );
|
2020-03-02 18:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the "reply HTML" files, for copy/paste:
|
2020-03-02 18:50:36 +00:00
|
|
|
// console.log( fixture.innerHTML );
|
|
|
|
|
|
|
|
actualHtml = fixture.innerHTML.trim();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
actualHtml,
|
|
|
|
expectedHtml,
|
2020-05-11 14:25:01 +00:00
|
|
|
caseItem.name
|
2020-03-02 18:50:36 +00:00
|
|
|
);
|
2020-05-11 14:25:01 +00:00
|
|
|
} );
|
2020-03-02 18:50:36 +00:00
|
|
|
} );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
QUnit.test( '#unwrapList', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/unwrap.json' );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var container = document.createElement( 'div' );
|
|
|
|
|
|
|
|
container.innerHTML = caseItem.html;
|
2020-05-26 20:47:46 +00:00
|
|
|
modifier.unwrapList( container.childNodes[ caseItem.index || 0 ] );
|
2020-04-27 16:23:27 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
2020-06-03 12:53:36 +00:00
|
|
|
container.innerHTML,
|
2020-04-27 16:23:27 +00:00
|
|
|
caseItem.expected,
|
|
|
|
caseItem.name
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|