menu_helper 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,177 +1,453 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class MenuByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
5
  super
6
- @menu = PluginAWeek::MenuHelper::Menu.new(:home, @controller)
6
+
7
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
8
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
7
9
  end
8
10
 
9
- def test_should_use_humanized_id_for_content
10
- assert_equal '<li id="home"><a href="http://test.host/">Home</a></li>', @menu.html
11
+ def test_should_have_a_name
12
+ assert_equal 'home', @menu.name
11
13
  end
12
14
 
13
- def test_should_set_html_id_to_id
14
- assert_equal 'home', @menu[:id]
15
+ def test_should_have_a_request_controller
16
+ assert_equal @controller, @menu.request_controller
15
17
  end
16
- end
17
-
18
- class MenuTest < Test::Unit::TestCase
19
- def test_should_not_linkify_if_not_auto_linking
20
- menu = create_menu(:home, nil, :auto_link => false)
21
- assert_equal '<li id="home">Home</li>', menu.html
18
+
19
+ def test_should_have_a_parent_menu_bar
20
+ assert_equal @menu_bar, @menu.parent_menu_bar
22
21
  end
23
22
 
24
- def test_default_menubar_id_should_use_menu_id
25
- menu = create_menu(:home, nil)
26
- assert_equal 'home_menubar', menu.menu_bar[:id]
23
+ def test_should_not_have_a_parent_menu
24
+ assert_nil @menu.parent_menu
27
25
  end
28
26
 
29
- def test_should_not_auto_link_if_auto_link_is_false
30
- menu = create_menu(:home, nil, :auto_link => false)
31
- assert !menu.auto_link?
27
+ def test_should_auto_set_ids
28
+ assert @menu.auto_set_ids?
32
29
  end
33
30
 
34
- def test_should_auto_link_if_url_options_is_string
35
- menu = create_menu(:home, nil, 'http://www.google.com')
36
- assert menu.auto_link?
31
+ def test_should_attach_active_submenus
32
+ assert @menu.attach_active_submenus?
37
33
  end
38
34
 
39
- def test_should_auto_link_if_url_options_is_hash_without_auto_link
40
- menu = create_menu(:home, nil, {})
41
- assert menu.auto_link?
35
+ def test_should_not_add_an_html_id
36
+ assert_nil @menu[:id]
42
37
  end
43
38
 
44
- def test_should_be_selected_if_url_is_current_page
45
- menu = create_menu(:contact)
46
- assert menu.selected?
39
+ def test_should_not_be_selected
40
+ assert !@menu.selected?
41
+ end
42
+ end
43
+
44
+ class MenuTest < Test::Unit::TestCase
45
+ def setup
46
+ super
47
+
48
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
49
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
47
50
  end
48
51
 
49
- def test_should_be_selected_if_submenu_is_selected
50
- menu = create_menu(:home) do |home|
51
- home.menu :contact
52
+ def test_should_accept_a_block
53
+ in_block = false
54
+ menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home) do |menu|
55
+ in_block = true
52
56
  end
53
- assert menu.selected?
57
+
58
+ assert in_block
54
59
  end
55
60
 
56
- def test_should_be_selected_if_submenu_of_submenu_is_selected
57
- menu = create_menu(:home) do |home|
58
- home.menu :about_us do |about_us|
59
- about_us.menu :contact
60
- end
61
- end
62
- assert menu.selected?
61
+ def test_should_include_last_class_in_html_if_last_menu
62
+ assert_equal '<li class="menubar-last"><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html(true)
63
63
  end
64
64
 
65
- def test_should_not_be_selected_if_url_is_not_current_page
66
- menu = create_menu(:home)
67
- assert !menu.selected?
65
+ def test_should_append_last_class_if_class_attribute_already_exists
66
+ @menu[:class] = 'pretty'
67
+ assert_equal '<li class="pretty menubar-last"><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html(true)
68
68
  end
69
69
 
