mediawiki-extensions-Discus.../tests/qunit/parser.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

99 lines
2.9 KiB
JavaScript

/* global moment */
var
testUtils = require( './testUtils.js' ),
Parser = require( 'ext.discussionTools.init' ).Parser;
QUnit.module( 'mw.dt.Parser', QUnit.newMwEnvironment() );
QUnit.test( '#getTimestampRegexp', function ( assert ) {
var cases = require( '../cases/timestamp-regex.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
assert.strictEqual(
parser.getTimestampRegexp( 'en', caseItem.format, '\\d', { UTC: 'UTC' } ),
caseItem.expected,
caseItem.message
);
} );
} );
QUnit.test( '#getTimestampParser', function ( assert ) {
var cases = require( '../cases/timestamp-parser.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
var tsParser = parser.getTimestampParser( 'en', caseItem.format, null, 'UTC', { UTC: 'UTC' } ),
expectedDate = moment( caseItem.expected );
assert.true(
tsParser( caseItem.data ).isSame( expectedDate ),
caseItem.message
);
} );
} );
QUnit.test( '#getTimestampParser (at DST change)', function ( assert ) {
var cases = require( '../cases/timestamp-parser-dst.json' ),
parser = new Parser( require( '../data-en.json' ) );
cases.forEach( function ( caseItem ) {
var regexp = parser.getTimestampRegexp( 'en', caseItem.format, '\\d', caseItem.timezoneAbbrs ),
tsParser = parser.getTimestampParser( 'en', caseItem.format, null, caseItem.timezone, caseItem.timezoneAbbrs ),
date = tsParser( caseItem.sample.match( regexp ) );
assert.true(
date.isSame( caseItem.expected ),
caseItem.message
);
assert.true(
date.isSame( caseItem.expectedUtc ),
caseItem.message
);
} );
} );
QUnit.test( '#getThreads', function ( assert ) {
var cases = require( '../cases/comments.json' );
var fixture = document.getElementById( 'qunit-fixture' );
cases.forEach( function ( caseItem ) {
var $dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
expected = require( caseItem.expected ),
config = require( caseItem.config ),
data = require( caseItem.data ),
title = mw.Title.newFromText( caseItem.title );
// Remove all but the body tags from full Parsoid docs
if ( $dom.filter( 'section' ).length ) {
$dom = $( '<div>' )
.append( $dom.filter( 'section' ) )
.append( $dom.filter( 'base' ) );
}
$( fixture ).empty().append( $dom );
testUtils.overrideMwConfig( config );
var threadItemSet = new Parser( data ).parse( fixture, title );
var threads = threadItemSet.getThreads();
threads.forEach( function ( thread, i ) {
testUtils.serializeComments( thread, fixture );
assert.deepEqual(
JSON.parse( JSON.stringify( thread ) ),
expected[ i ],
caseItem.name + ' section ' + i
);
} );
// Uncomment this to get updated content for the JSON files, for copy/paste:
// console.log( JSON.stringify( threads, null, 2 ) );
} );
} );
// TODO:
// * findCommentsById
// * findCommentsByName
// * getThreadItems