mediawiki-extensions-Visual.../modules/ve-mw/tests/dm/ve.dm.MWTransclusionModel.test.js
Thiemo Kreuz 55beccbb79 Tests for some trivial but critical template model methods
Not only do we want to make sure getUniquePartId() always starts
at 0 and increments correctly, it should return a number (and
not e.g. "part_0").

I realize the getTitle() test is also testing functionality from
mw.libs.ve.… (can be found in the file ve.utils.parsoid.js).
This is intentional. What we care about at this point is not a
library but the very specific functionality of a very specific
method we use quite a lot in code we touch.

Bug: T289560
Change-Id: I43c1d00dacf27a68b16f62ecca4adda22f437391
2021-08-27 12:49:06 +00:00

130 lines
3.4 KiB
JavaScript

/*!
* VisualEditor DataModel MWTransclusionModel tests.
*
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
*/
( function () {
QUnit.module( 've.dm.MWTransclusionModel', QUnit.newMwEnvironment( {
beforeEach() {
// Mock XHR for mw.Api()
this.server = this.sandbox.useFakeServer();
this.server.respondImmediately = true;
ve.test.utils.mwEnvironment.beforeEach.call( this );
},
afterEach() {
ve.test.utils.mwEnvironment.afterEach.call( this );
}
} ) );
function runAddPartTest( assert, name, response, server, callback ) {
const doc = ve.dm.Document.static.newBlankDocument(),
transclusion = new ve.dm.MWTransclusionModel( doc ),
part = ve.dm.MWTemplateModel.newFromName( transclusion, name ),
done = assert.async();
server.respondWith( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( response ) ] );
transclusion.addPart( part )
.then( () => {
callback( transclusion );
} )
.always( () => {
done();
} );
}
QUnit.test( 'getUniquePartId', function ( assert ) {
const transclusion = new ve.dm.MWTransclusionModel();
assert.strictEqual( transclusion.getUniquePartId(), 0 );
assert.strictEqual( transclusion.getUniquePartId(), 1 );
assert.strictEqual( transclusion.getUniquePartId(), 2 );
} );
QUnit.test( 'fetch template part data', function ( assert ) {
const response = {
batchcomplete: '',
pages: {
1331311: {
title: 'Template:Test',
description: { en: 'MWTransclusionModel template test' },
params: {
test: {
label: { en: 'Test param' },
type: 'string',
description: { en: 'This is a test param' },
required: false,
suggested: false,
example: null,
deprecated: false,
aliases: [],
autovalue: null,
default: null
}
},
paramOrder: [ 'test' ],
format: 'inline',
sets: [],
maps: {}
}
}
};
runAddPartTest( assert, 'Test', response, this.server, ( transclusion ) => {
const parts = transclusion.getParts(),
spec = parts[ 0 ].getSpec();
assert.strictEqual( parts.length, 1 );
assert.strictEqual( spec.getDescription( 'en' ), 'MWTransclusionModel template test' );
assert.strictEqual( spec.getParameterLabel( 'test', 'en' ), 'Test param' );
} );
} );
// T243868
QUnit.test( 'fetch part data for parameterized template with no TemplateData', function ( assert ) {
const response = {
batchcomplete: '',
pages: {
1331311: {
title: 'Template:NoData',
notemplatedata: true,
params: {
foo: [],
bar: []
}
}
}
};
runAddPartTest( assert, 'NoData', response, this.server, ( transclusion ) => {
const parts = transclusion.getParts(),
spec = parts[ 0 ].getSpec();
assert.strictEqual( parts.length, 1 );
assert.deepEqual( spec.getKnownParameterNames(), [ 'foo', 'bar' ] );
} );
} );
QUnit.test( 'fetch part data for template with no TemplateData and no params', function ( assert ) {
const response = {
batchcomplete: '',
pages: {
1331311: {
title: 'Template:NoParams',
notemplatedata: true,
params: []
}
}
};
runAddPartTest( assert, 'NoParams', response, this.server, ( transclusion ) => {
const parts = transclusion.getParts(),
spec = parts[ 0 ].getSpec();
assert.strictEqual( parts.length, 1 );
assert.deepEqual( spec.getKnownParameterNames(), [] );
} );
} );
}() );