2015-04-23 19:11:41 +00:00
|
|
|
define( [
|
2016-04-03 16:47:12 +00:00
|
|
|
"qunit",
|
2015-04-23 19:11:41 +00:00
|
|
|
"jquery",
|
|
|
|
"lib/common",
|
2020-05-16 07:16:24 +00:00
|
|
|
"lib/helper",
|
2015-04-23 19:11:41 +00:00
|
|
|
"ui/widget",
|
|
|
|
"ui/form-reset-mixin"
|
2020-05-16 07:16:24 +00:00
|
|
|
], function( QUnit, $, common, helper ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2015-04-23 19:11:41 +00:00
|
|
|
|
2024-05-13 22:24:20 +00:00
|
|
|
QUnit.module( "form-reset-mixin", {
|
2016-04-03 16:47:12 +00:00
|
|
|
beforeEach: function() {
|
2015-04-23 19:11:41 +00:00
|
|
|
$.widget( "ui.testWidget", [ $.ui.formResetMixin, {
|
|
|
|
_create: function() {
|
|
|
|
this._bindFormResetHandler();
|
|
|
|
},
|
|
|
|
_destroy: function() {
|
|
|
|
this._unbindFormResetHandler();
|
|
|
|
},
|
|
|
|
refresh: function() {
|
|
|
|
$.ui.testWidget.refreshed.push( this.element.attr( "id" ) );
|
|
|
|
}
|
|
|
|
} ] );
|
|
|
|
|
|
|
|
$.ui.testWidget.refreshed = [];
|
|
|
|
},
|
|
|
|
|
2016-04-03 16:47:12 +00:00
|
|
|
afterEach: function() {
|
2015-04-23 19:11:41 +00:00
|
|
|
delete $.ui.testWidget;
|
|
|
|
delete $.fn.testWidget;
|
2020-05-16 07:16:24 +00:00
|
|
|
return helper.moduleAfterEach.apply( this, arguments );
|
2015-04-23 19:11:41 +00:00
|
|
|
}
|
2015-08-24 13:35:44 +00:00
|
|
|
} );
|
2015-04-23 19:11:41 +00:00
|
|
|
|
2016-04-03 16:47:12 +00:00
|
|
|
QUnit.test( "form reset", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 2 );
|
2015-04-23 19:11:41 +00:00
|
|
|
|
|
|
|
var form = $( "#main" );
|
|
|
|
var inputs = form.find( "input" );
|
|
|
|
|
|
|
|
inputs.testWidget();
|
|
|
|
form.on( "reset", function() {
|
2015-08-24 13:35:44 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 16:47:12 +00:00
|
|
|
assert.deepEqual( $.ui.testWidget.refreshed, [ "input1", "input2", "input3", "input4" ],
|
2015-04-23 19:11:41 +00:00
|
|
|
"All widgets are refreshed on form reset" );
|
2016-04-03 16:47:12 +00:00
|
|
|
assert.equal( form.data( "ui-form-reset-instances" ).length, 4,
|
2015-04-23 19:11:41 +00:00
|
|
|
"All widget instances are tracked against the form" );
|
2016-04-03 16:47:12 +00:00
|
|
|
ready();
|
2015-04-23 19:11:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
form[ 0 ].reset();
|
|
|
|
} );
|
|
|
|
|
2016-04-03 16:47:12 +00:00
|
|
|
QUnit.test( "destroy", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 2 );
|
2015-04-23 19:11:41 +00:00
|
|
|
|
|
|
|
var form = $( "#main" );
|
|
|
|
var inputs = form.find( "input" );
|
|
|
|
|
|
|
|
inputs
|
|
|
|
.testWidget()
|
|
|
|
.eq( 1 )
|
|
|
|
.testWidget( "destroy" );
|
|
|
|
|
|
|
|
form.on( "reset", function() {
|
2015-08-24 13:35:44 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 16:47:12 +00:00
|
|
|
assert.deepEqual( $.ui.testWidget.refreshed, [ "input1", "input3", "input4" ],
|
2015-04-23 19:11:41 +00:00
|
|
|
"All widgets are refreshed on form reset" );
|
2016-04-03 16:47:12 +00:00
|
|
|
assert.deepEqual( form.data( "ui-form-reset-instances" ).length, 3,
|
2015-04-23 19:11:41 +00:00
|
|
|
"All widget instances are tracked against the form" );
|
2016-04-03 16:47:12 +00:00
|
|
|
ready();
|
2015-04-23 19:11:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
form[ 0 ].reset();
|
|
|
|
} );
|
|
|
|
|
2016-04-03 16:47:12 +00:00
|
|
|
QUnit.test( "destroy all", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 2 );
|
2015-04-23 19:11:41 +00:00
|
|
|
|
|
|
|
var form = $( "#main" );
|
|
|
|
|
|
|
|
form.find( "input" )
|
|
|
|
.testWidget()
|
|
|
|
.testWidget( "destroy" );
|
|
|
|
|
|
|
|
form.on( "reset", function() {
|
2015-08-24 13:35:44 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 16:47:12 +00:00
|
|
|
assert.deepEqual( $.ui.testWidget.refreshed, [], "No widgets are refreshed after destroy" );
|
|
|
|
assert.strictEqual( form.data( "ui-form-reset-instances" ), undefined,
|
2015-04-23 19:11:41 +00:00
|
|
|
"Form data is removed when the last widget instance is destroyed" );
|
2016-04-03 16:47:12 +00:00
|
|
|
ready();
|
2015-04-23 19:11:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
form[ 0 ].reset();
|
|
|
|
} );
|
|
|
|
|
|
|
|
} );
|