mediawiki-extensions-Visual.../modules/ve-mw/tests/ui/pages/ve.ui.MWAddParameterPage.test.js
Andrew Kostka a38338259d Improve input validation for the add parameter page
This patch improves the error handling for when a user tries to add
a parameter which is either an alias of a existing parameter, the
primary name of a existing aliased parameter, or a name/alias of an
existing parameter which is shown with an override label.

The error message was modified to always refer to the conflicting
parameter using the same name that is has in the sidebar.

Example: A parameter named "Parameter B" is already present in the
sidebar under its alias "B". When a user tries to add "Parameter B",
the new error message will inform the user that the parameter they
are trying to add already exists as "B".

Bug: T285869
Change-Id: I762b72b6cf14eb8ff5fcef63b4dcb70e297050de
2021-09-13 16:58:11 +02:00

92 lines
3.7 KiB
JavaScript

QUnit.module( 've.ui.MWAddParameterPage', ve.test.utils.mwEnvironment );
QUnit.test( 'Input event handler', ( assert ) => {
const transclusion = new ve.dm.MWTransclusionModel(),
template = new ve.dm.MWTemplateModel( transclusion, {} ),
parameter = new ve.dm.MWParameterModel( template ),
page = new ve.ui.MWAddParameterPage( parameter );
page.paramInputField.setValue( ' ' );
page.onParameterNameSubmitted();
assert.deepEqual( template.getParameters(), {}, 'empty input is ignored' );
page.paramInputField.setValue( ' p1 ' );
page.onParameterNameSubmitted();
assert.ok( template.hasParameter( 'p1' ), 'input is trimmed and parameter added' );
template.getParameter( 'p1' ).setValue( 'not empty' );
page.paramInputField.setValue( 'p1' );
page.onParameterNameSubmitted();
assert.ok( template.getParameter( 'p1' ).getValue(), 'existing parameter is not replaced' );
template.getSpec().setTemplateData( { params: { documented: {} } } );
page.paramInputField.setValue( 'documented' );
page.onParameterNameSubmitted();
assert.notOk( template.hasParameter( 'documented' ), 'documented parameter is not added' );
} );
QUnit.test( 'Outline item initialization', ( assert ) => {
const transclusion = new ve.dm.MWTransclusionModel(),
template = new ve.dm.MWTemplateModel( transclusion, {} ),
parameter = new ve.dm.MWParameterModel( template ),
page = new ve.ui.MWAddParameterPage( parameter );
page.setOutlineItem( new OO.ui.OutlineOptionWidget() );
const outlineItem = page.getOutlineItem();
assert.notOk( outlineItem.$element.children().length,
'Outline item should be empty' );
// eslint-disable-next-line no-jquery/no-class-state
assert.notOk( outlineItem.$element.hasClass( 'oo-ui-outlineOptionWidget' ),
'Outline item should not be styled' );
} );
[
[ '', 0 ],
[ 'a', 0 ],
[ 'a=b', '(visualeditor-dialog-transclusion-add-param-error-forbidden-char: =)' ],
[ 'x|a=b', '(visualeditor-dialog-transclusion-add-param-error-forbidden-char: |)' ],
[ 'used', '(visualeditor-dialog-transclusion-add-param-error-exists-selected: used, used)' ],
[ 'unused', '(visualeditor-dialog-transclusion-add-param-error-exists-unselected: unused, unused)' ],
[ 'usedAlias', '(visualeditor-dialog-transclusion-add-param-error-alias: usedAlias, xLabel)' ],
[ 'unusedAlias', '(visualeditor-dialog-transclusion-add-param-error-alias: unusedAlias, y)' ],
[ 'usedAliasNoLabel', '(visualeditor-dialog-transclusion-add-param-error-alias: usedAliasNoLabel, usedAliasNoLabel)' ],
[ 'usedDeprecated', '(visualeditor-dialog-transclusion-add-param-error-exists-selected: usedDeprecated, usedDeprecated)' ],
[ 'unusedDeprecated', '(visualeditor-dialog-transclusion-add-param-error-deprecated: unusedDeprecated, unusedDeprecated)' ]
].forEach( ( [ input, expected ] ) =>
QUnit.test( 'getValidationErrors: ' + input, ( assert ) => {
const data = {
target: {},
params: {
used: {},
usedAlias: {},
usedAliasNoLabel: {},
usedDeprecated: {}
}
};
const transclusion = new ve.dm.MWTransclusionModel(),
template = ve.dm.MWTemplateModel.newFromData( transclusion, data ),
parameter = new ve.dm.MWParameterModel( template ),
page = new ve.ui.MWAddParameterPage( parameter );
template.getSpec().setTemplateData( { params: {
used: {},
unused: {},
x: { aliases: [ 'usedAlias' ], label: 'xLabel' },
y: { aliases: [ 'unusedAlias' ] },
z: { aliases: [ 'usedAliasNoLabel' ] },
usedDeprecated: { deprecated: true },
unusedDeprecated: { deprecated: true }
} } );
template.addParameter( parameter );
const errors = page.getValidationErrors( input );
assert.strictEqual( errors.length, expected ? 1 : 0 );
if ( expected ) {
assert.strictEqual( errors[ 0 ].text(), expected );
}
} )
);