70
- def test_should_build_url_from_named_route_if_id_named_route_exists
71
- menu = create_menu(:home)
72
- expected = {:controller => 'home', :action => 'index', :only_path => false, :use_route => :home}
73
- assert_equal expected, menu.url_options
70
+ def test_should_allow_last_class_to_be_customized
71
+ @original_last_class = PluginAWeek::MenuHelper::Menu.last_class
72
+ PluginAWeek::MenuHelper::Menu.last_class = 'menubar-end'
73
+
74
+ assert_equal '<li class="menubar-end"><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html(true)
75
+ ensure
76
+ PluginAWeek::MenuHelper::Menu.last_class = @original_last_class
74
77
  end
75
78
 
76
- def test_should_build_url_from_named_route_if_id_and_parent_named_route_exists
77
- parent = create_menu(:home)
78
- menu = create_menu(:search, parent)
79
- expected = {:controller => 'home', :action => 'search', :only_path => false, :use_route => :home_search}
80
- assert_equal expected, menu.url_options
79
+ def test_should_not_modify_html_options_after_building_html
80
+ @menu.html(true)
81
+ assert_nil @menu[:class]
82
+ end
83
+ end
84
+
85
+ class MenuWithMatchingNamedRouteTest < Test::Unit::TestCase
86
+ def setup
87
+ super
88
+
89
+ ActionController::Routing::Routes.draw do |map|
90
+ map.home '', :controller => 'home', :action => 'index'
91
+ end
92
+
93
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
94
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
81
95
  end
82
96
 
83
- def test_should_use_id_as_default_controller_if_controller_exists
84
- menu = create_menu(:about_us)
85
- expected = {:controller => 'about_us', :only_path => false}
86
- assert_equal expected, menu.url_options
97
+ def test_should_build_url_from_named_route
98
+ expected = {:controller => 'home', :action => 'index', :only_path => false, :use_route => :home}
99
+ assert_equal expected, @menu.url_options
100
+ end
101
+ end
102
+
103
+ class MenuWithMatchingControllerTest < Test::Unit::TestCase
104
+ def setup
105
+ super
106
+
107
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
108
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
87
109
  end
88
110
 
89
- def test_should_use_parent_controller_as_default_controller_if_id_controller_does_not_exist
90
- create_menu(:home) do |home|
91
- menu = home.menu :privacy_policy
92
- expected = {:controller => 'home', :action => 'privacy_policy', :only_path => false}
93
- assert_equal expected, menu.url_options
94
- end
111
+ def test_should_use_name_as_controller
112
+ expected = {:controller => 'home', :only_path => false}
113
+ assert_equal expected, @menu.url_options
114
+ end
115
+ end
116
+
117
+ class MenuWithoutMatchingNamedRouteOrControllerTest < Test::Unit::TestCase
118
+ def setup
119
+ super
120
+
121
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
122
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :investors)
95
123
  end
96
124
 
97
- def test_should_use_request_controller_as_default_controller_if_parent_and_id_controller_does_not_exist
98
- menu = create_menu(:investors)
125
+ def test_should_use_request_controller_as_controller_and_name_as_action
99
126
  expected = {:controller => 'contact', :action => 'investors', :only_path => false}
100
- assert_equal expected, menu.url_options
127
+ assert_equal expected, @menu.url_options
128
+ end
129
+ end
130
+
131
+ class MenuWithCustomUrlOptionsTest < Test::Unit::TestCase
132
+ def setup
133
+ super
134
+
135
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
101
136
  end
102
137
 
103
- def test_should_use_custom_value_if_controller_is_specified
104
- menu = create_menu(:privacy_policy, nil, :controller => 'home')
138
+ def test_should_use_custom_controller_if_specified
139
+ menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :privacy_policy, :controller => 'home')
105
140
  expected = {:controller => 'home', :action => 'privacy_policy', :only_path => false}
106
141
  assert_equal expected, menu.url_options
107
142
  end
108
143
 
109
- def test_should_not_use_id_as_default_action_if_same_as_controller
110
- menu = create_menu(:about_us, nil, :controller => 'about_us')
144
+ def test_should_not_use_name_as_action_if_same_as_controller_name
145
+ menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :about_us, :controller => 'about_us')
111
146
  expected = {:controller => 'about_us', :only_path => false}
112
147
  assert_equal expected, menu.url_options
113
148
  end
114
149
 
