aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/tabs/tabs_events.js
blob: f9b1755f778a7e7f0bbb30e5d01f6e093ce78950 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(function( $ ) {

var state = TestHelpers.tabs.state;

module( "tabs: events" );

test( "create", function() {
	expect( 10 );

	var element = $( "#tabs1" ),
		tabs = element.find( "ul li" ),
		panels = element.children( "div" );

	element.tabs({
		create: function( event, ui ) {
			equal( ui.tab.length, 1, "tab length" );
			strictEqual( ui.tab[ 0 ], tabs[ 0 ], "tab" );
			equal( ui.panel.length, 1, "panel length" );
			strictEqual( ui.panel[ 0 ], panels[ 0 ], "panel" );
		}
	});
	element.tabs( "destroy" );

	element.tabs({
		active: 2,
		create: function( event, ui ) {
			equal( ui.tab.length, 1, "tab length" );
			strictEqual( ui.tab[ 0 ], tabs[ 2 ], "tab" );
			equal( ui.panel.length, 1, "panel length" );
			strictEqual( ui.panel[ 0 ], panels[ 2 ], "panel" );
		}
	});
	element.tabs( "destroy" );

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

test( "beforeActivate", function() {
	expect( 38 );

	var element = $( "#tabs1" ).tabs({
			active: false,
			collapsible: true
		}),
		tabs = element.find( ".ui-tabs-nav li" ),
		anchors = tabs.find( ".ui-tabs-anchor" ),
		panels = element.find( ".ui-tabs-panel" );

	// from collapsed
	element.one( "tabsbeforeactivate", function( event, ui ) {
		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.oldTab.length, 0, "oldTab length" );
		equal( ui.oldPanel.length, 0, "oldPanel length" );
		equal( ui.newTab.length, 1, "newTab length" );
		strictEqual( ui.newTab[ 0 ], tabs[ 0 ], "newTab" );
		equal( ui.newPanel.length, 1, "newPanel length" );
		strictEqual( ui.newPanel[ 0 ], panels[ 0 ], "newPanel" );
		state( element, 0, 0, 0 );
	});
	element.tabs( "option", "active", 0 );
	state( element, 1, 0, 0 );

	// switching tabs
	element.one( "tabsbeforeactivate", function( event, ui ) {
		equal( event.originalEvent.type, "click", "originalEvent" );
		equal( ui.oldTab.length, 1, "oldTab length" );
		strictEqual( ui.oldTab[ 0 ], tabs[ 0 ], "oldTab" );
		equal( ui.oldPanel.length, 1, "oldPanel length" );
		strictEqual( ui.oldPanel[ 0 ], panels[ 0 ], "oldPanel" );
		equal( ui.newTab.length, 1, "newTab length" );
		strictEqual( ui.newTab[ 0 ], tabs[ 1 ], "newTab" );
		equal( ui.newPanel.length, 1, "newPanel length" );
		strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
		state( element, 1, 0, 0 );
	});
	anchors.eq( 1 ).click();
	state( element, 0, 1, 0 );

	// collapsing
	element.one( "tabsbeforeactivate", function( event, ui ) {
		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.oldTab.length, 1, "oldTab length" );
		strictEqual( ui.oldTab[ 0 ], tabs[ 1 ], "oldTab" );
		equal( ui.oldPanel.length, 1, "oldPanel length" );
		strictEqual( ui.oldPanel[ 0 ], panels[ 1 ], "oldPanel" );
		equal( ui.newTab.length, 0, "newTab length" );
		equal( ui.newPanel.length, 0, "newPanel length" );
		state( element, 0, 1, 0 );
	});
	element.tabs( "option", "active", false );
	state( element, 0, 0, 0 );

	// prevent activation
	element.one( "tabsbeforeactivate", function( event, ui ) {
		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.oldTab.length, 0, "oldTab length" );
		equal( ui.oldPanel.length, 0, "oldTab" );
		equal( ui.newTab.length, 1, "newTab length" );
		strictEqual( ui.newTab[ 0 ], tabs[ 1 ], "newTab" );
		equal( ui.newPanel.length, 1, "newPanel length" );
		strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
		event.preventDefault();
		state( element, 0, 0, 0 );
	});
	element.tabs( "option", "active", 1 );
	state( element, 0, 0, 0 );
});

test( "activate", function() {
	expect( 30 );

	var element = $( "#tabs1" ).tabs({
			active: false,
			collapsible: true
		}),
		tabs = element.find( ".ui-tabs-nav li" ),
		anchors = element.find( ".ui-tabs-anchor" ),
		panels = element.find( ".ui-tabs-panel" );

	// from collapsed
	element.one( "tabsactivate", function( event, ui ) {
		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.oldTab.length, 0, "oldTab length" );
		equal( ui.oldPanel.length, 0, "oldPanel length" );
		equal( ui.newTab.length, 1, "newTab length" );
		strictEqual( ui.newTab[ 0 ], tabs[ 0 ], "newTab" );
		equal( ui.newPanel.length, 1, "newPanel length" );
		strictEqual( ui.newPanel[ 0 ], panels[ 0 ], "newPanel" );
		state( element, 1, 0, 0 );
	});
	element.tabs( "option", "active", 0 );
	state( element, 1, 0, 0 );

	// switching tabs
	element.one( "tabsactivate", function( event, ui ) {
		equal( event.originalEvent.type, "click", "originalEvent" );
		equal( ui.oldTab.length, 1, "oldTab length" );
		strictEqual( ui.oldTab[ 0 ], tabs[ 0 ], "oldTab" );
		equal( ui.oldPanel.length, 1, "oldPanel length" );
		strictEqual( ui.oldPanel[ 0 ], panels[ 0 ], "oldPanel" );
		equal( ui.newTab.length, 1, "newTab length" );
		strictEqual( ui.newTab[ 0 ], tabs[ 1 ], "newTab" );
		equal( ui.newPanel.length, 1, "newPanel length" );
		strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
		state( element, 0, 1, 0 );
	});
	anchors.eq( 1 ).click();
	state( element, 0, 1, 0 );

	// collapsing
	element.one( "tabsactivate", function( event, ui ) {
		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.oldTab.length, 1, "oldTab length" );
		strictEqual( ui.oldTab[ 0 ], tabs[ 1 ], "oldTab" );
		equal( ui.oldPanel.length, 1, "oldPanel length" );
		strictEqual( ui.oldPanel[ 0 ], panels[ 1 ], "oldPanel" );
		equal( ui.newTab.length, 0, "newTab length" );
		equal( ui.newPanel.length, 0, "newPanel length" );
		state( element, 0, 0, 0 );
	});
	element.tabs( "option", "active", false );
	state( element, 0, 0, 0 );

	// prevent activation
	element.one( "tabsbeforeactivate", function( event ) {
		ok( true, "tabsbeforeactivate" );
		event.preventDefault();
	});
	element.one( "tabsactivate", function() {
		ok( false, "tabsactivate" );
	});
	element.tabs( "option", "active", 1 );
});

