mediawiki-extensions-Discus.../tests/qunit/parser.test.js
Bartosz Dziewoński 361283a332 Ship HTML test files for JS using 'packageFiles' instead of 'templates'
We originally used 'templates' because it seemed like an obvious
choice for HTML files, and because 'packageFiles' requires extra code
to include anything that isn't a .js or .json file.

However, the templates are expected to be HTML fragments rather than
whole documents, and they are parsed in a particular way that takes a
lot of code to clean up (which we needed to do, because we use the
same test files for testing PHP code).

I tried doing it in the 'packageFiles' way, and the extra code doesn't
seem that bad in comparison after all. Moreover, the 'templates'
mechanism (when used the intended way) feels vaguely deprecated in
favor of Vue.js, and I'd rather move away from it.

This makes the tests faster too (probably mostly thanks to the removal
of the clean up code) – on my machine they go from 1800ms to 1500ms.

(Simplify linearWalk tests, as we no longer need to do weird things
with document fragments to get consistent outputs in PHP and JS.)

Change-Id: I39f9b994ce5636d70fea2e935a7c87c7d56dcb26
2022-10-12 22:45:41 +00:00

119 lines
3.5 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
);
} );
} );
require( '../cases/comments.json' ).forEach( function ( caseItem ) {
var testName = '#getThreads (' + caseItem.name + ')';
// Old parser tests are currently broken
var skipTests = [
'plwiki oldparser',
'enwiki oldparser',
'ckbwiki oldparser',
'arwiki no-paragraph oldparser',
'arwiki nbsp-timezone oldparser',
'frwiki fr-unsigned oldparser',
'itwiki it-unsigned oldparser',
'srwiki sr-ec variant',
'srwiki sr-el variant',
'Accidental dt tags (old parser)',
'Single comment, heading',
'Single comment with heading',
'Manually added signature with LRM',
'Signature which is just a selflink',
'Comments inside references (old parser)',
'Link using fallback 8-bit encoding (invalid UTF-8)',
'Fake headings using \';\' syntax in wikitext (<dt> tags)',
'Signatures in funny places',
'Timestamp format switch behavior'
];
if ( skipTests.indexOf( caseItem.name ) !== -1 ) {
QUnit.skip( testName );
return;
}
QUnit.test( testName, function ( assert ) {
var dom = ve.createDocumentFromHtml( require( '../' + caseItem.dom ) ),
expected = require( caseItem.expected ),
config = require( caseItem.config ),
data = require( caseItem.data ),
title = mw.Title.newFromText( caseItem.title );
var container = testUtils.getThreadContainer( dom );
testUtils.overrideMwConfig( config );
var threadItemSet = new Parser( data ).parse( container, title );
var threads = threadItemSet.getThreads();
threads.forEach( function ( thread, i ) {
testUtils.serializeComments( thread, container );
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