2024-03-11 12:35:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
QUnit.module( 've.ui.MWReferenceSearchWidget (Cite)', ve.test.utils.newMwEnvironment() );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'buildIndex', ( assert ) => {
|
2024-03-11 12:35:39 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.internalList = { getNodeGroups: () => ( {} ) };
|
2024-02-22 20:07:39 +00:00
|
|
|
assert.strictEqual( widget.index, null );
|
2024-03-11 12:35:39 +00:00
|
|
|
|
|
|
|
widget.buildIndex();
|
|
|
|
assert.deepEqual( widget.index, [] );
|
|
|
|
|
|
|
|
widget.onInternalListUpdate( [ 'mwReference/' ] );
|
2024-02-22 20:07:39 +00:00
|
|
|
assert.strictEqual( widget.index, null );
|
2024-03-11 12:35:39 +00:00
|
|
|
|
|
|
|
widget.buildIndex();
|
|
|
|
assert.deepEqual( widget.index, [] );
|
|
|
|
|
|
|
|
widget.onListNodeUpdate();
|
2024-02-22 20:07:39 +00:00
|
|
|
assert.strictEqual( widget.index, null );
|
2024-03-11 12:35:39 +00:00
|
|
|
} );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'buildSearchIndex when empty', ( assert ) => {
|
2024-02-22 20:08:02 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
widget.internalList = { getNodeGroups: () => ( {} ) };
|
|
|
|
|
|
|
|
const index = widget.buildSearchIndex();
|
|
|
|
assert.deepEqual( index, [] );
|
|
|
|
} );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'buildSearchIndex with a placeholder', ( assert ) => {
|
2024-02-22 20:08:02 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
const placeholder = true;
|
|
|
|
const node = { getAttribute: () => placeholder };
|
|
|
|
const groups = { 'mwReference/': { indexOrder: [ 0 ], firstNodes: [ node ] } };
|
|
|
|
widget.internalList = { getNodeGroups: () => groups, getItemNode: () => [] };
|
|
|
|
|
|
|
|
const index = widget.buildSearchIndex();
|
|
|
|
assert.deepEqual( index, [] );
|
|
|
|
} );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'buildSearchIndex', ( assert ) => {
|
2024-02-22 20:08:02 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
|
|
|
|
// XXX: This is a regression test with a fragile setup. Please feel free to delete this test
|
|
|
|
// when you feel like it doesn't make sense to update it.
|
|
|
|
const placeholder = false;
|
|
|
|
const node = {
|
|
|
|
getDocument: () => ( {
|
|
|
|
getInternalList: () => null
|
|
|
|
} ),
|
|
|
|
getAttributes: () => ( { listKey: 'literal/foo' } ),
|
|
|
|
getAttribute: () => placeholder
|
|
|
|
};
|
|
|
|
const groups = { 'mwReference/': { indexOrder: [ 0 ], firstNodes: [ node ] } };
|
|
|
|
widget.internalList = { getNodeGroups: () => groups, getItemNode: () => [] };
|
|
|
|
|
|
|
|
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' );
|
|
|
|
} );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'isIndexEmpty', ( assert ) => {
|
2024-03-11 12:35:39 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
|
|
|
assert.true( widget.isIndexEmpty() );
|
|
|
|
|
2024-02-22 18:28:17 +00:00
|
|
|
widget.internalList = {
|
2024-03-11 12:35:39 +00:00
|
|
|
getNodeGroups: () => ( { 'mwReference/': { indexOrder: [ 0 ] } } )
|
|
|
|
};
|
|
|
|
assert.false( widget.isIndexEmpty() );
|
|
|
|
} );
|
|
|
|
|
2024-05-31 14:27:11 +00:00
|
|
|
QUnit.test( 'buildSearchResults', ( assert ) => {
|
2024-03-11 12:35:39 +00:00
|
|
|
const widget = new ve.ui.MWReferenceSearchWidget();
|
2024-02-22 20:08:02 +00:00
|
|
|
widget.index = [ { text: 'a', reference: 'model-a' }, { text: 'b' } ];
|
2024-03-11 12:35:39 +00:00
|
|
|
|
|
|
|
assert.strictEqual( widget.getResults().getItemCount(), 0 );
|
2024-02-22 20:08:02 +00:00
|
|
|
const results = widget.buildSearchResults( 'A' );
|
|
|
|
assert.strictEqual( results.length, 1 );
|
|
|
|
assert.strictEqual( results[ 0 ].getData(), 'model-a' );
|
2024-03-11 12:35:39 +00:00
|
|
|
} );
|