test( "beforeLoad", function() {
	expect( 32 );

	var tab, panelId, panel,
		element = $( "#tabs2" );

	// init
	element.one( "tabsbeforeload", function( event, ui ) {
		tab = element.find( ".ui-tabs-nav li" ).eq( 2 );
		panelId = tab.attr( "aria-controls" );
		panel = $( "#" + panelId );

		ok( !( "originalEvent" in event ), "originalEvent" );
		ok( "abort" in ui.jqXHR, "jqXHR" );
		ok( ui.ajaxSettings.url, "data/test.html", "ajaxSettings.url" );
		equal( ui.tab.length, 1, "tab length" );
		strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
		equal( ui.panel.length, 1, "panel length" );
		strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
		equal( ui.panel.html(), "", "panel html" );
		event.preventDefault();
		state( element, 0, 0, 1, 0, 0 );
	});
	element.tabs({ active: 2 });
	state( element, 0, 0, 1, 0, 0 );
	equal( panel.html(), "", "panel html after" );
	element.tabs( "destroy" );

	// .option()
	element.one( "tabsbeforeload", function( event, ui ) {
		tab = element.find( ".ui-tabs-nav li" ).eq( 2 );
		panelId = tab.attr( "aria-controls" );
		panel = $( "#" + panelId );

		ok( !( "originalEvent" in event ), "originalEvent" );
		ok( "abort" in ui.jqXHR, "jqXHR" );
		ok( ui.ajaxSettings.url, "data/test.html", "ajaxSettings.url" );
		equal( ui.tab.length, 1, "tab length" );
		strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
		equal( ui.panel.length, 1, "panel length" );
		strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
		equal( ui.panel.html(), "", "panel html" );
		event.preventDefault();
		state( element, 1, 0, 0, 0, 0 );
	});
	element.tabs();
	element.tabs( "option", "active", 2 );
	state( element, 0, 0, 1, 0, 0 );
	equal( panel.html(), "", "panel html after" );

	// click, change panel content
	element.one( "tabsbeforeload", function( event, ui ) {
		tab = element.find( ".ui-tabs-nav li" ).eq( 3 );
		panelId = tab.attr( "aria-controls" );
		panel = $( "#" + panelId );

		equal( event.originalEvent.type, "click", "originalEvent" );
		ok( "abort" in ui.jqXHR, "jqXHR" );
		ok( ui.ajaxSettings.url, "data/test.html", "ajaxSettings.url" );
		equal( ui.tab.length, 1, "tab length" );
		strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
		equal( ui.panel.length, 1, "panel length" );
		strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
		ui.panel.html( "<p>testing</p>" );
		event.preventDefault();
		state( element, 0, 0, 1, 0, 0 );
	});
	element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 3 ).click();
	state( element, 0, 0, 0, 1, 0 );
	// .toLowerCase() is needed to convert <P> to <p> in old IEs
	equal( panel.html().toLowerCase(), "<p>testing</p>", "panel html after" );
});

