aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/accordion/events.js
blob: 5b852513229f5e5338f966f2dd2d1bf3ea85e878 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
define( [
	"qunit",
	"jquery",
	"./helper",
	"ui/widgets/accordion"
], function( QUnit, $, testHelper ) {

var beforeAfterEach = testHelper.beforeAfterEach,
	state = testHelper.state;

QUnit.module( "accordion: events", beforeAfterEach() );

QUnit.test( "create", function( assert ) {
	assert.expect( 10 );

	var element = $( "#list1" ),
		headers = element.children( "h3" ),
		contents = headers.next();

	element.accordion( {
		create: function( event, ui ) {
			assert.equal( ui.header.length, 1, "header length" );
			assert.strictEqual( ui.header[ 0 ], headers[ 0 ], "header" );
			assert.equal( ui.panel.length, 1, "panel length" );
			assert.strictEqual( ui.panel[ 0 ], contents[ 0 ], "panel" );
		}
	} );
	element.accordion( "destroy" );

	element.accordion( {
		active: 2,
		create: function( event, ui ) {
			assert.equal( ui.header.length, 1, "header length" );
			assert.strictEqual( ui.header[ 0 ], headers[ 2 ], "header" );
			assert.equal( ui.panel.length, 1, "panel length" );
			assert.strictEqual( ui.panel[ 0 ], contents[ 2 ], "panel" );
		}
	} );
	element.accordion( "destroy" );

	element.accordion( {
		active: false,
		collapsible: true,
		create: function( event, ui ) {
			assert.equal( ui.header.length, 0, "header length" );
			assert.equal( ui.panel.length, 0, "panel length" );
		}
	} );
	element.accordion( "destroy" );
} );

QUnit.test( "beforeActivate", function( assert ) {
	assert.expect( 38 );
	var element = $( "#list1" ).accordion( {
			active: false,
			collapsible: true
		} ),
		headers = element.find( ".ui-accordion-header" ),
		content = element.find( ".ui-accordion-content" );

	element.one( "accordionbeforeactivate", function( event, ui ) {
		assert.ok( !( "originalEvent" in event ) );
		assert.equal( ui.oldHeader.length, 0 );
		assert.equal( ui.oldPanel.length, 0 );
		assert.equal( ui.newHeader.length, 1 );
		assert.strictEqual( ui.newHeader[ 0 ], headers[ 0 ] );
		assert.equal( ui.newPanel.length, 1 );
		assert.strictEqual( ui.newPanel[ 0 ], content[ 0 ] );
		state( assert, element, 0, 0, 0 );
	} );
	element.accordion( "option", "active", 0 );
	state( assert, element, 1, 0, 0 );

	element.one( "accordionbeforeactivate", function( event, ui ) {
		assert.equal( event.originalEvent.type, "click" );
		assert.equal( ui.oldHeader.length, 1 );
		assert.strictEqual( ui.oldHeader[ 0 ], headers[ 0 ] );
		assert.equal( ui.oldPanel.length, 1 );
		assert.strictEqual( ui.oldPanel[ 0 ], content[ 0 ] );
		assert.equal( ui.newHeader.length, 1 );
		assert.strictEqual( ui.newHeader[ 0 ], headers[ 1 ] );
		assert.equal( ui.newPanel.length, 1 );
		assert.strictEqual( ui.newPanel[ 0 ], content[ 1 ] );
		state( assert, element, 1, 0, 0 );
	} );
	headers.eq( 1 ).trigger( "click" );
	state( assert, element, 0, 1, 0 );

	element.one( "accordionbeforeactivate", function( event, ui ) {
		assert.ok( !( "originalEvent" in event ) );
		assert.equal( ui.oldHeader.length, 1 );
		assert.strictEqual( ui.oldHeader[ 0 ], headers[ 1 ] );
		assert.equal( ui.oldPanel.length, 1 );
		assert.strictEqual( ui.oldPanel[ 0 ], content[ 1 ] );
		assert.equal( ui.newHeader.length, 0 );
		assert.equal( ui.newPanel.length, 0 );
		state( assert, element, 0, 1, 0 );
	} );
	element.accordion( "option", "active", false );
	state( assert, element, 0, 0, 0 );

	element.one( "accordionbeforeactivate", function( event, ui ) {
		assert.ok( !( "originalEvent" in event ) );
		assert.equal( ui.oldHeader.length, 0 );
		assert.equal( ui.oldPanel.length, 0 );
		assert.equal( ui.newHeader.length, 1 );
		assert.strictEqual( ui.newHeader[ 0 ], headers[ 2 ] );
		assert.equal( ui.newPanel.length, 1 );
		assert.strictEqual( ui.newPanel[ 0 ], content[ 2 ] );
		event.preventDefault();
		state( assert, element, 0, 0, 0 );
	} );
	element.accordion( "option", "active", 2 );
	state( assert, element, 0, 0, 0 );
} );

QUnit.test( "activate", function( assert ) {
	assert.expect( 21 );
	var element = $( "#list1" ).accordion( {
			active: false,
			collapsible: true
		} ),
		headers = element.find( ".ui-accordion-header" ),
		content = element.find( ".ui-accordion-content" );

	element.one( "accordionactivate", function( event, ui ) {
		assert.equal( ui.oldHeader.length, 0 );
		assert.equal( ui.oldPanel.length, 0 );
		assert.equal( ui.newHeader.length, 1 );
		assert.strictEqual( ui.newHeader[ 0 ], headers[ 0 ] );
		assert.equal( ui.newPanel.length, 1 );
		assert.strictEqual( ui.newPanel[ 0 ], content[ 0 ] );
	} );
	element.accordion( "option", "active", 0 );

	element.one( "accordionactivate", function( event, ui ) {
		assert.equal( ui.oldHeader.length, 1 );
		assert.strictEqual( ui.oldHeader[ 0 ], headers[ 0 ] );
		assert.equal( ui.oldPanel.length, 1 );
		assert.strictEqual( ui.oldPanel[ 0 ], content[ 0 ] );
		assert.equal( ui.newHeader.length, 1 );
		assert.strictEqual( ui.newHeader[ 0 ], headers[ 1 ] );
		assert.equal( ui.newPanel.length, 1 );
		assert.strictEqual( ui.newPanel[ 0 ], content[ 1 ] );
	} );
	headers.eq( 1 ).trigger( "click" );

	element.one( "accordionactivate", function( event, ui ) {
		assert.equal( ui.oldHeader.length, 1 );
		assert.strictEqual( ui.oldHeader[ 0 ], headers[ 1 ] );
		assert.equal( ui.oldPanel.length, 1 );
		assert.strictEqual( ui.oldPanel[ 0 ], content[ 1 ] );
		assert.equal( ui.newHeader.length, 0 );
		assert.equal( ui.newPanel.length, 0 );
	} );
	element.accordion( "option", "active", false );

	// Prevent activation
	element.one( "accordionbeforeactivate", function( event ) {
		assert.ok( true );
		event.preventDefault();
	} );
	element.one( "accordionactivate", function() {
		assert.ok( false );
	} );
	element.accordion( "option", "active", 1 );
} );

} );