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-07-20 21:15:03 +00:00
|
|
|
Parser = require( 'ext.discussionTools.init' ).Parser;
|
2019-10-24 11:41:25 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
QUnit.module( 'mw.dt.Parser', testUtils.newEnvironment() );
|
2019-10-24 11:41:25 +00:00
|
|
|
|
|
|
|
QUnit.test( '#getTimestampRegexp', function ( assert ) {
|
2020-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-regex.json' ),
|
|
|
|
parser = new Parser( document.createElement( 'div' ) );
|
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-09-03 20:59:33 +00:00
|
|
|
parser.getTimestampRegexp( 'en', caseItem.format, '\\d', { UTC: 'UTC' } ),
|
2020-05-08 11:03:49 +00:00
|
|
|
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-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser.json' ),
|
|
|
|
parser = new Parser( document.createElement( 'div' ) );
|
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 ) {
|
2020-09-03 20:59:33 +00:00
|
|
|
var tsParser = parser.getTimestampParser( 'en', caseItem.format, null, 'UTC', { UTC: 'UTC' } ),
|
2020-05-08 11:03:49 +00:00
|
|
|
expectedDate = moment( caseItem.expected );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
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-07-20 21:15:03 +00:00
|
|
|
var cases = require( '../cases/timestamp-parser-dst.json' ),
|
|
|
|
parser = new Parser( document.createElement( 'div' ) );
|
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 ) {
|
2020-09-03 20:59:33 +00:00
|
|
|
var regexp = parser.getTimestampRegexp( 'en', caseItem.format, '\\d', caseItem.timezoneAbbrs ),
|
|
|
|
tsParser = parser.getTimestampParser( 'en', caseItem.format, null, caseItem.timezone, caseItem.timezoneAbbrs ),
|
2020-05-08 11:03:49 +00:00
|
|
|
date = tsParser( caseItem.sample.match( regexp ) );
|
2019-10-29 21:35:43 +00:00
|
|
|
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
2020-05-08 11:03:49 +00:00
|
|
|
date.isSame( caseItem.expected ),
|
|
|
|
caseItem.message
|
2019-10-29 21:35:43 +00:00
|
|
|
);
|
2021-11-08 19:03:40 +00:00
|
|
|
assert.true(
|
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-07-20 21:15:03 +00:00
|
|
|
QUnit.test( '#getThreads', function ( assert ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var cases = require( '../cases/comments.json' );
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var fixture = document.getElementById( 'qunit-fixture' );
|
2020-05-08 11:03:49 +00:00
|
|
|
cases.forEach( function ( caseItem ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var $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
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var parser = new Parser( fixture );
|
|
|
|
var threads = parser.getThreads();
|
2019-10-24 12:59:42 +00:00
|
|
|
|
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
|
|
|
} );
|