mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-17 03:18:48 +00:00
fbb7d211d0
A lot of the checks are redundant. The first check still is redundant because the later two cover everything as well. But I left it for performance reasons. Additionally: * There was no test for the method. * This patch also updates a few pieces of documentation in the same class. Change-Id: I10f2944a844cc070bdc08dec6719929b383e34fa
378 lines
7.8 KiB
JavaScript
378 lines
7.8 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel MWTemplateModel tests.
|
|
*
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
( function () {
|
|
const transclusionData = {
|
|
params: {
|
|
foo: { wt: 'Foo value' },
|
|
bar: { wt: 'Bar value' },
|
|
empty: { wt: '' }
|
|
},
|
|
target: {
|
|
href: './Template:Test',
|
|
wt: 'Test'
|
|
}
|
|
};
|
|
|
|
QUnit.module( 've.dm.MWTemplateModel', ve.test.utils.mwEnvironment );
|
|
|
|
/**
|
|
* Create a new MWTemplateModel initialized with a static transclusion data fixture.
|
|
*
|
|
* @return {ve.dm.MWTemplateModel}
|
|
*/
|
|
function newTemplateModel() {
|
|
const doc = ve.dm.Document.static.newBlankDocument(),
|
|
transclusion = new ve.dm.MWTransclusionModel( doc ),
|
|
clonedTransclusionData = ve.extendObject( {}, transclusionData );
|
|
|
|
return ve.dm.MWTemplateModel.newFromData( transclusion, clonedTransclusionData );
|
|
}
|
|
|
|
/* Tests */
|
|
|
|
QUnit.test( 'hasParameter', ( assert ) => {
|
|
const template = newTemplateModel();
|
|
|
|
// All parameters are primary as long as the TemplateData documentation isn't known
|
|
assert.strictEqual( template.hasParameter( 'bar' ), true );
|
|
assert.strictEqual( template.hasParameter( 'resolved-bar' ), false );
|
|
assert.strictEqual( template.hasParameter( 'alternative-bar' ), false );
|
|
|
|
template.getSpec().setTemplateData( { params: {
|
|
'resolved-bar': { aliases: [ 'bar', 'alternative-bar' ] }
|
|
} } );
|
|
|
|
// Now "bar" and "alternative-bar" are aliases, and "resolved-bar" is the primary name
|
|
assert.strictEqual( template.hasParameter( 'bar' ), true );
|
|
assert.strictEqual( template.hasParameter( 'resolved-bar' ), true );
|
|
assert.strictEqual( template.hasParameter( 'alternative-bar' ), true );
|
|
} );
|
|
|
|
QUnit.test( 'serialize input parameters', ( assert ) => {
|
|
const templateModel = newTemplateModel(),
|
|
serializedTransclusionData = templateModel.serialize();
|
|
|
|
assert.deepEqual( serializedTransclusionData, { template: transclusionData } );
|
|
} );
|
|
|
|
QUnit.test( 'serialize changed input parameters', ( assert ) => {
|
|
const templateModel = newTemplateModel(),
|
|
newParameterModel = new ve.dm.MWParameterModel( templateModel, 'baz', 'Baz value' );
|
|
|
|
templateModel.addParameter( newParameterModel );
|
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
|
|
|
assert.deepEqual( serializedTransclusionData.template.params.baz, { wt: 'Baz value' } );
|
|
} );
|
|
|
|
// T75134
|
|
QUnit.test( 'serialize after parameter was removed', ( assert ) => {
|
|
const templateModel = newTemplateModel(),
|
|
barParam = templateModel.getParameter( 'bar' );
|
|
|
|
templateModel.removeParameter( barParam );
|
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
|
|
|
assert.deepEqual( serializedTransclusionData.template.params, { foo: { wt: 'Foo value' }, empty: { wt: '' } } );
|
|
} );
|
|
|
|
// T101075
|
|
QUnit.test( 'serialize without empty parameter not present in original parameter set', ( assert ) => {
|
|
const templateModel = newTemplateModel(),
|
|
newEmptyParam = new ve.dm.MWParameterModel( templateModel, 'new_empty', '' );
|
|
|
|
templateModel.addParameter( newEmptyParam );
|
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
|
|
|
assert.deepEqual( serializedTransclusionData, { template: transclusionData } );
|
|
} );
|
|
|
|
[
|
|
{
|
|
name: 'serialize with explicit parameter order',
|
|
spec: {
|
|
params: {
|
|
foo: {},
|
|
empty: {},
|
|
bar: {}
|
|
},
|
|
paramOrder: [ 'bar', 'foo', 'empty' ]
|
|
},
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
},
|
|
{
|
|
name: 'serialize with no parameter order',
|
|
spec: {
|
|
params: {
|
|
foo: {},
|
|
empty: {},
|
|
bar: {}
|
|
}
|
|
},
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
},
|
|
{
|
|
name: 'serialize with aliases',
|
|
spec: {
|
|
params: {
|
|
foo: {},
|
|
empty: {},
|
|
hasaliases: { aliases: [ 'bar', 'baz' ] }
|
|
}
|
|
},
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
},
|
|
{
|
|
name: 'serialize with unknown params',
|
|
spec: {
|
|
params: {
|
|
bar: {}
|
|
}
|
|
},
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
}
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
QUnit.test( name, ( assert ) => {
|
|
const templateModel = newTemplateModel();
|
|
|
|
templateModel.getSpec().setTemplateData( spec );
|
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
|
const order = Object.keys( serializedTransclusionData.template.params );
|
|
assert.deepEqual( order, expected );
|
|
} );
|
|
} );
|
|
|
|
[
|
|
{
|
|
name: 'no spec retrieved',
|
|
spec: null,
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'foo'
|
|
]
|
|
},
|
|
{
|
|
name: 'empty spec',
|
|
spec: {},
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'foo'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder and all known params',
|
|
spec: {
|
|
params: {
|
|
bar: {},
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
},
|
|
paramOrder: [ 'foo', 'empty', 'bar', 'unused' ]
|
|
},
|
|
expected: [
|
|
'foo',
|
|
'empty',
|
|
'bar'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder and some unknown params',
|
|
spec: {
|
|
params: {
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
},
|
|
paramOrder: [ 'foo', 'empty', 'unused' ]
|
|
},
|
|
expected: [
|
|
'foo',
|
|
'empty',
|
|
'bar'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder but all unknown params',
|
|
spec: {
|
|
params: {},
|
|
paramOrder: []
|
|
},
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'foo'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with no paramOrder, all known params',
|
|
spec: {
|
|
params: {
|
|
bar: {},
|
|
foo: {},
|
|
unused: {},
|
|
empty: {}
|
|
}
|
|
},
|
|
expected: [
|
|
'bar',
|
|
'foo',
|
|
'empty'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
spec: {
|
|
params: {
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
}
|
|
},
|
|
expected: [
|
|
'empty',
|
|
'foo',
|
|
'bar'
|
|
]
|
|
}
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
QUnit.test( 'getOrderedParameterNames: ' + name, ( assert ) => {
|
|
const templateModel = newTemplateModel();
|
|
if ( spec !== null ) {
|
|
templateModel.getSpec().setTemplateData( spec );
|
|
}
|
|
assert.deepEqual( templateModel.getOrderedParameterNames(), expected );
|
|
} );
|
|
} );
|
|
|
|
[
|
|
{
|
|
name: 'no spec retrieved',
|
|
spec: null,
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'foo'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder and all known params',
|
|
spec: {
|
|
params: {
|
|
bar: {},
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
},
|
|
paramOrder: [ 'foo', 'empty', 'unused', 'bar' ]
|
|
},
|
|
expected: [
|
|
'foo',
|
|
'empty',
|
|
'unused',
|
|
'bar'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder and some unknown params',
|
|
spec: {
|
|
params: {
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
},
|
|
paramOrder: [ 'foo', 'empty', 'unused' ]
|
|
},
|
|
expected: [
|
|
'foo',
|
|
'empty',
|
|
'unused',
|
|
'bar'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder but all unknown params',
|
|
spec: {
|
|
params: {},
|
|
paramOrder: []
|
|
},
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'foo'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with no paramOrder, all known params',
|
|
spec: {
|
|
params: {
|
|
bar: {},
|
|
foo: {},
|
|
unused: {},
|
|
empty: {}
|
|
}
|
|
},
|
|
expected: [
|
|
'bar',
|
|
'foo',
|
|
'unused',
|
|
'empty'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
spec: {
|
|
params: {
|
|
empty: {},
|
|
unused: {},
|
|
foo: {}
|
|
}
|
|
},
|
|
expected: [
|
|
'empty',
|
|
'unused',
|
|
'foo',
|
|
'bar'
|
|
]
|
|
},
|
|
{
|
|
name: 'spec with explicit paramOrder and aliases',
|
|
spec: {
|
|
params: {
|
|
empty: {},
|
|
unused: {},
|
|
hasalias: {
|
|
aliases: [ 'bar', 'baz' ]
|
|
}
|
|
},
|
|
paramOrder: [ 'hasalias', 'empty', 'unused' ]
|
|
},
|
|
expected: [
|
|
'bar',
|
|
'empty',
|
|
'unused',
|
|
'foo'
|
|
]
|
|
}
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
QUnit.test( 'getAllParametersOrdered: ' + name, ( assert ) => {
|
|
const templateModel = newTemplateModel();
|
|
if ( spec !== null ) {
|
|
templateModel.getSpec().setTemplateData( spec );
|
|
}
|
|
assert.deepEqual( templateModel.getAllParametersOrdered(), expected );
|
|
} );
|
|
} );
|
|
}() );
|