115
- def test_should_use_custom_value_if_action_is_specified
116
- menu = create_menu(:privacy_policy, nil, :controller => 'home', :action => 'privacy')
150
+ def test_should_use_custom_action_if_specified
151
+ menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :privacy_policy, :controller => 'home', :action => 'privacy')
117
152
  expected = {:controller => 'home', :action => 'privacy', :only_path => false}
118
153
  assert_equal expected, menu.url_options
119
154
  end
155
+ end
156
+
157
+ class MenuWithSpecificUrlTest < Test::Unit::TestCase
158
+ def setup
159
+ super
160
+
161
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
162
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :search, 'Search', 'http://www.google.com')
163
+ end
120
164
 
121
- def test_should_allow_string_urls
122
- menu = create_menu(:search, nil, 'Search', 'http://www.google.com')
123
- assert_equal 'http://www.google.com', menu.url_options
165
+ def test_should_use_exact_url
166
+ assert_equal 'http://www.google.com', @menu.url_options
167
+ end
168
+ end
169
+
170
+ class MenuWithMenubarId < Test::Unit::TestCase
171
+ def setup
172
+ super
173
+
174
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {}, :id => 'menus')
175
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
124
176
  end
125
177
 
126
- def test_should_include_selected_class_in_html_if_selected
127
- menu = create_menu(:contact)
128
- assert_equal '<li class="selected" id="contact"><a href="http://test.host/contact">Contact</a></li>', menu.html
178
+ def test_should_prefix_menu_id_with_menu_bar_id
179
+ assert_equal 'menus-home', @menu[:id]
180
+ end
181
+ end
182
+
183
+ class MenuWithoutContentTest < Test::Unit::TestCase
184
+ def setup
185
+ super
186
+
187
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
188
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
129
189
  end
130
190
 
131
- def test_should_append_selected_class_if_class_attribute_already_exists
132
- menu = create_menu(:contact, nil, {}, :class => 'pretty')
133
- assert_equal '<li class="pretty selected" id="contact"><a href="http://test.host/contact">Contact</a></li>', menu.html
191
+ def test_should_use_titleized_version_of_name_as_content
192
+ assert_equal '<li><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html
193
+ end
194
+ end
195
+
196
+ class MenuWithCustomContentTest < Test::Unit::TestCase
197
+ def setup
198
+ super
199
+
200
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
201
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home, 'My Home')
134
202
  end
135
203
 
136
- def test_should_include_last_class_in_html_if_last_menu
137
- menu = create_menu(:home)
138
- assert_equal '<li class="last" id="home"><a href="http://test.host/">Home</a></li>', menu.html(true)
204
+ def test_should_use_custom_content_as_content
205
+ assert_equal '<li><a href="http://test.host/home"><span>My Home</span></a></li>', @menu.html
206
+ end
207
+ end
208
+
209
+ class MenuWithoutLinkingTest < Test::Unit::TestCase
210
+ def setup
211
+ super
212
+
213
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
214
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home, {}, :link => false)
139
215
  end
140
216
 
141
- def test_should_append_last_class_if_class_attribute_already_exists
142
- menu = create_menu(:home, nil, {}, :class => 'pretty')
143
- assert_equal '<li class="pretty last" id="home"><a href="http://test.host/">Home</a></li>', menu.html(true)
217
+ def test_should_not_linkify_html
218
+ assert_equal '<li><span>Home</span></li>', @menu.html
219
+ end
220
+ end
221
+
222
+ class MenuWithoutAutoIdSettingTest < Test::Unit::TestCase
223
+ def setup
224
+ super
225
+
226
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {:auto_set_ids => false}, :id => 'menus')
227
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
228
+ end
229
+
230
+ def test_should_not_set_default_id
231
+ assert_nil @menu[:id]
232
+ end
233
+ end
234
+
235
+ class MenuWhenNotCurrentPageTest < Test::Unit::TestCase
236
+ def setup
237
+ super
238
+
239
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
240
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
144
241
  end
145
242
 
146
- def test_should_not_modify_html_options_after_building_menu
147
- menu = create_menu(:home)
148
- menu.html
149
- assert_nil menu[:class]
243
+ def test_should_not_be_selected
244
+ assert !@menu.selected?
150
245
  end
151
246
 
