mediawiki-extensions-Discus.../tests/qunit/parser.test.js
Bartosz Dziewoński 77614a2d02 tests: Fix root node / container handling
Since times immemorial, and for reasons lost to history, our test code
was adding an extra <div> wrapper before parsing the HTML used for
tests. This wasn't a problem, until now, because I want to add some
tests for T303396 that need to check that the *real* wrappers present
in some test cases are handled correctly.

Changes to test cases mostly remove a leading "0/" from serialized
ranges, corresponding to removing the extra wrapper.

Change-Id: Ia50e3590538c8cd274b02d2a937ba1a3fbb4ac89
2022-03-10 18:43:58 +01:00

92 lines
2.8 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 ).date.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 ) ).date;
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 );
$( fixture ).empty().append( testUtils.getThreadContainer( $dom ).children() );
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