2019-10-29 21:35:43 +00:00
|
|
|
/* global moment */
|
2019-10-24 11:41:25 +00:00
|
|
|
var
|
2020-03-19 18:43:51 +00:00
|
|
|
testUtils = require( './testUtils.js' ),
|
2020-02-25 02:10:27 +00:00
|
|
|
parser = require( 'ext.discussionTools.init' ).parser;
|
2019-10-24 11:41:25 +00:00
|
|
|
|
2020-03-19 18:43:51 +00:00
|
|
|
QUnit.module( 'mw.dt.parser', testUtils.newEnvironment() );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
|
|
|
QUnit.test( '#getTimestampRegexp', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/timestamp-regex.json' );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
2020-05-18 20:07:00 +00:00
|
|
|
testUtils.overrideParserData( require( '../data-en.json' ) );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2019-10-24 11:41:25 +00:00
|
|
|
assert.strictEqual(
|
2020-05-08 11:03:49 +00:00
|
|
|
parser.getTimestampRegexp( caseItem.format, '\\d', { UTC: 'UTC' } ),
|
|
|
|
caseItem.expected,
|
|
|
|
caseItem.message
|
2019-10-24 11:41:25 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-24 11:41:25 +00:00
|
|
|
} );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2019-10-29 21:35:43 +00:00
|
|
|
QUnit.test( '#getTimestampParser', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser.json' );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-18 20:07:00 +00:00
|
|
|
testUtils.overrideParserData( require( '../data-en.json' ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var tsParser = parser.getTimestampParser( caseItem.format, null, 'UTC', { UTC: 'UTC' } ),
|
|
|
|
expectedDate = moment( caseItem.expected );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
|
|
|
assert.ok(
|
2020-05-08 11:03:49 +00:00
|
|
|
tsParser( caseItem.data ).isSame( expectedDate ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-29 21:35:43 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#getTimestampParser (at DST change)', function ( assert ) {
|
2020-05-18 20:07:00 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser-dst.json' );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-18 20:07:00 +00:00
|
|
|
testUtils.overrideParserData( require( '../data-en.json' ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var regexp = parser.getTimestampRegexp( caseItem.format, '\\d', caseItem.timezoneAbbrs ),
|
|
|
|
tsParser = parser.getTimestampParser( caseItem.format, null, caseItem.timezone, caseItem.timezoneAbbrs ),
|
|
|
|
date = tsParser( caseItem.sample.match( regexp ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
|
|
|
assert.ok(
|
2020-05-08 11:03:49 +00:00
|
|
|
date.isSame( caseItem.expected ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
|
|
|
assert.ok(
|
2020-05-08 11:03:49 +00:00
|
|
|
date.isSame( caseItem.expectedUtc ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-29 21:35:43 +00:00
|
|
|
} );
|
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
QUnit.test( '#getComments/#groupThreads', function ( assert ) {
|
2020-05-08 11:03:49 +00:00
|
|
|
var fixture,
|
2020-05-18 20:07:00 +00:00
|
|
|
cases = require( '../cases/comments.json' );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var comments, threads,
|
2020-05-13 20:19:43 +00:00
|
|
|
$dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
2020-05-08 11:03:49 +00:00
|
|
|
expected = require( caseItem.expected ),
|
|
|
|
config = require( caseItem.config ),
|
|
|
|
data = require( caseItem.data );
|
|
|
|
|
2020-05-13 20:19:43 +00:00
|
|
|
// 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 );
|
2020-05-08 11:03:49 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
|
|
|
testUtils.overrideParserData( data );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
|
|
|
comments = parser.getComments( fixture );
|
|
|
|
threads = parser.groupThreads( comments );
|
|
|
|
|
2020-05-08 11:03:49 +00:00
|
|
|
threads.forEach( function ( thread, i ) {
|
|
|
|
testUtils.serializeComments( thread, fixture );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
2020-05-08 11:03:49 +00:00
|
|
|
JSON.parse( JSON.stringify( thread ) ),
|
|
|
|
expected[ i ],
|
|
|
|
caseItem.name + ' section ' + i
|
2019-10-24 12:59:42 +00:00
|
|
|
);
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-11-24 16:28:01 +00:00
|
|
|
|
2020-03-04 20:25:35 +00:00
|
|
|
// Uncomment this to get updated content for the JSON files, for copy/paste:
|
2019-11-24 16:28:01 +00:00
|
|
|
// console.log( JSON.stringify( threads, null, 2 ) );
|
2020-05-08 11:03:49 +00:00
|
|
|
} );
|
2019-10-24 12:59:42 +00:00
|
|
|
} );
|
2020-03-04 20:25:35 +00:00
|
|
|
|
|
|
|
QUnit.test( '#getTranscludedFrom', function ( assert ) {
|
2020-05-15 00:51:36 +00:00
|
|
|
var fixture,
|
2020-05-18 20:07:00 +00:00
|
|
|
cases = require( '../cases/transcluded.json' );
|
2020-03-04 20:25:35 +00:00
|
|
|
|
|
|
|
fixture = document.getElementById( 'qunit-fixture' );
|
|
|
|
|
2020-05-15 00:51:36 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var comments, transcludedFrom,
|
|
|
|
$dom = mw.template.get( 'test.DiscussionTools', caseItem.dom ).render(),
|
|
|
|
expected = require( caseItem.expected ),
|
|
|
|
config = require( caseItem.config ),
|
|
|
|
data = require( caseItem.data );
|
|
|
|
|
|
|
|
$( fixture ).empty().append( $dom );
|
2020-03-19 17:37:19 +00:00
|
|
|
mw.libs.ve.unwrapParsoidSections( fixture );
|
|
|
|
|
2020-05-15 00:51:36 +00:00
|
|
|
testUtils.overrideMwConfig( config );
|
|
|
|
testUtils.overrideParserData( data );
|
2020-03-04 20:25:35 +00:00
|
|
|
|
|
|
|
comments = parser.getComments( fixture );
|
|
|
|
parser.groupThreads( comments );
|
|
|
|
|
|
|
|
transcludedFrom = {};
|
2020-05-15 00:51:36 +00:00
|
|
|
comments.forEach( function ( comment ) {
|
|
|
|
if ( comment.id ) {
|
|
|
|
transcludedFrom[ comment.id ] = parser.getTranscludedFrom( comment );
|
2020-03-04 20:25:35 +00:00
|
|
|
}
|
2020-05-15 00:51:36 +00:00
|
|
|
} );
|
2020-03-04 20:25:35 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
transcludedFrom,
|
2020-05-15 00:51:36 +00:00
|
|
|
expected,
|
|
|
|
caseItem.name
|
2020-03-04 20:25:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Uncomment this to get updated content for the JSON files, for copy/paste:
|
|
|
|
// console.log( JSON.stringify( transcludedFrom, null, 2 ) );
|
2020-05-15 00:51:36 +00:00
|
|
|
} );
|
2020-03-04 20:25:35 +00:00
|
|
|
} );
|
2020-05-19 19:01:03 +00:00
|
|
|
|
|
|
|
QUnit.test( '#getAuthors', function ( assert ) {
|
|
|
|
var cases = require( '../cases/authors.json' );
|
|
|
|
|
|
|
|
cases.forEach( function ( caseItem ) {
|
|
|
|
var authors = parser.getAuthors( caseItem.thread );
|
|
|
|
assert.deepEqual(
|
|
|
|
authors,
|
|
|
|
caseItem.expected
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|