Tobias Gritschacher has submitted this change and it was merged.
Change subject: Introducing jQuery.valueview.MockViewState
......................................................................
Introducing jQuery.valueview.MockViewState
ViewState for usage in future tests.
Also moves jQuery.valueview.ViewState into its own resource loader module.
Change-Id: I53063504c5bd29eceb57552ba2ad33a4f0738700
---
M ValueView/ValueView.resources.php
M ValueView/ValueView.tests.qunit.php
A ValueView/resources/jquery.valueview/valueview.MockViewState.js
A ValueView/tests/qunit/jquery.valueview/valueview.MockViewState.tests.js
4 files changed, 222 insertions(+), 2 deletions(-)
Approvals:
Tobias Gritschacher: Verified; Looks good to me, approved
diff --git a/ValueView/ValueView.resources.php
b/ValueView/ValueView.resources.php
index 95c39be..aee0099 100644
--- a/ValueView/ValueView.resources.php
+++ b/ValueView/ValueView.resources.php
@@ -67,6 +67,24 @@
),
),
+ 'jquery.valueview.ViewState' => $moduleTemplate + array(
+ 'scripts' => array(
+ 'jquery.valueview/valueview.ViewState.js',
+ ),
+ 'dependencies' => array(
+ 'jquery.valueview.base',
+ ),
+ ),
+
+ 'jquery.valueview.MockViewState' => $moduleTemplate + array(
+ 'scripts' => array(
+ 'jquery.valueview/valueview.MockViewState.js',
+ ),
+ 'dependencies' => array(
+ 'jquery.valueview.ViewState',
+ ),
+ ),
+
// The actual valueview (vv) widget:
'jquery.valueview.valueview' => $moduleTemplate + array(
'scripts' => array(
@@ -79,6 +97,7 @@
'dependencies' => array(
'jquery.ui.widget',
'jquery.valueview.base',
+ 'jquery.valueview.ViewState',
'jquery.valueview.experts', // because vv deals
with ExpertFactory
'jquery.valueview.experts.unsupportedvalue', //
for displaying unsupported values
'jquery.valueview.experts.emptyvalue', // for
displaying empty values
diff --git a/ValueView/ValueView.tests.qunit.php
b/ValueView/ValueView.tests.qunit.php
index c0bc370..81ebd1a 100644
--- a/ValueView/ValueView.tests.qunit.php
+++ b/ValueView/ValueView.tests.qunit.php
@@ -62,6 +62,16 @@
),
),
+ 'jquery.valueview.MockViewState.tests' => array(
+ 'scripts' => array(
+
"$bp/jquery.valueview/valueview.MockViewState.tests.js",
+ ),
+ 'dependencies' => array(
+ 'jquery.valueview.MockViewState',
+ 'qunit.parameterize',
+ ),
+ ),
+
'jquery.valueview.ExpertFactory.tests' => array(
'scripts' => array(
"$bp/jquery.valueview/valueview.ExpertFactory.tests.js",
@@ -69,9 +79,9 @@
'dependencies' => array(
'jquery.valueview.experts', // contains
ExpertFactory
'jquery.valueview.experts.mock',
- 'qunit.parameterize'
+ 'qunit.parameterize',
),
- )
+ ),
);
} );
diff --git a/ValueView/resources/jquery.valueview/valueview.MockViewState.js
b/ValueView/resources/jquery.valueview/valueview.MockViewState.js
new file mode 100644
index 0000000..fa3234d
--- /dev/null
+++ b/ValueView/resources/jquery.valueview/valueview.MockViewState.js
@@ -0,0 +1,56 @@
+/**
+ * @file
+ * @ingroup ValueView
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+jQuery.valueview.MockViewState = ( function( $, inherit, ViewState ) {
+ 'use strict';
+
+ /**
+ * Mock ViewState for usage in tests. Allows to inject the state as a
plain object.
+ *
+ * @constructor
+ * @extends jQuery.valueview.ViewState
+ * @since 0.1
+ *
+ * @param {Object} [definition={}] A plain object with the fiels
"isInEditMode", "isDisabled",
+ * "value" and "options". This will just keep a reference to the
object, so changing the
+ * object from the outside will also update the ViewState's
functions return values.
+ */
+ return inherit( 'ValueviewMockViewState', ViewState, function (
definition ) {
+ if( definition !== undefined && !$.isPlainObject( definition )
) {
+ throw new Error( 'Given definition needs to be a plain
object' );
+ }
+ this._view = definition || {};
+ }, {
+ /**
+ * @see jQuery.valueview.ViewState.isInEditMode
+ */
+ isInEditMode: function() {
+ return !!this._view.isInEditMode;
+ },
+
+ /**
+ * @see jQuery.valueview.ViewState.isDisabled
+ */
+ isDisabled: function() {
+ return !!this._view.isDisabled;
+ },
+
+ /**
+ * @see jQuery.valueview.ViewState.value
+ */
+ value: function() {
+ return this._view.value;
+ },
+
+ /**
+ * @see jQuery.valueview.ViewState.option
+ */
+ option: function( key ) {
+ return ( this._view.options || {} )[ key ];
+ }
+ } );
+
+}( jQuery, dataValues.util.inherit, jQuery.valueview.ViewState ) );
diff --git
a/ValueView/tests/qunit/jquery.valueview/valueview.MockViewState.tests.js
b/ValueView/tests/qunit/jquery.valueview/valueview.MockViewState.tests.js
new file mode 100644
index 0000000..9d80ab7
--- /dev/null
+++ b/ValueView/tests/qunit/jquery.valueview/valueview.MockViewState.tests.js
@@ -0,0 +1,135 @@
+/**
+ * @since 0.1
+ * @ingroup ValueView
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+
+( function( $, QUnit, valueview ) {
+ 'use strict';
+
+ var ViewState = valueview.ViewState,
+ MockViewState = valueview.MockViewState;
+
+ QUnit.module( 'jquery.valueview.MockViewState' );
+
+ QUnit
+ .cases( [
+ {
+ title: 'without constructor argument',
+ constructorArg: undefined,
+ isInEditMode: false,
+ isDisabled: false,
+ value: undefined,
+ optionFoo: undefined,
+ optionBar: undefined
+ }, {
+ title: 'empty object as constructor argument',
+ constructorArg: {},
+ isInEditMode: false,
+ isDisabled: false,
+ value: undefined,
+ optionFoo: undefined,
+ optionBar: undefined
+ }, {
+ title: 'fully defined object with mixed definition',
+ constructorArg: {
+ isInEditMode: true,
+ isDisabled: false,
+ value: 'foo',
+ options: {
+ foo: true,
+ bar: '42'
+ }
+ },
+ isInEditMode: true,
+ isDisabled: false,
+ value: 'foo',
+ optionFoo: true,
+ optionBar: '42'
+ }, {
+ title: 'fully defined object with incomplete/weird
definition',
+ constructorArg: {
+ isInEditMode: 'foo', // should result into true
+ isDisabled: 'xxx', // should result into true
+ options: {
+ foo: true
+ }
+ },
+ isInEditMode: true,
+ isDisabled: true,
+ value: undefined,
+ optionFoo: true,
+ optionBar: undefined
+ }
+ ] )
+ .test( 'constructor', function( params, assert ) {
+ var viewState = new MockViewState(
params.constructorArg );
+
+ assert.ok(
+ viewState instanceof MockViewState,
+ 'MockViewState has been created successfully'
+ );
+
+ assert.ok(
+ viewState instanceof ViewState,
+ 'Constructed MockViewState is instanceof
ViewState'
+ );
+ } )
+ .test( 'isInEditMode', buildMemberTestFn( 'isInEditMode' ) )
+ .test( 'isDisabled', buildMemberTestFn( 'isDisabled' ) )
+ .test( 'value', buildMemberTestFn( 'value' ) )
+ .test( 'option', function( params, assert ) {
+ var viewState = new MockViewState(
params.constructorArg );
+
+ assert.strictEqual(
+ viewState.option( 'foo' ),
+ params.optionFoo,
+ 'Option "foo" holds injected value'
+ );
+
+ assert.strictEqual(
+ viewState.option( 'bar' ),
+ params.optionBar,
+ 'Option "bar" holds injected value'
+ );
+
+ } );
+
+ /**
+ * Helper which returns a test function for a member of MockViewState.
+ *
+ * @param {string} memberName
+ * @returns {Function}
+ */
+ function buildMemberTestFn( memberName ) {
+ return function( params, assert ) {
+ var viewState = new MockViewState(
params.constructorArg );
+
+ assert.strictEqual(
+ viewState[ memberName ](),
+ params[ memberName ],
+ '"' + memberName + '" returns injected value'
+ );
+ };
+ }
+
+ QUnit.test( 'Changing state after construction', function( assert ) {
+ var state = {},
+ viewState = new MockViewState( state );
+
+ assert.ok(
+ !viewState.isInEditMode(),
+ 'MockViewState "isInEditMode" returns false after
injecting empty definition'
+ );
+
+ state.isInEditMode = true;
+
+ assert.ok(
+ viewState.isInEditMode(),
+ 'MockViewState "isInEditMode" returns true after
changing object given to constructor'
+ );
+ } );
+
+}( jQuery, QUnit, jQuery.valueview ) );
--
To view, visit https://gerrit.wikimedia.org/r/62195
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I53063504c5bd29eceb57552ba2ad33a4f0738700
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits