jenkins-bot has submitted this change and it was merged. Change subject: Added basic listview QUnit tests ......................................................................
Added basic listview QUnit tests Change-Id: I72b3cf0c48d2c00fcb25615e1f0acf4afc990b7b --- M lib/WikibaseLib.hooks.php A lib/tests/qunit/jquery.wikibase/jquery.wikibase.listview.tests.js 2 files changed, 296 insertions(+), 3 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/WikibaseLib.hooks.php b/lib/WikibaseLib.hooks.php index a05a5b8..6f041a0 100644 --- a/lib/WikibaseLib.hooks.php +++ b/lib/WikibaseLib.hooks.php @@ -155,15 +155,22 @@ ), ); - $testModules['qunit']['jquery.wikibase.wbtooltip.tests'] = array( + $testModules['qunit']['jquery.wikibase.listview.tests'] = $moduleBase + array( + 'scripts' => array( + 'tests/qunit/jquery.wikibase/jquery.wikibase.listview.tests.js', + ), + 'dependencies' => array( + 'jquery.wikibase.listview', + ), + ); + + $testModules['qunit']['jquery.wikibase.wbtooltip.tests'] = $moduleBase + array( 'scripts' => array( 'tests/qunit/jquery.wikibase/jquery.wikibase.wbtooltip.tests.js', ), 'dependencies' => array( 'jquery.wikibase.wbtooltip', ), - 'localBasePath' => __DIR__, - 'remoteExtPath' => 'Wikibase/lib', ); return true; diff --git a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.listview.tests.js b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.listview.tests.js new file mode 100644 index 0000000..390d719 --- /dev/null +++ b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.listview.tests.js @@ -0,0 +1,286 @@ +/** + * @since 0.4 + * @file + * @ingroup WikibaseLib + * @licence GNU GPL v2+ + * @author H. Snater < mediaw...@snater.com > + */ +( function( $, mw, wb ) { + 'use strict'; + + /** + * Initializes a listview widget suitable for testing. + * + * @param {*[]} [value] + * @return {jQuery} + */ + function createListview( value ) { + var $node = $( '<div/>' ).addClass( 'test_listview' ); + + $node.listview( { + listItemAdapter: new $.wikibase.listview.ListItemAdapter( { + listItemWidget: $.wikibasetest.valuewidget, + listItemWidgetValueAccessor: 'value', + newItemOptionsFn: function( value ) { + return { value: value || null }; + } + } ), + value: ( value ) ? value : null + } ); + + return $node; + } + + QUnit.module( 'jquery.wikibase.listview', window.QUnit.newWbEnvironment( { + setup: function() { + /** + * Basic widget to be used as list item. + */ + $.widget( 'wikibasetest.valuewidget', { + value: function( value ) { + if( value ) { + this.options.value = value; + } + return this.options.value; + } + } ); + }, + teardown: function() { + $( '.test_listview' ).each( function( i, node ) { + var $node = $( node ), + listview = $node.data( 'listview' ); + + if( listview ) { + listview.destroy(); + } + + $node.remove(); + } ); + + delete( $.wikibasetest.valuewidget ); + } + } ) ); + + QUnit.test( 'Initialize and destroy', function( assert ) { + + /** + * Runs assertions testing initialization and destruction of a listview widget initialized + * with the values passed. + * + * @param {Object} assert + * @param {string[]} [values] + */ + function testInitAndDestroy( assert, values ) { + var $node = createListview( values ), + listview = $node.data( 'listview' ), + valuesLength = ( values ) ? values.length : 0; + + assert.ok( + listview !== undefined, + 'Instantiated listview widget.' + ); + + assert.equal( + listview.items().length, + valuesLength, + 'Listview does not feature any items.' + ); + + assert.equal( + listview.value().length, + valuesLength, + 'Listview does not return an array of values.' + ); + + assert.equal( + listview.nonEmptyItems().length, + valuesLength, + 'Listview does not feature any items not empty.' + ); + + listview.destroy(); + + assert.ok( + $node.data( 'listview' ) === undefined, + 'Destroyed listview.' + ); + + assert.strictEqual( + $node.children().length, + valuesLength, + 'Reset listview node to initial state.' + ); + + $node.remove(); + } + + testInitAndDestroy( assert ); + testInitAndDestroy( assert, ['a'] ); + testInitAndDestroy( assert, ['a', 'b'] ); + } ); + + QUnit.test( 'value()', function( assert ) { + var $node = createListview(), + listview = $node.data( 'listview' ), + values = [ + ['a', 'b', 'c'], + ['d'] + ]; + + assert.strictEqual( + listview.value().length, + 0, + 'Listview is empty.' + ); + + listview.value( values[0] ); + + assert.strictEqual( + listview.value().length, + 3, + 'Set value via value().' + ); + + listview.value( values[1] ); + + assert.strictEqual( + listview.value().length, + 1, + 'Overwrote value via value().' + ); + + listview.value( [] ); + + assert.strictEqual( + listview.value().length, + 0, + 'Emptied listview via value().' + ); + } ); + + QUnit.test( 'addItem() and removeItem()', function( assert ) { + var $node = createListview(), + listview = $node.data( 'listview' ), + values = ['a', 'b', 'c'], + listItems = []; + + for( var i = 0; i < values.length; i++ ) { + listview.addItem( values[i] ); + + assert.strictEqual( + listview.items().length, + ( i + 1 ), + 'Added item #' + i + ' to the list.' + ); + + assert.equal( + listview.listItemAdapter().liValue( listview.items().eq( i ) ), + values[i], + 'Retrieved listview\'s list item node for list item #' + i + '.' + ); + + listItems.push( listview.items().eq( i ) ); + } + + listview.removeItem( listItems[2] ); + + assert.strictEqual( + listview.items().length, + 2, + 'Removed third item from the list.' + ); + + listview.removeItem( listItems[0] ); + + assert.strictEqual( + listview.items().length, + 1, + 'Removed first item from the list.' + ); + + listview.removeItem( listItems[1] ); + + assert.strictEqual( + listview.items().length, + 0, + 'Removed second item from the list emptying the list.' + ); + } ); + + QUnit.test( 'enterNewItem()', function( assert ) { + var $node = createListview(), + listview = $node.data( 'listview' ), + values = ['a', 'b', 'c']; + + listview.enterNewItem(); + + assert.strictEqual( + listview.items().length, + 1, + 'Inserted new (empty) item.' + ); + + assert.strictEqual( + listview.nonEmptyItems().length, + 0, + 'Listview features no non-empty items.' + ); + + listview.addItem( values[0] ); + + assert.strictEqual( + listview.items().length, + 2, + 'Inserted a non-empty item.' + ); + + assert.strictEqual( + listview.nonEmptyItems().length, + 1, + 'Listview features one non-empty item.' + ); + + listview.enterNewItem(); + + assert.strictEqual( + listview.items().length, + 3, + 'Inserted another new (empty) item.' + ); + + assert.strictEqual( + listview.nonEmptyItems().length, + 1, + 'Listview features one non-empty item.' + ); + + listview.removeItem( listview.items().eq( 0 ) ); + + assert.strictEqual( + listview.items().length, + 2, + 'Removed first empty item.' + ); + + assert.strictEqual( + listview.nonEmptyItems().length, + 1, + 'Listview features one non-empty item.' + ); + + listview.removeItem( listview.items().eq( 0 ) ); + + assert.strictEqual( + listview.items().length, + 1, + 'Removed non-empty item.' + ); + + assert.strictEqual( + listview.nonEmptyItems().length, + 0, + 'Listview features no non-empty item.' + ); + } ); + +} )( jQuery, mediaWiki, wikibase ); -- To view, visit https://gerrit.wikimedia.org/r/80191 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I72b3cf0c48d2c00fcb25615e1f0acf4afc990b7b Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de> Gerrit-Reviewer: Daniel Werner <daniel.wer...@wikimedia.de> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits