mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-17 21:21:35 +00:00
329df8c953
* Export parser data (date format, digits, timezone names, and messages for weekday/month names) converted to language variants * Update the parsers to try matching using every variant, in case the page is displayed in non-default variant (and to avoid problems with incomplete variant conversion) Bug: T259818 Change-Id: I04d73992cd31ce06fa79f87df0c0a53d7efc3c58
103 lines
3 KiB
JavaScript
103 lines
3 KiB
JavaScript
/* global moment */
|
|
var
|
|
testUtils = require( './testUtils.js' ),
|
|
Parser = require( 'ext.discussionTools.init' ).Parser;
|
|
|
|
QUnit.module( 'mw.dt.Parser', testUtils.newEnvironment() );
|
|
|
|
QUnit.test( '#getTimestampRegexp', function ( assert ) {
|
|
var cases = require( '../cases/timestamp-regex.json' ),
|
|
parser = new Parser( document.createElement( 'div' ) );
|
|
|
|
testUtils.overrideParserData( 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( document.createElement( 'div' ) );
|
|
|
|
testUtils.overrideParserData( require( '../data-en.json' ) );
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
var tsParser = parser.getTimestampParser( 'en', caseItem.format, null, 'UTC', { UTC: 'UTC' } ),
|
|
expectedDate = moment( caseItem.expected );
|
|
|
|
assert.ok(
|
|
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( document.createElement( 'div' ) );
|
|
|
|
testUtils.overrideParserData( 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.ok(
|
|
date.isSame( caseItem.expected ),
|
|
caseItem.message
|
|
);
|
|
assert.ok(
|
|
date.isSame( caseItem.expectedUtc ),
|
|
caseItem.message
|
|
);
|
|
} );
|
|
} );
|
|
|
|
QUnit.test( '#getThreads', function ( assert ) {
|
|
var fixture,
|
|
cases = require( '../cases/comments.json' );
|
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
var threads, parser,
|
|
$dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
|
expected = require( caseItem.expected ),
|
|
config = require( caseItem.config ),
|
|
data = require( caseItem.data );
|
|
|
|
// 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 );
|
|
testUtils.overrideParserData( data );
|
|
|
|
parser = new Parser( fixture );
|
|
threads = parser.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 ) );
|
|
} );
|
|
} );
|