Skip to content

Commit

Permalink
Merge pull request #35 from sirbrillig/add/unit-tests
Browse files Browse the repository at this point in the history
Refactor Form and introduce JS unit tests
  • Loading branch information
westonruter authored Feb 16, 2017
2 parents 910bcc9 + 8562511 commit 9c764ad
Show file tree
Hide file tree
Showing 18 changed files with 1,923 additions and 158 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**/*.min.js
**/node_modules/**
**/vendor/**
/tests/js/lib/**
**/*.browserified.js
/bower_components/**
1 change: 1 addition & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"**/*.jsx",
"**/node_modules/**",
"**/vendor/**",
"**/tests/**",
"bower_components/**"
]
}
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
**/vendor/**
**/*.jsx
/bower_components/**
/tests/js/**
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install:
- source $DEV_LIB_PATH/travis.install.sh

script:
- npm test
- source $DEV_LIB_PATH/travis.script.sh

after_script:
Expand Down
4 changes: 1 addition & 3 deletions core-adapter-widgets/archives/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ wp.widgets.formConstructor.archives = (function() {
*
* @constructor
*/
ArchivesWidgetForm = wp.widgets.Form.extend( {
id_base: 'archives'
} );
ArchivesWidgetForm = wp.widgets.Form.extend( {} );

if ( 'undefined' !== typeof module ) {
module.exports = ArchivesWidgetForm;
Expand Down
4 changes: 1 addition & 3 deletions core-adapter-widgets/categories/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ wp.widgets.formConstructor.categories = (function() {
*
* @constructor
*/
CategoriesWidgetForm = wp.widgets.Form.extend( {
id_base: 'categories'
} );
CategoriesWidgetForm = wp.widgets.Form.extend( {} );

if ( 'undefined' !== typeof module ) {
module.exports = CategoriesWidgetForm;
Expand Down
2 changes: 1 addition & 1 deletion core-adapter-widgets/tag_cloud/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function render_form_template() {
$taxonomy_choices[ $taxonomy->name ] = $taxonomy->label;
}
$this->render_form_field_template( array(
'name' => 'taxonomy',
'field' => 'taxonomy',
'label' => __( 'Taxonomy:', 'default' ),
'type' => 'select',
'choices' => $taxonomy_choices,
Expand Down
4 changes: 2 additions & 2 deletions js-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Plugin Name: JS Widgets
* Description: The next generation of widgets in core, embracing JS for UI and powering the Widgets REST API.
* Plugin URI: https://github.com/xwp/wp-js-widgets/
* Version: 0.3.0
* Author: Weston Ruter, XWP
* Version: 0.3.0-beta
* Author: XWP
* Author URI: https://make.xwp.co/
* License: GPLv2+
*
Expand Down
30 changes: 18 additions & 12 deletions js/customize-js-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable complexity */
/* eslint consistent-this: [ "error", "control" ] */

wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-vars
wp.customize.JSWidgets = (function( wp, api, $, _ ) { // eslint-disable-line no-unused-vars
'use strict';

var component = {}, originalInitialize;
Expand All @@ -16,7 +16,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
* @returns {void}
*/
component.init = function initComponent() {
component.extendWidgetControl();
component.extendWidgetControl( api.Widgets.WidgetControl );

// Handle (re-)adding a (previously-removed) control.
api.control.bind( 'add', function( addedControl ) {
Expand Down Expand Up @@ -46,9 +46,10 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
/**
* Inject WidgetControl instances with our component.WidgetControl method overrides.
*
* @returns {void}
* @param {wp.Customize.Widgets.WidgetControl} WidgetControl The constructor function to modify
* @returns {WidgetControl} The constructor function with a modified prototype
*/
component.extendWidgetControl = function extendWidgetControl() {
component.extendWidgetControl = function extendWidgetControl( WidgetControl ) {

/**
* Initialize JS widget control.
Expand All @@ -57,7 +58,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
* @param {object} options Control options.
* @returns {void}
*/
api.Widgets.WidgetControl.prototype.initialize = function initializeWidgetControl( id, options ) {
WidgetControl.prototype.initialize = function initializeWidgetControl( id, options ) {
var control = this, isJsWidget;
isJsWidget = options.params.widget_id_base && 'undefined' !== typeof wp.widgets.formConstructor[ options.params.widget_id_base ];
if ( isJsWidget ) {
Expand All @@ -67,6 +68,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
originalInitialize.call( control, id, options );
}
};
return WidgetControl;
};

/**
Expand All @@ -83,11 +85,11 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
/**
* Initialize.
*
* @param {string} id
* @param {object} options
* @param {object} options.params
* @param {string} options.params.widget_id
* @param {string} options.params.widget_id_base
* @param {string} id The widget id
* @param {object} options The options (see below)
* @param {object} options.params The params (see below)
* @param {string} options.params.widget_id The widget id
* @param {string} options.params.widget_id_base The widget id_base
* @param {string} [options.params.type] - Must be 'widget_form'.
* @param {string} [options.params.content] - This may be supplied by addWidget, but it will not be read since the form is constructed dynamically.
* @param {string} [options.params.widget_control] - Handled internally, if supplied, an error will be thrown.
Expand Down Expand Up @@ -232,7 +234,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
* Submit the widget form via Ajax and get back the updated instance,
* along with the new widget control form to render.
*
* @param {object} [args]
* @param {object} [args] The args to update the widget (see below)
* @param {Object|null} [args.instance=null] When the model changes, the instance is sent here; otherwise, the inputs from the form are used
* @param {Function|null} [args.complete=null] Function which is called when the request finishes. Context is bound to the control. First argument is any error. Following arguments are for success.
* @returns {void}
Expand All @@ -259,6 +261,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
*
* @deprecated
* @private
* @return {void}
*/
_getInputs: function _getInputs() {
throw new Error( 'The _getInputs method should not be called for customize widget instances.' );
Expand All @@ -271,6 +274,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
*
* @deprecated
* @private
* @return {void}
*/
_getInputsSignature: function _getInputsSignature() {
throw new Error( 'The _getInputsSignature method should not be called for customize widget instances.' );
Expand All @@ -283,6 +287,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
*
* @deprecated
* @private
* @return {void}
*/
_getInputState: function _getInputState() {
throw new Error( 'The _getInputState method should not be called for customize widget instances.' );
Expand All @@ -295,6 +300,7 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-
*
* @deprecated
* @private
* @return {void}
*/
_setInputState: function _setInputState() {
throw new Error( 'The _setInputState method should not be called for customize widget instances.' );
Expand All @@ -310,4 +316,4 @@ wp.customize.JSWidgets = (function( api, $ ) { // eslint-disable-line no-unused-

return component;

})( wp.customize, jQuery );
})( wp, wp.customize, jQuery, _ );
Loading

0 comments on commit 9c764ad

Please sign in to comment.