152
- def test_should_include_submenus_if_submenus_exist
153
- menu = create_menu(:home) do |home|
247
+ def test_should_not_include_selected_css_class_in_html
248
+ assert_equal '<li><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html
249
+ end
250
+ end
251
+
252
+ class MenuWhenCurrentPageTest < Test::Unit::TestCase
253
+ def setup
254
+ super
255
+
256
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
257
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :contact)
258
+ end
259
+
260
+ def test_should_be_selected
261
+ assert @menu.selected?
262
+ end
263
+
264
+ def test_should_include_selected_css_class_in_html
265
+ assert_equal '<li class="menubar-selected"><a href="http://test.host/contact"><span>Contact</span></a></li>', @menu.html
266
+ end
267
+
268
+ def test_should_append_selected_class_if_class_attribute_already_exists
269
+ @menu[:class] = 'pretty'
270
+ assert_equal '<li class="pretty menubar-selected"><a href="http://test.host/contact"><span>Contact</span></a></li>', @menu.html
271
+ end
272
+
273
+ def test_should_allow_selected_class_to_be_customized
274
+ @original_selected_class = PluginAWeek::MenuHelper::Menu.selected_class
275
+ PluginAWeek::MenuHelper::Menu.selected_class = 'menubar-active'
276
+ assert_equal '<li class="menubar-active"><a href="http://test.host/contact"><span>Contact</span></a></li>', @menu.html
277
+ ensure
278
+ PluginAWeek::MenuHelper::Menu.selected_class = @original_selected_class
279
+ end
280
+ end
281
+
282
+ class MenuWithoutSubmenusTest < Test::Unit::TestCase
283
+ def setup
284
+ super
285
+
286
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
287
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home)
288
+ end
289
+
290
+ def test_should_not_render_a_menu_bar
291
+ assert_equal '<li><a href="http://test.host/home"><span>Home</span></a></li>', @menu.html
292
+ end
293
+ end
294
+
295
+ class MenuWithSubmenusTest < Test::Unit::TestCase
296
+ def setup
297
+ super
298
+
299
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
300
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home) do |home|
154
301
  home.menu :about_us do |about_us|
155
- about_us.menu :contact
302
+ about_us.menu :who_we_are
156
303
  end
157
304
  end
158
-
305
+ end
306
+
307
+ def test_should_render_a_menu_bar
159
308
  expected = <<-eos
160
- <li class="selected" id="home"><a href="http://test.host/">Home</a>
161
- <ul id="home_menubar">
162
- <li class="selected last" id="about_us"><a href="http://test.host/about_us">About Us</a>
163
- <ul id="about_us_menubar">
164
- <li class="selected last" id="contact"><a href="http://test.host/contact">Contact</a></li>
309
+ <li><a href="http://test.host/home"><span>Home</span></a>
310
+ <ul class="menubar menubar-2">
311
+ <li class="menubar-last"><a href="http://test.host/about_us"><span>About Us</span></a>
312
+ <ul class="menubar menubar-3">
313
+ <li class="menubar-last"><a href="http://test.host/about_us/who_we_are"><span>Who We Are</span></a></li>
165
314
  </ul>
166
315
  </li>
167
316
  </ul>
168
317
  </li>
169
318
  eos
170
- assert_equal expected.gsub(/\n\s*/, ''), menu.html
319
+ assert_equal expected.gsub(/\n\s*/, ''), @menu.html
320
+ end
321
+ end
322
+
323
+ class MenuUnselectedWithDetachedActiveSubmenusTest < Test::Unit::TestCase
324
+ def setup
325
+ super
326
+
327
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :attach_active_submenus => false)
328
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :home) do |home|
329
+ home.menu :about_us
330
+ end
171
331
  end
172
332
 
