2024-03-11 12:35:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-08-30 10:40:27 +00:00
|
|
|
( function () {
|
|
|
|
QUnit.module( 've.ui.MWReferenceSearchWidget (Cite)', ve.test.utils.newMwEnvironment() );
|
|
|
|
|
|
|
|
function getDocRefsMock( hasNode ) {
|
|
|
|
const listKey = 'literal/foo';
|
|
|
|
const node = hasNode ? {
|
|
|
|
getAttribute: ( name ) => {
|
|
|
|
switch ( name ) {
|
|
|
|
case 'listKey': return listKey;
|
|
|
|
default: return undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getAttributes: () => ( {} ),
|
|
|
|
getInternalItem: () => ( {} ),
|
|
|
|
getDocument: () => ( new ve.dm.Document() )
|
|
|
|
} : {};
|
|
|
|
const groups = hasNode ? {
|
|
|
|
'mwReference/': {
|
|
|
|
indexOrder: [ 0 ],
|
|
|
|
firstNodes: [ node ],
|
|
|
|
keyedNodes: { [ listKey ]: [ node ] }
|
2024-08-21 13:22:06 +00:00
|
|
|
}
|
2024-08-30 10:40:27 +00:00
|
|
|
} : {};
|
|
|
|
const docRefsMock = {
|
|
|
|
getAllGroupNames: () => ( Object.keys( groups ) ),
|
|
|
|
getIndexLabel: () => ( '1' ),
|
|
|
|
getItemNode: () => ( node ),
|
|
|
|
getGroupRefs: ( groupName ) => ( ve.dm.MWGroupReferences.static.makeGroupRefs( groups[ groupName ] ) ),
|
|
|
|
hasRefs: () => ( !!hasNode )
|
|
|
|
};
|
|
|
|
|
|
|
|
return docRefsMock;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.test( 'buildIndex', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.setDocumentRefs( getDocRefsMock() );
|
|
|
|
|
|
|
|
assert.strictEqual( widget.index, null );
|
|
|
|
widget.buildIndex();
|
|
|
|
assert.deepEqual( widget.index, [] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'buildSearchIndex when empty', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.setDocumentRefs( getDocRefsMock() );
|
|
|
|
|
|
|
|
const index = widget.buildSearchIndex();
|
|
|
|
assert.deepEqual( index, [] );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'buildSearchIndex', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.setDocumentRefs( getDocRefsMock( true ) );
|
|
|
|
|
|
|
|
const index = widget.buildSearchIndex();
|
|
|
|
assert.deepEqual( index.length, 1 );
|
2024-09-02 10:26:41 +00:00
|
|
|
assert.deepEqual( index[ 0 ].footnoteLabel, '1' );
|
2024-08-30 10:40:27 +00:00
|
|
|
assert.deepEqual( index[ 0 ].name, 'foo' );
|
2024-09-02 10:26:41 +00:00
|
|
|
assert.deepEqual( index[ 0 ].searchableText, '1 foo' );
|
2024-08-30 10:40:27 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'isIndexEmpty', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.setDocumentRefs( getDocRefsMock() );
|
|
|
|
assert.true( widget.isIndexEmpty() );
|
|
|
|
|
|
|
|
widget.setDocumentRefs( getDocRefsMock( true ) );
|
|
|
|
assert.false( widget.isIndexEmpty() );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'buildSearchResults', ( assert ) => {
|
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
2024-09-02 10:26:41 +00:00
|
|
|
widget.index = [ { searchableText: 'a', reference: 'model-a' }, { searchableText: 'b' } ];
|
2024-08-30 10:40:27 +00:00
|
|
|
|
|
|
|
assert.strictEqual( widget.getResults().getItemCount(), 0 );
|
|
|
|
const results = widget.buildSearchResults( 'A' );
|
|
|
|
assert.strictEqual( results.length, 1 );
|
|
|
|
assert.strictEqual( results[ 0 ].getData(), 'model-a' );
|
|
|
|
} );
|
|
|
|
}() );
|