mediawiki-extensions-Discus.../tests/qunit/modifier.test.js

210 lines
8.2 KiB
JavaScript
Raw Normal View History

var
testUtils = require( './testUtils.js' ),
parser = require( 'ext.discussionTools.init' ).parser,
modifier = require( 'ext.discussionTools.init' ).modifier;
QUnit.module( 'mw.dt.modifier', testUtils.newEnvironment() );
QUnit.test( '#addListItem/#removeListItem', function ( assert ) {
var i, j, cases,
actualHtml, expectedHtml, reverseActualHtml, reverseExpectedHtml,
comments, nodes, node, fixture;
cases = [
{
name: 'plwiki oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-oldparser/pl-big-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-oldparser/pl-big-oldparser-modified.html' ).render(),
config: require( './data/plwiki-config.json' ),
data: require( './data/plwiki-data.json' )
},
{
name: 'plwiki parsoid',
dom: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-parsoid/pl-big-parsoid.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-parsoid/pl-big-parsoid-modified.html' ).render(),
config: require( './data/plwiki-config.json' ),
data: require( './data/plwiki-data.json' )
},
{
name: 'enwiki oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/en-big-oldparser/en-big-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/en-big-oldparser/en-big-oldparser-modified.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
},
{
name: 'enwiki parsoid',
dom: mw.template.get( 'test.DiscussionTools', 'cases/en-big-parsoid/en-big-parsoid.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/en-big-parsoid/en-big-parsoid-modified.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
Pick reply insertion point based on parser tree, not DOM tree I don't like that I had to special-case `<p>` tags (top-level comments) in this code. I feel like it should be possible to handle top-level comments and replies in a generic way, but I couldn't find a way to do it that actually worked. Notes about changes to the behavior, based on the test cases: * Given a top-level comment A, if there was a "list gap" in the replies to it: previously new replies would be incorrectly added at the location of the gap; now they are added after the last reply. (T242822) Example: "pl", comment at "08:23, 29 wrz 2018 (CEST)" * Given a top-level comment A and a reply to it B that skips an indentation level: previously new replies to A would be added with the same indentation level as B; now they are added with the indentation level of A plus one. (The old behavior wasn't a bug, and this is an accidental effect of other changes, but it seems okay.) Example: "pl", comment at "03:22, 30 wrz 2018 (CEST)" and reply at "09:43, 30 wrz 2018 (CEST)" * Given a top-level comment A, a reply to it B, and a following top-level comment C that starts at the same indentation level as B: previously new replies to A would be incorrectly added in the middle of the comment C, due to the DOM list structure; now they are added before C. (T241391) (It seems that comment C was supposed to be a multi-line reply that was wrongly indented. Unfortunately we have no way to distinguish this case from a top-level multi-line comment that just happens to start with a bullet list.) Example: "pl", comments at "03:36, 24 paź 2018 (CEST)", "08:35, 24 paź 2018 (CEST)", "17:14, 24 paź 2018 (CEST)" * In the "en" example, there are some other changes where funnily nested tags result in slightly different results with the new code. They don't look important. * In rare cases, we must split an existing list to add a reply in the right place. (Basically add `</ul>` before the reply and `<ul>` after, but it's a bit awkward in DOM terms.) Example: split-list.html, comment "aaa"; also split-list2.html (which is the result of saving the previous reply), comment "aaa" * The modifier can no longer generate DOM that is invalid HTML, fixing a FIXME in modifier.test.js (or at least, it doesn't happen in these test cases any more). Bug: T241391 Bug: T242822 Change-Id: I2a70db01e9a8916c5636bc59ea8490166966d5ec
2020-01-15 06:09:13 +00:00
},
{
name: 'arwiki no-paragraph oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-oldparser/ar-no-paragraph-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-oldparser/ar-no-paragraph-oldparser-modified.html' ).render(),
config: require( './data/arwiki-config.json' ),
data: require( './data/arwiki-data.json' )
},
{
name: 'arwiki no-paragraph parsoid',
dom: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-parsoid/ar-no-paragraph-parsoid.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-parsoid/ar-no-paragraph-parsoid-modified.html' ).render(),
config: require( './data/arwiki-config.json' ),
data: require( './data/arwiki-data.json' )
},
Pick reply insertion point based on parser tree, not DOM tree I don't like that I had to special-case `<p>` tags (top-level comments) in this code. I feel like it should be possible to handle top-level comments and replies in a generic way, but I couldn't find a way to do it that actually worked. Notes about changes to the behavior, based on the test cases: * Given a top-level comment A, if there was a "list gap" in the replies to it: previously new replies would be incorrectly added at the location of the gap; now they are added after the last reply. (T242822) Example: "pl", comment at "08:23, 29 wrz 2018 (CEST)" * Given a top-level comment A and a reply to it B that skips an indentation level: previously new replies to A would be added with the same indentation level as B; now they are added with the indentation level of A plus one. (The old behavior wasn't a bug, and this is an accidental effect of other changes, but it seems okay.) Example: "pl", comment at "03:22, 30 wrz 2018 (CEST)" and reply at "09:43, 30 wrz 2018 (CEST)" * Given a top-level comment A, a reply to it B, and a following top-level comment C that starts at the same indentation level as B: previously new replies to A would be incorrectly added in the middle of the comment C, due to the DOM list structure; now they are added before C. (T241391) (It seems that comment C was supposed to be a multi-line reply that was wrongly indented. Unfortunately we have no way to distinguish this case from a top-level multi-line comment that just happens to start with a bullet list.) Example: "pl", comments at "03:36, 24 paź 2018 (CEST)", "08:35, 24 paź 2018 (CEST)", "17:14, 24 paź 2018 (CEST)" * In the "en" example, there are some other changes where funnily nested tags result in slightly different results with the new code. They don't look important. * In rare cases, we must split an existing list to add a reply in the right place. (Basically add `</ul>` before the reply and `<ul>` after, but it's a bit awkward in DOM terms.) Example: split-list.html, comment "aaa"; also split-list2.html (which is the result of saving the previous reply), comment "aaa" * The modifier can no longer generate DOM that is invalid HTML, fixing a FIXME in modifier.test.js (or at least, it doesn't happen in these test cases any more). Bug: T241391 Bug: T242822 Change-Id: I2a70db01e9a8916c5636bc59ea8490166966d5ec
2020-01-15 06:09:13 +00:00
{
name: 'Must split a list to reply to one of the comments',
dom: mw.template.get( 'test.DiscussionTools', 'cases/split-list/split-list.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/split-list/split-list-modified.html' ).render(),
Pick reply insertion point based on parser tree, not DOM tree I don't like that I had to special-case `<p>` tags (top-level comments) in this code. I feel like it should be possible to handle top-level comments and replies in a generic way, but I couldn't find a way to do it that actually worked. Notes about changes to the behavior, based on the test cases: * Given a top-level comment A, if there was a "list gap" in the replies to it: previously new replies would be incorrectly added at the location of the gap; now they are added after the last reply. (T242822) Example: "pl", comment at "08:23, 29 wrz 2018 (CEST)" * Given a top-level comment A and a reply to it B that skips an indentation level: previously new replies to A would be added with the same indentation level as B; now they are added with the indentation level of A plus one. (The old behavior wasn't a bug, and this is an accidental effect of other changes, but it seems okay.) Example: "pl", comment at "03:22, 30 wrz 2018 (CEST)" and reply at "09:43, 30 wrz 2018 (CEST)" * Given a top-level comment A, a reply to it B, and a following top-level comment C that starts at the same indentation level as B: previously new replies to A would be incorrectly added in the middle of the comment C, due to the DOM list structure; now they are added before C. (T241391) (It seems that comment C was supposed to be a multi-line reply that was wrongly indented. Unfortunately we have no way to distinguish this case from a top-level multi-line comment that just happens to start with a bullet list.) Example: "pl", comments at "03:36, 24 paź 2018 (CEST)", "08:35, 24 paź 2018 (CEST)", "17:14, 24 paź 2018 (CEST)" * In the "en" example, there are some other changes where funnily nested tags result in slightly different results with the new code. They don't look important. * In rare cases, we must split an existing list to add a reply in the right place. (Basically add `</ul>` before the reply and `<ul>` after, but it's a bit awkward in DOM terms.) Example: split-list.html, comment "aaa"; also split-list2.html (which is the result of saving the previous reply), comment "aaa" * The modifier can no longer generate DOM that is invalid HTML, fixing a FIXME in modifier.test.js (or at least, it doesn't happen in these test cases any more). Bug: T241391 Bug: T242822 Change-Id: I2a70db01e9a8916c5636bc59ea8490166966d5ec
2020-01-15 06:09:13 +00:00
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
},
{
name: 'Must split a list to reply to one of the comments (version 2)',
dom: mw.template.get( 'test.DiscussionTools', 'cases/split-list2/split-list2.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/split-list2/split-list2-modified.html' ).render(),
Pick reply insertion point based on parser tree, not DOM tree I don't like that I had to special-case `<p>` tags (top-level comments) in this code. I feel like it should be possible to handle top-level comments and replies in a generic way, but I couldn't find a way to do it that actually worked. Notes about changes to the behavior, based on the test cases: * Given a top-level comment A, if there was a "list gap" in the replies to it: previously new replies would be incorrectly added at the location of the gap; now they are added after the last reply. (T242822) Example: "pl", comment at "08:23, 29 wrz 2018 (CEST)" * Given a top-level comment A and a reply to it B that skips an indentation level: previously new replies to A would be added with the same indentation level as B; now they are added with the indentation level of A plus one. (The old behavior wasn't a bug, and this is an accidental effect of other changes, but it seems okay.) Example: "pl", comment at "03:22, 30 wrz 2018 (CEST)" and reply at "09:43, 30 wrz 2018 (CEST)" * Given a top-level comment A, a reply to it B, and a following top-level comment C that starts at the same indentation level as B: previously new replies to A would be incorrectly added in the middle of the comment C, due to the DOM list structure; now they are added before C. (T241391) (It seems that comment C was supposed to be a multi-line reply that was wrongly indented. Unfortunately we have no way to distinguish this case from a top-level multi-line comment that just happens to start with a bullet list.) Example: "pl", comments at "03:36, 24 paź 2018 (CEST)", "08:35, 24 paź 2018 (CEST)", "17:14, 24 paź 2018 (CEST)" * In the "en" example, there are some other changes where funnily nested tags result in slightly different results with the new code. They don't look important. * In rare cases, we must split an existing list to add a reply in the right place. (Basically add `</ul>` before the reply and `<ul>` after, but it's a bit awkward in DOM terms.) Example: split-list.html, comment "aaa"; also split-list2.html (which is the result of saving the previous reply), comment "aaa" * The modifier can no longer generate DOM that is invalid HTML, fixing a FIXME in modifier.test.js (or at least, it doesn't happen in these test cases any more). Bug: T241391 Bug: T242822 Change-Id: I2a70db01e9a8916c5636bc59ea8490166966d5ec
2020-01-15 06:09:13 +00:00
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
},
{
name: 'Reply inserted inside/outside various wrapper elements',
dom: mw.template.get( 'test.DiscussionTools', 'cases/wrappers/wrappers.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/wrappers/wrappers-modified.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
},
{
name: 'Signatures in funny places',
dom: mw.template.get( 'test.DiscussionTools', 'cases/signatures-funny/signatures-funny.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/signatures-funny/signatures-funny-modified.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
}
];
fixture = document.getElementById( 'qunit-fixture' );
for ( i = 0; i < cases.length; i++ ) {
testUtils.overrideMwConfig( cases[ i ].config );
testUtils.overrideParserData( cases[ i ].data );
$( fixture ).empty().append( cases[ i ].expected );
expectedHtml = fixture.innerHTML;
$( fixture ).empty().append( cases[ i ].dom.clone() );
reverseExpectedHtml = fixture.innerHTML;
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.
nodes = [];
for ( j = 0; j < comments.length; j++ ) {
if ( comments[ j ].type === 'heading' ) {
continue;
}
Pick reply insertion point based on parser tree, not DOM tree I don't like that I had to special-case `<p>` tags (top-level comments) in this code. I feel like it should be possible to handle top-level comments and replies in a generic way, but I couldn't find a way to do it that actually worked. Notes about changes to the behavior, based on the test cases: * Given a top-level comment A, if there was a "list gap" in the replies to it: previously new replies would be incorrectly added at the location of the gap; now they are added after the last reply. (T242822) Example: "pl", comment at "08:23, 29 wrz 2018 (CEST)" * Given a top-level comment A and a reply to it B that skips an indentation level: previously new replies to A would be added with the same indentation level as B; now they are added with the indentation level of A plus one. (The old behavior wasn't a bug, and this is an accidental effect of other changes, but it seems okay.) Example: "pl", comment at "03:22, 30 wrz 2018 (CEST)" and reply at "09:43, 30 wrz 2018 (CEST)" * Given a top-level comment A, a reply to it B, and a following top-level comment C that starts at the same indentation level as B: previously new replies to A would be incorrectly added in the middle of the comment C, due to the DOM list structure; now they are added before C. (T241391) (It seems that comment C was supposed to be a multi-line reply that was wrongly indented. Unfortunately we have no way to distinguish this case from a top-level multi-line comment that just happens to start with a bullet list.) Example: "pl", comments at "03:36, 24 paź 2018 (CEST)", "08:35, 24 paź 2018 (CEST)", "17:14, 24 paź 2018 (CEST)" * In the "en" example, there are some other changes where funnily nested tags result in slightly different results with the new code. They don't look important. * In rare cases, we must split an existing list to add a reply in the right place. (Basically add `</ul>` before the reply and `<ul>` after, but it's a bit awkward in DOM terms.) Example: split-list.html, comment "aaa"; also split-list2.html (which is the result of saving the previous reply), comment "aaa" * The modifier can no longer generate DOM that is invalid HTML, fixing a FIXME in modifier.test.js (or at least, it doesn't happen in these test cases any more). Bug: T241391 Bug: T242822 Change-Id: I2a70db01e9a8916c5636bc59ea8490166966d5ec
2020-01-15 06:09:13 +00:00
node = modifier.addListItem( comments[ j ] );
node.textContent = 'Reply to ' + comments[ j ].id;
nodes.push( node );
}
// Uncomment this to get updated content for the "modified HTML" files, for copy/paste:
// console.log( fixture.innerHTML );
actualHtml = fixture.innerHTML.trim();
assert.strictEqual(
actualHtml,
expectedHtml,
cases[ i ].name
);
// Now discard the replies and verify we get the original document back.
for ( j = 0; j < nodes.length; j++ ) {
modifier.removeListItem( nodes[ j ] );
}
reverseActualHtml = fixture.innerHTML;
assert.strictEqual(
reverseActualHtml,
reverseExpectedHtml,
cases[ i ].name + ' (discard replies)'
);
}
} );
QUnit.test( '#addReplyLink', function ( assert ) {
var i, j, cases, actualHtml, expectedHtml, comments, linkNode, fixture;
cases = [
{
name: 'plwiki oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-oldparser/pl-big-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/pl-big-oldparser/pl-big-oldparser-reply.html' ).render(),
config: require( './data/plwiki-config.json' ),
data: require( './data/plwiki-data.json' )
},
{
name: 'enwiki oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/en-big-oldparser/en-big-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/en-big-oldparser/en-big-oldparser-reply.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
},
{
name: 'arwiki no-paragraph oldparser',
dom: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-oldparser/ar-no-paragraph-oldparser.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/ar-no-paragraph-oldparser/ar-no-paragraph-oldparser-reply.html' ).render(),
config: require( './data/arwiki-config.json' ),
data: require( './data/arwiki-data.json' )
},
{
name: 'Signatures in funny places',
dom: mw.template.get( 'test.DiscussionTools', 'cases/signatures-funny/signatures-funny.html' ).render(),
expected: mw.template.get( 'test.DiscussionTools', 'cases/signatures-funny/signatures-funny-reply.html' ).render(),
config: require( './data/enwiki-config.json' ),
data: require( './data/enwiki-data.json' )
}
];
fixture = document.getElementById( 'qunit-fixture' );
for ( i = 0; i < cases.length; i++ ) {
testUtils.overrideMwConfig( cases[ i ].config );
testUtils.overrideParserData( cases[ i ].data );
$( fixture ).empty().append( cases[ i ].expected );
expectedHtml = fixture.innerHTML;
$( fixture ).empty().append( cases[ i ].dom.clone() );
comments = parser.getComments( fixture );
parser.groupThreads( comments );
// Add a reply link to every comment.
for ( j = 0; j < comments.length; j++ ) {
if ( comments[ j ].type === 'heading' ) {
continue;
}
linkNode = document.createElement( 'a' );
linkNode.textContent = 'Reply';
linkNode.href = '#';
modifier.addReplyLink( comments[ j ], linkNode );
}
// Uncomment this to get updated content for the "reply HTML" files, for copy/paste:
// console.log( fixture.innerHTML );
actualHtml = fixture.innerHTML.trim();
assert.strictEqual(
actualHtml,
expectedHtml,
cases[ i ].name
);
}
} );