2020-11-16 18:43:00 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateModel tests.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function () {
|
2021-04-29 14:42:18 +00:00
|
|
|
const transclusionData = {
|
2020-11-16 18:43:00 +00:00
|
|
|
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() {
|
2021-04-29 14:42:18 +00:00
|
|
|
const doc = ve.dm.Document.static.newBlankDocument(),
|
2020-11-16 18:43:00 +00:00
|
|
|
transclusion = new ve.dm.MWTransclusionModel( doc ),
|
|
|
|
clonedTransclusionData = ve.extendObject( {}, transclusionData );
|
|
|
|
|
|
|
|
return ve.dm.MWTemplateModel.newFromData( transclusion, clonedTransclusionData );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize input parameters', ( assert ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const templateModel = newTemplateModel(),
|
2020-11-16 18:43:00 +00:00
|
|
|
serializedTransclusionData = templateModel.serialize();
|
|
|
|
|
|
|
|
assert.deepEqual( serializedTransclusionData, { template: transclusionData } );
|
|
|
|
} );
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize changed input parameters', ( assert ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const templateModel = newTemplateModel(),
|
|
|
|
newParameterModel = new ve.dm.MWParameterModel( templateModel, 'baz', 'Baz value' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
templateModel.addParameter( newParameterModel );
|
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
assert.deepEqual( serializedTransclusionData.template.params.baz, { wt: 'Baz value' } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// T75134
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize after parameter was removed', ( assert ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const templateModel = newTemplateModel(),
|
|
|
|
barParam = templateModel.getParameter( 'bar' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
templateModel.removeParameter( barParam );
|
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
assert.deepEqual( serializedTransclusionData.template.params, { foo: { wt: 'Foo value' }, empty: { wt: '' } } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// T101075
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize without empty parameter not present in original parameter set', ( assert ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const templateModel = newTemplateModel(),
|
|
|
|
newEmptyParam = new ve.dm.MWParameterModel( templateModel, 'new_empty', '' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
templateModel.addParameter( newEmptyParam );
|
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
assert.deepEqual( serializedTransclusionData, { template: transclusionData } );
|
|
|
|
} );
|
2021-06-18 11:01:39 +00:00
|
|
|
|
2021-06-23 10:17:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'serialize with explicit parameter order',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
bar: {}
|
|
|
|
},
|
2021-06-23 10:17:42 +00:00
|
|
|
paramOrder: [ 'bar', 'foo', 'empty' ]
|
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with no parameter order',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
bar: {}
|
|
|
|
}
|
2021-06-23 10:17:42 +00:00
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with aliases',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
hasaliases: { aliases: [ 'bar', 'baz' ] }
|
2021-06-23 10:17:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with unknown params',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
bar: {}
|
|
|
|
}
|
2021-06-23 10:17:42 +00:00
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( name, ( assert ) => {
|
|
|
|
const templateModel = newTemplateModel();
|
|
|
|
|
2021-07-01 09:00:44 +00:00
|
|
|
templateModel.getSpec().setTemplateData( spec );
|
2021-06-23 10:17:42 +00:00
|
|
|
|
|
|
|
const serializedTransclusionData = templateModel.serialize();
|
|
|
|
const order = Object.keys( serializedTransclusionData.template.params );
|
|
|
|
assert.deepEqual( order, expected );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-06-18 11:01:39 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
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: {}
|
|
|
|
},
|
2021-06-18 08:44:32 +00:00
|
|
|
paramOrder: [ 'foo', 'empty', 'bar', 'unused' ]
|
2021-06-18 11:01:39 +00:00
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'foo',
|
|
|
|
'empty',
|
|
|
|
'bar'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
},
|
2021-06-18 08:44:32 +00:00
|
|
|
paramOrder: [ 'foo', 'empty', 'unused' ]
|
2021-06-18 11:01:39 +00:00
|
|
|
},
|
|
|
|
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',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
|
|
|
'empty'
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'empty',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
|
|
|
'bar'
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( 'getOrderedParameterNames: ' + name, ( assert ) => {
|
|
|
|
const templateModel = newTemplateModel();
|
|
|
|
if ( spec !== null ) {
|
2021-07-01 09:00:44 +00:00
|
|
|
templateModel.getSpec().setTemplateData( spec );
|
2021-06-18 11:01:39 +00:00
|
|
|
}
|
|
|
|
assert.deepEqual( templateModel.getOrderedParameterNames(), expected );
|
|
|
|
} );
|
|
|
|
} );
|
2021-06-18 08:44:32 +00:00
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
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',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
|
|
|
'unused',
|
|
|
|
'empty'
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'empty',
|
2021-06-23 14:31:07 +00:00
|
|
|
'unused',
|
|
|
|
'foo',
|
|
|
|
'bar'
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
2021-06-28 10:37:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and aliases',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
hasalias: {
|
|
|
|
aliases: [ 'bar', 'baz' ]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
paramOrder: [ 'hasalias', 'empty', 'unused' ]
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
|
|
|
'unused',
|
|
|
|
'foo'
|
|
|
|
]
|
2021-06-18 08:44:32 +00:00
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( 'getAllParametersOrdered: ' + name, ( assert ) => {
|
|
|
|
const templateModel = newTemplateModel();
|
|
|
|
if ( spec !== null ) {
|
2021-07-01 09:00:44 +00:00
|
|
|
templateModel.getSpec().setTemplateData( spec );
|
2021-06-18 08:44:32 +00:00
|
|
|
}
|
|
|
|
assert.deepEqual( templateModel.getAllParametersOrdered(), expected );
|
|
|
|
} );
|
|
|
|
} );
|
2020-11-16 18:43:00 +00:00
|
|
|
}() );
|