mediawiki-extensions-Cite/tests/qunit/ve-cite/ve.ui.MWReferenceSearchWidget.test.js
Adam Wight 6e03d2cafa Moving ref group knowledge into a dedicated data structure
Pushes per-group knowledge down into a structured object and give it
an interface, separated from the singleton cache across all groups.

Also changes the behavior of orphaned subrefs so that they're still
rendered as subrefs, with an error placeholder where the parent
should be.

Bug: T372871
Change-Id: I84e679a8365f3fbfabaf344d99f56f6d069c0776
2024-08-26 09:26:15 +00:00

101 lines
2.9 KiB
JavaScript

'use strict';
QUnit.module( 've.ui.MWReferenceSearchWidget (Cite)', ve.test.utils.newMwEnvironment() );
function getInternalListMock( hasNode ) {
const listKey = 'literal/foo';
const node = hasNode ? {
getAttribute: ( name ) => {
switch ( name ) {
case 'listKey': return listKey;
default: return undefined;
}
},
getAttributes: () => ( {} )
} : {};
const groups = hasNode ? {
'mwReference/': {
indexOrder: [ 0 ],
firstNodes: [ node ],
keyedNodes: { [ listKey ]: [ node ] }
}
} : {};
const docRefsMock = {
getAllGroupNames: () => ( Object.keys( groups ) ),
getIndexLabel: () => ( '1' ),
getItemNode: () => ( node ),
getGroupRefs: ( groupName ) => ( ve.dm.MWGroupReferences.static.makeGroupRefs( groups[ groupName ] ) )
};
const docMock = {
getStorage: () => ( docRefsMock ),
getOriginalDocument: () => ( null )
};
const mockInternalList = {
getDocument: () => ( docMock ),
getNodeGroups: () => ( groups ),
getItemNode: () => ( node ),
getNodeGroup: ( groupName ) => ( groups[ groupName ] )
};
docMock.getInternalList = () => ( mockInternalList );
node.getDocument = () => ( docMock );
return mockInternalList;
}
QUnit.test( 'buildIndex', ( assert ) => {
const widget = new ve.ui.MWReferenceSearchWidget();
widget.internalList = getInternalListMock();
assert.strictEqual( widget.index, null );
widget.buildIndex();
assert.deepEqual( widget.index, [] );
widget.onInternalListUpdate( [ 'mwReference/' ] );
assert.strictEqual( widget.index, null );
widget.buildIndex();
assert.deepEqual( widget.index, [] );
widget.onListNodeUpdate();
assert.strictEqual( widget.index, null );
} );
QUnit.test( 'buildSearchIndex when empty', ( assert ) => {
const widget = new ve.ui.MWReferenceSearchWidget();
widget.internalList = getInternalListMock();
const index = widget.buildSearchIndex();
assert.deepEqual( index, [] );
} );
QUnit.test( 'buildSearchIndex', ( assert ) => {
const widget = new ve.ui.MWReferenceSearchWidget();
widget.internalList = getInternalListMock( true );
const index = widget.buildSearchIndex();
assert.deepEqual( index.length, 1 );
assert.deepEqual( index[ 0 ].citation, '1' );
assert.deepEqual( index[ 0 ].name, 'foo' );
assert.deepEqual( index[ 0 ].text, '1 foo' );
} );
QUnit.test( 'isIndexEmpty', ( assert ) => {
const widget = new ve.ui.MWReferenceSearchWidget();
assert.true( widget.isIndexEmpty() );
widget.internalList = {
getNodeGroups: () => ( { 'mwReference/': { indexOrder: [ 0 ] } } )
};
assert.false( widget.isIndexEmpty() );
} );
QUnit.test( 'buildSearchResults', ( assert ) => {
const widget = new ve.ui.MWReferenceSearchWidget();
widget.index = [ { text: 'a', reference: 'model-a' }, { text: 'b' } ];
assert.strictEqual( widget.getResults().getItemCount(), 0 );
const results = widget.buildSearchResults( 'A' );
assert.strictEqual( results.length, 1 );
assert.strictEqual( results[ 0 ].getData(), 'model-a' );
} );