asyncTest( "load", function() {
	expect( 21 );

	var tab, panelId, panel,
		element = $( "#tabs2" );

	// init
	element.one( "tabsload", function( event, ui ) {
		tab = element.find( ".ui-tabs-nav li" ).eq( 2 );
		panelId = tab.attr( "aria-controls" );
		panel = $( "#" + panelId );

		ok( !( "originalEvent" in event ), "originalEvent" );
		equal( ui.tab.length, 1, "tab length" );
		strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
		equal( ui.panel.length, 1, "panel length" );
		strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
		equal( ui.panel.find( "p" ).length, 1, "panel html" );
		state( element, 0, 0, 1, 0, 0 );
		tabsload1();
	});
	element.tabs({ active: 2 });

	function tabsload1() {
		// .option()
		element.one( "tabsload", function( event, ui ) {
			tab = element.find( ".ui-tabs-nav li" ).eq( 3 );
			panelId = tab.attr( "aria-controls" );
			panel = $( "#" + panelId );

			ok( !( "originalEvent" in event ), "originalEvent" );
			equal( ui.tab.length, 1, "tab length" );
			strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
			equal( ui.panel.length, 1, "panel length" );
			strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
			equal( ui.panel.find( "p" ).length, 1, "panel html" );
			state( element, 0, 0, 0, 1, 0 );
			tabsload2();
		});
		element.tabs( "option", "active", 3 );
	}

	function tabsload2() {
		// click, change panel content
		element.one( "tabsload", function( event, ui ) {
			tab = element.find( ".ui-tabs-nav li" ).eq( 4 );
			panelId = tab.attr( "aria-controls" );
			panel = $( "#" + panelId );

			equal( event.originalEvent.type, "click", "originalEvent" );
			equal( ui.tab.length, 1, "tab length" );
			strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
			equal( ui.panel.length, 1, "panel length" );
			strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
			equal( ui.panel.find( "p" ).length, 1, "panel html" );
			state( element, 0, 0, 0, 0, 1 );
			start();
		});
		element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 4 ).click();
	}
});

}( jQuery ) );