Merge "Streamlining the MWReferenceEditPanel QUnit test"

This commit is contained in:
jenkins-bot 2024-09-03 13:19:54 +00:00 committed by Gerrit Code Review
commit 52dc522cca
2 changed files with 99 additions and 81 deletions

View file

@ -29,11 +29,18 @@ ve.ui.MWReferenceEditPanel = function VeUiMWReferenceEditPanel( config ) {
this.$element.addClass( 've-ui-mwReferenceEditPanel' );
// Properties
this.referenceModel = null;
this.originalGroup = null;
/** @member {ve.dm.MWDocumentReferences} */
/**
* @member {ve.dm.MWDocumentReferences|null}
*/
this.docRefs = null;
/**
* @member {ve.dm.MWReferenceModel|null}
*/
this.referenceModel = null;
/**
* @member {string|null}
*/
this.originalGroup = null;
// Create content editor
this.referenceTarget = ve.init.target.createTargetWidget(

View file

@ -1,89 +1,100 @@
'use strict';
QUnit.module( 've.ui.MWReferenceEditPanel (Cite)', ve.test.utils.newMwEnvironment() );
( function () {
QUnit.module( 've.ui.MWReferenceEditPanel (Cite)', ve.test.utils.newMwEnvironment() );
function getDocRefsMock( doc, hasNode, reUse ) {
const node = new ve.dm.MWReferenceNode( {
type: 'mwReference',
attributes: {
listKey: 'literal/bar',
refGroup: 'mwReference/'
},
originalDomElementsHash: Math.random()
function getSimpleNode( doc ) {
const node = new ve.dm.MWReferenceNode( {
type: 'mwReference',
attributes: {
refGroup: 'mwReference/'
},
originalDomElementsHash: Math.random()
} );
node.setDocument( doc );
return node;
}
function getDocRefsMock( node, reUse ) {
const groupRefs = {
getRefUsages: () => ( reUse ? [ node, node ] : [] ),
getInternalModelNode: () => ( node )
};
return {
getAllGroupNames: () => ( [ 'mwReference/' ] ),
getGroupRefs: () => ( groupRefs )
};
}
QUnit.test( 'setting and getting a reference', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
const ref = new ve.dm.MWReferenceModel( new ve.dm.Document( [] ) );
editPanel.setDocumentReferences( getDocRefsMock() );
const changeHandlerSpy = sinon.spy();
editPanel.connect( null, { change: changeHandlerSpy } );
ref.setGroup( 'group' );
editPanel.setReferenceForEditing( ref );
// values setup correctly
assert.strictEqual( editPanel.originalGroup, 'group' );
assert.strictEqual( editPanel.referenceGroupInput.getValue(), 'group' );
// interface setup correctly
assert.false( editPanel.referenceGroupInput.isDisabled() );
assert.false( editPanel.reuseWarning.isVisible() );
assert.false( editPanel.extendsWarning.isVisible() );
// change handler triggered
const expectedChange = {
isModified: false,
hasContent: false
};
assert.true( changeHandlerSpy.calledWith( expectedChange ) );
// reference getter
editPanel.referenceGroupInput.setValue( '' );
assert.strictEqual( editPanel.getReferenceFromEditing().getGroup(), '' );
} );
node.setDocument( doc );
const groupRefs = {
getRefUsages: () => ( reUse ? [ node, node ] : [] ),
getInternalModelNode: () => ( hasNode ? node : undefined )
};
return {
getAllGroupNames: () => ( [ 'mwReference/' ] ),
getGroupRefs: () => ( groupRefs )
};
}
QUnit.test( 'setReferenceForEditing', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
QUnit.test( 're-used references', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
const ref = new ve.dm.MWReferenceModel( new ve.dm.Document( [] ) );
editPanel.setDocumentReferences( getDocRefsMock( null, true ) );
editPanel.setReferenceForEditing( ref );
const doc = ve.dm.citeExample.createExampleDocument( 'references' );
const ref = new ve.dm.MWReferenceModel( doc );
editPanel.setDocumentReferences( getDocRefsMock( doc ) );
// interface setup correctly
assert.true( editPanel.reuseWarning.isVisible() );
assert.false( editPanel.extendsWarning.isVisible() );
} );
const changeHandlerStub = sinon.stub();
editPanel.connect( changeHandlerStub );
QUnit.test( 'sub-references', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
const doc = new ve.dm.Document( [] );
const ref = new ve.dm.MWReferenceModel( doc );
ref.setGroup( 'group' );
editPanel.setReferenceForEditing( ref );
// does exist in the example document
ref.extendsRef = 'literal/bar';
editPanel.setDocumentReferences( getDocRefsMock( getSimpleNode( doc ) ) );
editPanel.setReferenceForEditing( ref );
assert.strictEqual( editPanel.originalGroup, 'group' );
assert.strictEqual( editPanel.referenceGroupInput.getValue(), 'group' );
assert.false( editPanel.referenceGroupInput.isDisabled() );
assert.false( editPanel.reuseWarning.isVisible() );
assert.false( editPanel.extendsWarning.isVisible() );
assert.strictEqual( editPanel.getReferenceFromEditing().getGroup(), 'group' );
assert.false( editPanel.reuseWarning.isVisible() );
assert.true( editPanel.extendsWarning.isVisible() );
assert.false( editPanel.extendsWarning.getLabel().text().indexOf( 'cite-ve-dialog-reference-missing-parent-ref' ) !== -1 );
// TODO improve node mock to check content insertion for the parent
// assert.true( editPanel.extendsWarning.getLabel().text().indexOf( 'Bar' ) !== -1 );
sinon.assert.notCalled( changeHandlerStub );
} );
// test sub ref with missing main ref
ref.extendsRef = 'literal/notexist';
editPanel.setDocumentReferences( getDocRefsMock() );
editPanel.setReferenceForEditing( ref );
QUnit.test( 're-used references', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
const doc = ve.dm.citeExample.createExampleDocument( 'references' );
const ref = new ve.dm.MWReferenceModel( doc );
editPanel.setDocumentReferences( getDocRefsMock( doc, true, true ) );
editPanel.setReferenceForEditing( ref );
assert.true( editPanel.reuseWarning.isVisible() );
assert.false( editPanel.extendsWarning.isVisible() );
} );
QUnit.test( 'sub-references', ( assert ) => {
ve.init.target.surface = { commandRegistry: { registry: {} } };
const editPanel = new ve.ui.MWReferenceEditPanel();
const doc = ve.dm.citeExample.createExampleDocument( 'references' );
const ref = new ve.dm.MWReferenceModel( doc );
// does exist in the example document
ref.extendsRef = 'literal/bar';
editPanel.setDocumentReferences( getDocRefsMock( doc, true ) );
editPanel.setReferenceForEditing( ref );
assert.false( editPanel.reuseWarning.isVisible() );
assert.true( editPanel.extendsWarning.isVisible() );
assert.false( editPanel.extendsWarning.getLabel().text().indexOf( 'cite-ve-dialog-reference-missing-parent-ref' ) !== -1 );
// TODO improve node mock to check content insertion for the parent
// assert.true( editPanel.extendsWarning.getLabel().text().indexOf( 'Bar' ) !== -1 );
// test sub ref with missing main ref
ref.extendsRef = 'literal/notexist';
editPanel.setDocumentReferences( getDocRefsMock( doc ) );
editPanel.setReferenceForEditing( ref );
assert.false( editPanel.reuseWarning.isVisible() );
assert.true( editPanel.extendsWarning.isVisible() );
assert.true( editPanel.extendsWarning.getLabel().text().indexOf( 'cite-ve-dialog-reference-missing-parent-ref' ) !== -1 );
} );
assert.false( editPanel.reuseWarning.isVisible() );
assert.true( editPanel.extendsWarning.isVisible() );
assert.true( editPanel.extendsWarning.getLabel().text().indexOf( 'cite-ve-dialog-reference-missing-parent-ref' ) !== -1 );
} );
}() );