mediawiki-extensions-Discus.../tests/qunit/modifier.test.js
Bartosz Dziewoński 8e44b43df0 Split off ThreadItemSet from CommentParser
Goal:
-----
Finishing the work from Iadb7757debe000025e52770ca51ebcf24ca8ee66
by changing CommentParser::parse() to return a data object, instead of
the whole parser.

Changes:
--------
ThreadItemSet.php:
ThreadItemSet.js:
* New data class to access the results of parsing a discussion. Most
  methods and properties are moved from CommentParser with no changes.

CommentParser.php:
Parser.js:
* parse() returns a new ThreadItemSet.
* Remove methods moved to ThreadItemSet.
* Placeholder headings are generated slightly differently, as we process
  things in a different order.
* Grouping threads and computing IDs/names is no longer lazy. We always
  needed IDs/names anyway.
* computeId() explicitly uses a ThreadItemSet to check the existing IDs
  when de-duplicating.

controller.js:
* Move the code for turning some nodes annotated by CommentFormatter
  into a ThreadItemSet (previously a Parser) from controller#init to
  ThreadItemSet.static.newFromAnnotatedNodes, and rewrite it to handle
  assigning parents/replies and recalculating legacy IDs more nicely.
* mw.dt.pageThreads is now a ThreadItemSet.

Change-Id: I49bfe019aa460651447fd383f73eafa9d7180a92
2022-02-21 16:22:32 +00:00

166 lines
4.8 KiB
JavaScript

var
testUtils = require( './testUtils.js' ),
Parser = require( 'ext.discussionTools.init' ).Parser,
modifier = require( 'ext.discussionTools.init' ).modifier;
QUnit.module( 'mw.dt.modifier', QUnit.newMwEnvironment() );
require( '../cases/modified.json' ).forEach( function ( caseItem, i ) {
// This should be one test with many cases, rather than multiple tests, but the cases are large
// enough that processing all of them at once causes timeouts in Karma test runner.
QUnit.test( '#addListItem/#removeAddedListItem case ' + i, function ( assert ) {
var fixture = document.getElementById( 'qunit-fixture' );
var 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 ),
title = mw.Title.newFromText( caseItem.title );
testUtils.overrideMwConfig( config );
$( fixture ).empty().append( expected );
var expectedHtml = fixture.innerHTML;
$( fixture ).empty().append( dom );
var reverseExpectedHtml = fixture.innerHTML;
var threadItemSet = new Parser( data ).parse( fixture, title );
var comments = threadItemSet.getCommentItems();
// 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.
var nodes = [];
comments.forEach( function ( comment ) {
var node = modifier.addListItem( comment, caseItem.replyIndentation || 'invisible' );
node.textContent = 'Reply to ' + comment.id;
nodes.push( node );
} );
// Uncomment this to get updated content for the "modified HTML" files, for copy/paste:
// console.log( fixture.innerHTML );
var actualHtml = fixture.innerHTML;
assert.strictEqual(
actualHtml,
expectedHtml,
caseItem.name
);
// Now discard the replies and verify we get the original document back.
nodes.forEach( function ( node ) {
modifier.removeAddedListItem( node );
} );
var reverseActualHtml = fixture.innerHTML;
assert.strictEqual(
reverseActualHtml,
reverseExpectedHtml,
caseItem.name + ' (discard replies)'
);
} );
} );
QUnit.test( '#addReplyLink', function ( assert ) {
var cases = require( '../cases/reply.json' ),
fixture = document.getElementById( 'qunit-fixture' );
cases.forEach( function ( caseItem ) {
var 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 ),
title = mw.Title.newFromText( caseItem.title );
testUtils.overrideMwConfig( config );
$( fixture ).empty().append( expected );
var expectedHtml = fixture.innerHTML;
$( fixture ).empty().append( dom );
var threadItemSet = new Parser( data ).parse( fixture, title );
var comments = threadItemSet.getCommentItems();
// Add a reply link to every comment.
comments.forEach( function ( comment ) {
var linkNode = document.createElement( 'a' );
linkNode.textContent = 'Reply';
linkNode.href = '#';
modifier.addReplyLink( comment, linkNode );
} );
// Uncomment this to get updated content for the "reply HTML" files, for copy/paste:
// console.log( fixture.innerHTML );
var actualHtml = fixture.innerHTML;
assert.strictEqual(
actualHtml,
expectedHtml,
caseItem.name
);
} );
} );
QUnit.test( '#unwrapList', function ( assert ) {
var cases = require( '../cases/unwrap.json' );
cases.forEach( function ( caseItem ) {
var container = document.createElement( 'div' );
container.innerHTML = caseItem.html;
modifier.unwrapList( container.childNodes[ caseItem.index || 0 ] );
assert.strictEqual(
container.innerHTML,
caseItem.expected,
caseItem.name
);
} );
} );
QUnit.test( 'isWikitextSigned', function ( assert ) {
var cases = require( '../cases/isWikitextSigned.json' );
cases.forEach( function ( caseItem ) {
assert.strictEqual(
modifier.isWikitextSigned( caseItem.wikitext ),
caseItem.expected,
caseItem.msg
);
} );
} );
QUnit.test( 'isHtmlSigned', function ( assert ) {
var cases = require( '../cases/isHtmlSigned.json' );
cases.forEach( function ( caseItem ) {
var container = document.createElement( 'div' );
container.innerHTML = caseItem.html;
assert.strictEqual(
modifier.isHtmlSigned( container ),
caseItem.expected,
caseItem.msg
);
} );
} );
QUnit.test( 'sanitizeWikitextLinebreaks', function ( assert ) {
var cases = require( '../cases/sanitize-wikitext-linebreaks.json' );
cases.forEach( function ( caseItem ) {
assert.strictEqual(
modifier.sanitizeWikitextLinebreaks( caseItem.wikitext ),
caseItem.expected,
caseItem.msg
);
} );
} );
// TODO:
// * addSiblingListItem