173
- private
174
- def create_menu(id, parent = nil, *args, &block)
175
- PluginAWeek::MenuHelper::Menu.new(id, @controller, parent, *args, &block)
333
+ def test_should_render_a_menu_bar
334
+ expected = <<-eos
335
+ <li><a href="http://test.host/home"><span>Home</span></a>
336
+ <ul class="menubar menubar-2">
337
+ <li class="menubar-last"><a href="http://test.host/about_us"><span>About Us</span></a></li>
338
+ </ul>
339
+ </li>
340
+ eos
341
+ assert_equal expected.gsub(/\n\s*/, ''), @menu.html
342
+ end
343
+
344
+ def test_should_not_store_a_menu_bar_in_content_variable
345
+ assert !@controller.instance_variable_defined?('@content_for_menu_bar_level_2')
346
+ end
347
+ end
348
+
349
+ class MenuSelectedWithDetachedActiveSubmenusTest < Test::Unit::TestCase
350
+ def setup
351
+ super
352
+
353
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :attach_active_submenus => false)
354
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :contact) do |contact|
355
+ contact.menu :investors
176
356
  end
357
+ end
358
+
359
+ def test_should_not_render_a_menu_bar
360
+ assert_equal '<li class="menubar-selected"><a href="http://test.host/contact"><span>Contact</span></a></li>', @menu.html
361
+ end
362
+
363
+ def test_should_store_a_menu_bar_in_content_variable
364
+ # Generate the html to store it in the variable
365
+ @menu.html
366
+
367
+ expected = <<-eos
368
+ <ul class="menubar menubar-2">
369
+ <li class="menubar-last"><a href="http://test.host/contact/investors"><span>Investors</span></a></li>
370
+ </ul>
371
+ eos
372
+ assert_equal expected.gsub(/\n\s*/, ''), @controller.instance_variable_get('@content_for_menu_bar_level_2')
373
+ end
374
+ end
375
+
376
+ class MenuWithSubmenuAsCurrentPageTest < Test::Unit::TestCase
377
+ def setup
378
+ super
379
+
380
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
381
+ @menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :about_us) do |about_us|
382
+ about_us.menu :contact
383
+ end
384
+ end
385
+
386
+ def test_should_be_selected
387
+ assert @menu.selected?
388
+ end
389
+
390
+ def test_should_include_selected_css_class_in_html
391
+ expected = <<-eos
392
+ <li class="menubar-selected"><a href="http://test.host/about_us"><span>About Us</span></a>
393
+ <ul class="menubar menubar-2 menubar-selected">
394
+ <li class="menubar-selected menubar-last"><a href="http://test.host/contact"><span>Contact</span></a></li>
395
+ </ul>
396
+ </li>
397
+ eos
398
+ assert_equal expected.gsub(/\n\s*/, ''), @menu.html
399
+ end
400
+ end
401
+
402
+ class MenuWithParentMenuTest < Test::Unit::TestCase
403
+ def setup
404
+ super
405
+
406
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
407
+ @parent_menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :contact, {}, :id => 'contact')
408
+ @menu = @parent_menu.menu :investors
409
+ end
410
+
411
+ def test_should_have_a_parent_menu
412
+ assert_equal @parent_menu, @menu.parent_menu
413
+ end
414
+
415
+ def test_should_prefix_menu_id_with_parent_menu_id
416
+ assert_equal 'contact-investors', @menu[:id]
417
+ end
418
+ end
419
+
420
+ class MenuWithParentMenuAndMatchingNamedRouteTest < Test::Unit::TestCase
421
+ def setup
422
+ super
423
+
424
+ ActionController::Routing::Routes.draw do |map|
425
+ map.contact_investors 'contact/investors', :controller => 'contact', :action => 'investors'
426
+ map.connect ':controller/:action/:id'
427
+ end
428
+
429
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
430
+ @parent_menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :contact)
431
+ @menu = @parent_menu.menu :investors
432
+ end
433
+
434
+ def test_should_build_url_from_named_route
435
+ expected = {:controller => 'contact', :action => 'investors', :only_path => false, :use_route => :contact_investors}
436
+ assert_equal expected, @menu.url_options
437
+ end
438
+ end
439
+
440
+ class MenuWithParentMenuAndMatchingControllerTest < Test::Unit::TestCase
441
+ def setup
442
+ super
443
+
444
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
445
+ @parent_menu = PluginAWeek::MenuHelper::Menu.new(@menu_bar, :contact)
446
+ @menu = @parent_menu.menu :investors
447
+ end
448
+
449
+ def test_should_use_parent_controller_as_controller
450
+ expected = {:controller => 'contact', :action => 'investors', :only_path => false}
451
+ assert_equal expected, @menu.url_options
452
+ end
177
453
  end