aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/tabs/tabs_methods.js
blob: dbb5c2d153f148283d08c23a6569661324e3498d (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
/*
 * tabs_methods.js
 */
(function($) {

module("tabs: methods");

test('init', function() {
	expect(9);

	el = $('#tabs1').tabs();

	ok(true, '.tabs() called on element');
	ok( el.is('.ui-tabs.ui-widget.ui-widget-content.ui-corner-all'), 'attach classes to container');
	ok( $('ul', el).is('.ui-tabs-nav.ui-helper-reset.ui-helper-clearfix.ui-widget-header.ui-corner-all'), 'attach classes to list' );
	ok( $('div:eq(0)', el).is('.ui-tabs-panel.ui-widget-content.ui-corner-bottom'), 'attach classes to panel' );
	ok( $('li:eq(0)', el).is('.ui-tabs-selected.ui-state-active.ui-corner-top'), 'attach classes to active li');
	ok( $('li:eq(1)', el).is('.ui-state-default.ui-corner-top'), 'attach classes to inactive li');
	equals( el.tabs('option', 'active'), 0, 'active option set' );
	equals( $('li', el).index( $('li.ui-tabs-selected', el) ), 0, 'second tab active');
	equals( $('div', el).index( $('div.ui-tabs-hide', '#tabs1') ), 1, 'second panel should be hidden' );
});

test('init with hash', function() {
	expect(5);
	
	//set a hash in the url
	location.hash = '#fragment-2';
	
	//selection of tab with divs ordered differently than list
	el = $('#tabs1').tabs();
	
	equals(el.tabs('option', 'active'), 1, 'second tab should be active');
	
	ok(!$('#tabs1 ul li:eq(0)').is('.ui-tabs-selected.ui-state-active'), 'first tab should not be selected nor active');
	ok($('#tabs1 div:eq(0)').is('.ui-tabs-hide'), 'first div for first tab should be hidden');
	
	ok($('#tabs1 ul li:eq(1)').is('.ui-tabs-selected.ui-state-active'), 'second tab should be selected and active');
	ok(!$('#tabs1 div:eq(1)').is('.ui-tabs-hide'), 'second div for second tab should not be hidden');
});

test('init mismatched order with hash', function() {
	expect(5);
	
	//set a hash in the url
	location.hash = '#tabs7-2';
	
	//selection of tab with divs ordered differently than list
	el = $('#tabs7').tabs();
	
	equals(el.tabs('option', 'active'), 1, 'second tab should be active');
	
	ok(!$('#tabs7-list li:eq(0)').is('.ui-tabs-selected.ui-state-active'), 'first tab should not be selected nor active');
	ok($('#tabs7 div:eq(1)').is('.ui-tabs-hide'), 'second div for first tab should be hidden');
	
	ok($('#tabs7-list li:eq(1)').is('.ui-tabs-selected.ui-state-active'), 'second tab should be selected and active');
	ok(!$('#tabs7 div:eq(0)').is('.ui-tabs-hide'), 'first div for second tab should not be hidden');
});

test('destroy', function() {
	expect(6);

	el = $('#tabs1').tabs({ collapsible: true });
	$('li:eq(2)', el).simulate('mouseover').find('a').focus();
	el.tabs('destroy');

	ok( el.is(':not(.ui-tabs, .ui-widget, .ui-widget-content, .ui-corner-all, .ui-tabs-collapsible)'), 'remove classes from container');
	ok( $('ul', el).is(':not(.ui-tabs-nav, .ui-helper-reset, .ui-helper-clearfix, .ui-widget-header, .ui-corner-all)'), 'remove classes from list' );
	ok( $('div:eq(1)', el).is(':not(.ui-tabs-panel, .ui-widget-content, .ui-corner-bottom, .ui-tabs-hide)'), 'remove classes to panel' );
	ok( $('li:eq(0)', el).is(':not(.ui-tabs-selected, .ui-state-active, .ui-corner-top)'), 'remove classes from active li');
	ok( $('li:eq(1)', el).is(':not(.ui-state-default, .ui-corner-top)'), 'remove classes from inactive li');
	ok( $('li:eq(2)', el).is(':not(.ui-state-hover, .ui-state-focus)'), 'remove classes from mouseovered or focused li');
});

test('enable', function() {
    expect(8);

	el = $('#tabs1').tabs({ disabled: [ 0, 1 ] });
	el.tabs("enable", 1);
	ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
	same(el.tabs('option', 'disabled'), [ 0 ], 'update property');
	
	// enable all tabs
	el.tabs({ disabled: [ 0, 1 ] });
	el.tabs("enable");
	ok( !$('li.ui-state-disabled', el).length, 'enable all');
	same(el.tabs('option', 'disabled'), false, 'update property');

	el.tabs('destroy');
	// enable all tabs one by one
	el.tabs({ disabled: [ 1, 2 ] });
	el.tabs("enable", 1);
	ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
	same(el.tabs('option', 'disabled'), [ 2 ], 'update property');
	el.tabs("enable", 2);
	ok( $('li:eq(2)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
	same( el.tabs('option', 'disabled'), false, 'set to false');
});

test('disable', function() {
    expect(12);

	// normal
	el = $('#tabs1').tabs();
	el.tabs('disable', 1);
	ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li');
	same(el.tabs('option', 'disabled'), [ 1 ], 'update disabled property');

	// disable selected
	el.tabs('disable', 0);
	ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to selected li');
	same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property');
	
	// disable all tabs
	el.tabs('disable');
	same( $('li.ui-state-disabled', el).length, 3, 'disable all');
	same(el.tabs('option', 'disabled'), true, 'set to true');

	el.tabs("destroy");
	// disable all tabs one by one
	el.tabs();
	el.tabs('disable', 0);
	ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to li');
	same(el.tabs('option', 'disabled'), [ 0 ], 'update disabled property');
	el.tabs('disable', 1);
	ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li');
	same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property');
	el.tabs('disable', 2);
	ok( $('li:eq(2)', el).is('.ui-state-disabled'), 'add class to li');
	same(el.tabs('option', 'disabled'), true, 'set to true');
});

test('select', function() {
	expect(6);

	el = $('#tabs1').tabs();

	el.tabs('select', 1);
	equals(el.tabs('option', 'active'), 1, 'should select tab');

	el.tabs('destroy');
	el.tabs({ collapsible: true });
	el.tabs('select', 0);
	equals(el.tabs('option', 'active'), -1, 'should collapse tab passing in the already active tab');

	el.tabs('destroy');
	el.tabs({ collapsible: true });
	el.tabs('select', -1);
	equals(el.tabs('option', 'active'), -1, 'should collapse tab passing in -1');

	el.tabs('destroy');
	el.tabs();
	el.tabs('select', 0);
	equals(el.tabs('option', 'active'), 0, 'should not collapse tab if collapsible is not set to true');
	el.tabs('select', -1);
	equals(el.tabs('option', 'active'), 0, 'should not collapse tab if collapsible is not set to true');

	el.tabs('select', '#fragment-2');
	equals(el.tabs('option', 'active'), 1, 'should select tab by id');
});

test('refresh', function() {
	expect(5);

	var el = $('<div id="tabs"><ul></ul></div>').tabs(),
		ul = el.find('ul');

	equals(el.tabs('option', 'active'), -1, 'Initially empty, no active tab');

	ul.append('<li><a href="data/test.html">Test 1</a></li>');
	el.tabs('refresh');
	equals( el.find('.ui-tabs-panel').length, 1, 'Panel created after refresh');

	ul.find('li').remove();
	el.tabs('refresh');
	equals( el.find('.ui-tabs-panel').length, 0, 'Panel removed after refresh');

	ul.append('<li><a href="#test1">Test 1</a></li>');
	$('<div id="test1">Test Panel 1</div>').insertAfter( ul );
	el.tabs('refresh');
	el.tabs('select', 0);
	equals( el.tabs('option', 'active'), 0, 'First tab added should be auto active');

	ul.append('<li><a href="#test2">Test 2</a></li>');
	$('<div id="test2">Test Panel 2</div>').insertAfter( ul );
	el.tabs('refresh');
	equals( el.tabs('option', 'active'), 0, 'Second tab added should not be auto active');
});

test('load', function() {
	ok(false, "missing test - untested code is broken code.");
});

})(jQuery);