aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/form-reset-mixin/core.js
blob: 6041ca8a3a9c54f56aa2341754ab5c95d476382d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
define( [
	"qunit",
	"jquery",
	"lib/common",
	"lib/helper",
	"ui/widget",
	"ui/form-reset-mixin"
], function( QUnit, $, common, helper ) {
"use strict";

QUnit.module( "form-reset-mixin", {
	beforeEach: function() {
		$.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 = [];
	},

	afterEach: function() {
		delete $.ui.testWidget;
		delete $.fn.testWidget;
		return helper.moduleAfterEach.apply( this, arguments );
	}
} );

QUnit.test( "form reset", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );

	var form = $( "#main" );
	var inputs = form.find( "input" );

	inputs.testWidget();
	form.on( "reset", function() {
		setTimeout( function() {
			assert.deepEqual( $.ui.testWidget.refreshed, [ "input1", "input2", "input3", "input4" ],
				"All widgets are refreshed on form reset" );
			assert.equal( form.data( "ui-form-reset-instances" ).length, 4,
				"All widget instances are tracked against the form" );
			ready();
		} );
	} );
	form[ 0 ].reset();
} );

QUnit.test( "destroy", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );

	var form = $( "#main" );
	var inputs = form.find( "input" );

	inputs
		.testWidget()
		.eq( 1 )
			.testWidget( "destroy" );

	form.on( "reset", function() {
		setTimeout( function() {
			assert.deepEqual( $.ui.testWidget.refreshed, [ "input1", "input3", "input4" ],
				"All widgets are refreshed on form reset" );
			assert.deepEqual( form.data( "ui-form-reset-instances" ).length, 3,
				"All widget instances are tracked against the form" );
			ready();
		} );
	} );
	form[ 0 ].reset();
} );

QUnit.test( "destroy all", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );

	var form = $( "#main" );

	form.find( "input" )
		.testWidget()
		.testWidget( "destroy" );

	form.on( "reset", function() {
		setTimeout( function() {
			assert.deepEqual( $.ui.testWidget.refreshed, [], "No widgets are refreshed after destroy" );
			assert.strictEqual( form.data( "ui-form-reset-instances" ), undefined,
				"Form data is removed when the last widget instance is destroyed" );
			ready();
		} );
	} );
	form[ 0 ].reset();
} );

} );