strikeroff-simple-navigation 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,315 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SimpleNavigation::ItemContainer do
4
+ before(:each) do
5
+ @item_container = SimpleNavigation::ItemContainer.new
6
+ end
7
+ describe 'initialize' do
8
+ it "should set the renderer to the globally-configured renderer per default" do
9
+ SimpleNavigation::Configuration.instance.should_receive(:renderer)
10
+ @item_container = SimpleNavigation::ItemContainer.new
11
+ end
12
+ it "should have an empty items-array" do
13
+ @item_container = SimpleNavigation::ItemContainer.new
14
+ @item_container.items.should be_empty
15
+ end
16
+ end
17
+
18
+ describe 'selected?' do
19
+ before(:each) do
20
+ @item_1 = stub(:item, :selected? => false)
21
+ @item_2 = stub(:item, :selected? => false)
22
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
23
+ end
24
+ it "should return nil if no item is selected" do
25
+ @item_container.should_not be_selected
26
+ end
27
+ it "should return true if one item is selected" do
28
+ @item_1.stub!(:selected? => true)
29
+ @item_container.should be_selected
30
+ end
31
+ end
32
+
33
+ describe 'selected_item' do
34
+ before(:each) do
35
+ @item_1 = stub(:item, :selected? => false)
36
+ @item_2 = stub(:item, :selected? => false)
37
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
38
+ end
39
+ context 'navigation explicitely set' do
40
+ before(:each) do
41
+ @item_container.stub!(:[] => @item_1)
42
+ end
43
+ it "should return the explicitely selected item" do
44
+ @item_container.selected_item.should == @item_1
45
+ end
46
+ end
47
+ context 'navigation not explicitely set' do
48
+ before(:each) do
49
+ @item_container.stub!(:[] => nil)
50
+ end
51
+ context 'no item selected' do
52
+ it "should return nil" do
53
+ @item_container.selected_item.should be_nil
54
+ end
55
+ end
56
+ context 'one item selected' do
57
+ before(:each) do
58
+ @item_1.stub!(:selected? => true)
59
+ end
60
+ it "should return the selected item" do
61
+ @item_container.selected_item.should == @item_1
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ describe 'selected_sub_navigation?' do
68
+ context 'with an item selected' do
69
+ before(:each) do
70
+ @selected_item = stub(:selected_item)
71
+ @item_container.stub!(:selected_item => @selected_item)
72
+ end
73
+ context 'selected item has sub_navigation' do
74
+ before(:each) do
75
+ @sub_navigation = stub(:sub_navigation)
76
+ @selected_item.stub!(:sub_navigation => @sub_navigation)
77
+ end
78
+ it {@item_container.send(:selected_sub_navigation?).should be_true}
79
+ end
80
+ context 'selected item does not have sub_navigation' do
81
+ before(:each) do
82
+ @selected_item.stub!(:sub_navigation => nil)
83
+ end
84
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
85
+ end
86
+ end
87
+ context 'without an item selected' do
88
+ before(:each) do
89
+ @item_container.stub!(:selected_item => nil)
90
+ end
91
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
92
+ end
93
+
94
+ end
95
+
96
+ describe 'active_item_container_for' do
97
+ context "the desired level is the same as the container's" do
98
+ it {@item_container.active_item_container_for(1).should == @item_container}
99
+ end
100
+ context "the desired level is different than the container's" do
101
+ context 'with no selected subnavigation' do
102
+ before(:each) do
103
+ @item_container.stub!(:selected_sub_navigation? => false)
104
+ end
105
+ it {@item_container.active_item_container_for(2).should be_nil}
106
+ end
107
+ context 'with selected subnavigation' do
108
+ before(:each) do
109
+ @item_container.stub!(:selected_sub_navigation? => true)
110
+ @sub_nav = stub(:sub_nav)
111
+ @selected_item = stub(:selected_item)
112
+ @item_container.stub!(:selected_item => @selected_item)
113
+ @selected_item.stub!(:sub_navigation => @sub_nav)
114
+ end
115
+ it "should call recursively on the sub_navigation" do
116
+ @sub_nav.should_receive(:active_item_container_for).with(2)
117
+ @item_container.active_item_container_for(2)
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ describe 'current_explicit_navigation' do
124
+ it "should call SimpleNavigation.current_navigation with the container's level" do
125
+ SimpleNavigation.should_receive(:current_navigation_for).with(1)
126
+ @item_container.current_explicit_navigation
127
+ end
128
+ end
129
+
130
+ describe 'item' do
131
+
132
+ context 'unconditional item' do
133
+
134
+ before(:each) do
135
+ @item_container.stub!(:should_add_item?).and_return(true)
136
+ @options = {}
137
+ end
138
+
139
+ context 'block given' do
140
+ before(:each) do
141
+ @sub_container = stub(:sub_container)
142
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
143
+ end
144
+
145
+ it "should should yield an new ItemContainer" do
146
+ @item_container.item('key', 'name', 'url', @options) do |container|
147
+ container.should == @sub_container
148
+ end
149
+ end
150
+ it "should create a new Navigation-Item with the given params and the specified block" do
151
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, @proc)
152
+ @item_container.item('key', 'name', 'url', @options, &@proc)
153
+ end
154
+ it "should add the created item to the list of items" do
155
+ @item_container.items.should_receive(:<<)
156
+ @item_container.item('key', 'name', 'url', @options) {}
157
+ end
158
+ end
159
+
160
+ context 'no block given' do
161
+ it "should create a new Navigation_item with the given params and nil as sub_navi" do
162
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil)
163
+ @item_container.item('key', 'name', 'url', @options)
164
+ end
165
+ it "should add the created item to the list of items" do
166
+ @item_container.items.should_receive(:<<)
167
+ @item_container.item('key', 'name', 'url', @options)
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ context 'conditions given for item' do
174
+
175
+ context '"if" given' do
176
+
177
+ before(:each) do
178
+ @options = {:if => Proc.new {@condition}}
179
+ end
180
+
181
+ it "should remove if from options" do
182
+ @item_container.item('key', 'name', 'url', @options)
183
+ @options[:if].should be_nil
184
+ end
185
+
186
+ context 'if evals to true' do
187
+ before(:each) do
188
+ @condition = true
189
+ end
190
+ it "should create a new Navigation-Item" do
191
+ SimpleNavigation::Item.should_receive(:new)
192
+ @item_container.item('key', 'name', 'url', @options)
193
+ end
194
+ end
195
+
196
+ context 'if evals to false' do
197
+ before(:each) do
198
+ @condition = false
199
+ end
200
+ it "should not create a new Navigation-Item" do
201
+ SimpleNavigation::Item.should_not_receive(:new)
202
+ @item_container.item('key', 'name', 'url', @options)
203
+ end
204
+ end
205
+
206
+ context 'if is not a proc or method' do
207
+ it "should raise an error" do
208
+ lambda {@item_container.item('key', 'name', 'url', {:if => 'text'})}.should raise_error
209
+ end
210
+ end
211
+
212
+ context '"unless" given' do
213
+
214
+ before(:each) do
215
+ @options = {:unless => Proc.new {@condition}}
216
+ end
217
+
218
+
219
+ it "should remove unless from options" do
220
+ @item_container.item('key', 'name', 'url', @options)
221
+ @options[:unless].should be_nil
222
+ end
223
+
224
+ context 'unless evals to false' do
225
+ before(:each) do
226
+ @condition = false
227
+ end
228
+ it "should create a new Navigation-Item" do
229
+ SimpleNavigation::Item.should_receive(:new)
230
+ @item_container.item('key', 'name', 'url', @options)
231
+ end
232
+ end
233
+
234
+ context 'unless evals to true' do
235
+ before(:each) do
236
+ @condition = true
237
+ end
238
+ it "should not create a new Navigation-Item" do
239
+ SimpleNavigation::Item.should_not_receive(:new)
240
+ @item_container.item('key', 'name', 'url', @options)
241
+ end
242
+ end
243
+
244
+ end
245
+
246
+ end
247
+
248
+ end
249
+
250
+ end
251
+
252
+ describe '[]' do
253
+
254
+ before(:each) do
255
+ @item_container.item(:first, 'first', 'bla')
256
+ @item_container.item(:second, 'second', 'bla')
257
+ @item_container.item(:third, 'third', 'bla')
258
+ end
259
+
260
+ it "should return the item with the specified navi_key" do
261
+ @item_container[:second].name.should == 'second'
262
+ end
263
+ it "should return nil if no item exists for the specified navi_key" do
264
+ @item_container[:invalid].should be_nil
265
+ end
266
+ end
267
+
268
+ describe 'render' do
269
+ before(:each) do
270
+ @renderer = stub(:renderer)
271
+ @renderer_instance = stub(:renderer_instance, :null_object => true)
272
+ @renderer.stub!(:new).and_return(@renderer_instance)
273
+ @item_container.stub!(:renderer).and_return(@renderer)
274
+ @items = stub(:items)
275
+ @item_container.stub!(:items).and_return(@items)
276
+ end
277
+ it "should instatiate a renderer" do
278
+ @renderer.should_receive(:new)
279
+ @item_container.render(:current_navigation)
280
+ end
281
+ it "should call render on the renderer and pass self" do
282
+ @renderer_instance.should_receive(:render).with(@item_container, anything)
283
+ @item_container.render(:current_navigation)
284
+ end
285
+ it "should call render on the renderer and pass the include_sub_navigation option" do
286
+ @renderer_instance.should_receive(:render).with(anything, true)
287
+ @item_container.render(true)
288
+ end
289
+
290
+ end
291
+
292
+ describe 'level_for_item' do
293
+ before(:each) do
294
+ @item_container.item(:p1, 'p1', 'p1')
295
+ @item_container.item(:p2, 'p2', 'p2') do |p2|
296
+ p2.item(:s1, 's1', 's1')
297
+ p2.item(:s2, 's2', 's2') do |s2|
298
+ s2.item(:ss1, 'ss1', 'ss1')
299
+ s2.item(:ss2, 'ss2', 'ss2')
300
+ end
301
+ p2.item(:s3, 's3', 's3')
302
+ end
303
+ @item_container.item(:p3, 'p3', 'p3')
304
+ end
305
+ it {@item_container.level_for_item(:p1).should == 1}
306
+ it {@item_container.level_for_item(:p3).should == 1}
307
+ it {@item_container.level_for_item(:s1).should == 2}
308
+ it {@item_container.level_for_item(:ss1).should == 3}
309
+ it {@item_container.level_for_item(:x).should be_nil}
310
+
311
+
312
+
313
+ end
314
+
315
+ end
@@ -0,0 +1,344 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SimpleNavigation::Item do
4
+
5
+ before(:each) do
6
+ @item_container = stub(:item_container)
7
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, nil)
8
+ end
9
+
10
+ describe 'initialize' do
11
+ context 'method' do
12
+ context 'defined' do
13
+ before(:each) do
14
+ @options = {:method => :delete}
15
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
16
+ end
17
+ it 'should set the method as instance_var' do
18
+ @item.method.should == :delete
19
+ end
20
+ it 'should set the html-options without the method' do
21
+ @item.instance_variable_get(:@html_options).key?(:method).should be_false
22
+ end
23
+ end
24
+
25
+ context 'undefined' do
26
+ it 'should set the instance-var to nil' do
27
+ @item.method.should be_nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ describe 'selected?' do
34
+ context 'explicitly selected' do
35
+ before(:each) do
36
+ @item.stub!(:selected_by_config? => true)
37
+ end
38
+ it {@item.should be_selected}
39
+ it "should not evaluate the subnav or urls" do
40
+ @item.should_not_receive(:selected_by_subnav?)
41
+ @item.should_not_receive(:selected_by_url?)
42
+ @item.selected?
43
+ end
44
+ end
45
+ context 'not explicitly selected' do
46
+ before(:each) do
47
+ @item.stub!(:selected_by_config? => false)
48
+ end
49
+ context 'subnav is selected' do
50
+ before(:each) do
51
+ @item.stub!(:selected_by_subnav? => true)
52
+ end
53
+ it {@item.should be_selected}
54
+ end
55
+ context 'subnav is not selected' do
56
+ before(:each) do
57
+ @item.stub!(:selected_by_subnav? => false)
58
+ end
59
+ context 'selected by url' do
60
+ before(:each) do
61
+ @item.stub!(:selected_by_url? => true)
62
+ end
63
+ it {@item.should be_selected}
64
+ end
65
+ context 'not selected by url' do
66
+ before(:each) do
67
+ @item.stub!(:selected_by_url? => false)
68
+ end
69
+ it {@item.should_not be_selected}
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'selected_class' do
76
+ context 'item is selected' do
77
+ before(:each) do
78
+ @item.stub!(:selected? => true)
79
+ end
80
+ it {@item.instance_eval {selected_class.should == 'selected'}}
81
+ end
82
+
83
+ context 'item is not selected' do
84
+ before(:each) do
85
+ @item.stub!(:selected? => false)
86
+ end
87
+ it {@item.instance_eval {selected_class.should == nil}}
88
+ end
89
+ end
90
+
91
+ describe 'html_options' do
92
+ describe 'class' do
93
+ context 'with classes defined in options' do
94
+ before(:each) do
95
+ @options = {:class => 'my_class'}
96
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
97
+ end
98
+ context 'with item selected' do
99
+ before(:each) do
100
+ @item.stub!(:selected? => true)
101
+ end
102
+ it {@item.html_options[:class].should == 'my_class selected'}
103
+ end
104
+
105
+ context 'with item not selected' do
106
+ before(:each) do
107
+ @item.stub!(:selected? => false)
108
+ end
109
+ it {@item.html_options[:class].should == 'my_class'}
110
+ end
111
+ end
112
+
113
+ context 'without classes in options' do
114
+ before(:each) do
115
+ @options = {}
116
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
117
+ end
118
+ context 'with item selected' do
119
+ before(:each) do
120
+ @item.stub!(:selected? => true)
121
+ end
122
+ it {@item.html_options[:class].should == 'selected'}
123
+ end
124
+
125
+ context 'with item not selected' do
126
+ before(:each) do
127
+ @item.stub!(:selected? => false)
128
+ end
129
+ it {@item.html_options[:class].should be_blank}
130
+ end
131
+ end
132
+ end
133
+
134
+ describe 'id' do
135
+ context 'with autogenerate_item_ids == true' do
136
+ before(:each) do
137
+ @item.stub!(:autogenerate_item_ids? => true)
138
+ @item.stub!(:selected? => false)
139
+ end
140
+ context 'with id defined in options' do
141
+ before(:each) do
142
+ @item.html_options = {:id => 'my_id'}
143
+ end
144
+ it {@item.html_options[:id].should == 'my_id'}
145
+ end
146
+
147
+ context 'with no id definied in options (using default id)' do
148
+ before(:each) do
149
+ @item.html_options = {}
150
+ end
151
+ it {@item.html_options[:id].should == 'my_key'}
152
+ end
153
+ end
154
+
155
+ context 'with autogenerate_item_ids == false' do
156
+ before(:each) do
157
+ @item.stub!(:autogenerate_item_ids? => false)
158
+ @item.stub!(:selected? => false)
159
+ end
160
+ context 'with id defined in options' do
161
+ before(:each) do
162
+ @item.html_options = {:id => 'my_id'}
163
+ end
164
+ it {@item.html_options[:id].should == 'my_id'}
165
+ end
166
+
167
+ context 'with no id definied in options (using default id)' do
168
+ before(:each) do
169
+ @item.html_options = {}
170
+ end
171
+ it {@item.html_options[:id].should be_nil}
172
+ end
173
+
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ describe 'selected_by_subnav?' do
181
+ context 'item has subnav' do
182
+ before(:each) do
183
+ @sub_navigation = stub(:sub_navigation)
184
+ @item.stub!(:sub_navigation => @sub_navigation)
185
+ end
186
+ it "should return true if subnav is selected" do
187
+ @sub_navigation.stub!(:selected? => true)
188
+ @item.should be_selected_by_subnav
189
+ end
190
+ it "should return false if subnav is not selected" do
191
+ @sub_navigation.stub!(:selected? => false)
192
+ @item.should_not be_selected_by_subnav
193
+ end
194
+ end
195
+ context 'item does not have subnav' do
196
+ before(:each) do
197
+ @item.stub!(:sub_navigation => @sub_navigation)
198
+ end
199
+ it {@item.should_not be_selected_by_subnav}
200
+ end
201
+ end
202
+
203
+ describe 'selected_by_config?' do
204
+ context 'navigation explicitly set' do
205
+ it "should return true if current matches key" do
206
+ @item_container.stub!(:current_explicit_navigation => :my_key)
207
+ @item.should be_selected_by_config
208
+ end
209
+ it "should return false if current does not match key" do
210
+ @item_container.stub!(:current_explicit_navigation => :other_key)
211
+ @item.should_not be_selected_by_config
212
+ end
213
+ end
214
+ context 'navigation not explicitly set' do
215
+ before(:each) do
216
+ @item_container.stub!(:current_explicit_navigation => nil)
217
+ end
218
+ it {@item.should_not be_selected_by_config}
219
+ end
220
+ end
221
+
222
+ describe 'selected_by_url?' do
223
+ context 'auto_highlight is turned on' do
224
+ before(:each) do
225
+ @item.stub!(:auto_highlight? => true)
226
+ end
227
+ context 'root path matches' do
228
+ before(:each) do
229
+ @item.stub!(:root_path_match? => true)
230
+ end
231
+ it {@item.send(:selected_by_url?).should be_true}
232
+ end
233
+ context 'root path does not match' do
234
+ before(:each) do
235
+ @item.stub!(:root_path_match? => false)
236
+ end
237
+ context 'template is set' do
238
+ before(:each) do
239
+ @template = stub(:template)
240
+ SimpleNavigation.stub!(:template => @template)
241
+ end
242
+ context 'current request url matches url' do
243
+ before(:each) do
244
+ @template.stub!(:current_page? => true)
245
+ end
246
+ it "should test with the item's url" do
247
+ @template.should_receive(:current_page?).with('url')
248
+ @item.send(:selected_by_url?)
249
+ end
250
+ it {@item.send(:selected_by_url?).should be_true}
251
+ end
252
+ context 'no match' do
253
+ before(:each) do
254
+ @template.stub!(:current_page? => false)
255
+ end
256
+ it {@item.send(:selected_by_url?).should be_false}
257
+ end
258
+ end
259
+ context 'template is not set' do
260
+ before(:each) do
261
+ SimpleNavigation.stub!(:template => nil)
262
+ end
263
+ it {@item.send(:selected_by_url?).should be_false}
264
+ end
265
+ end
266
+ end
267
+ context 'auto_highlight is turned off' do
268
+ before(:each) do
269
+ @item.stub!(:auto_highlight? => false)
270
+ end
271
+ it {@item.send(:selected_by_url?).should be_false}
272
+ end
273
+ end
274
+
275
+ describe 'root_path_match?' do
276
+ before(:each) do
277
+ @request = stub(:request)
278
+ @controller = stub(:controller, :request => @request)
279
+ SimpleNavigation.stub!(:controller => @controller)
280
+ end
281
+ it "should match if both url == /" do
282
+ @request.stub!(:path => '/')
283
+ @item.stub!(:url => '/')
284
+ @item.send(:root_path_match?).should be_true
285
+ end
286
+ it "should not match if item url is not /" do
287
+ @request.stub!(:path => '/')
288
+ @item.stub!(:url => '/bla')
289
+ @item.send(:root_path_match?).should be_false
290
+ end
291
+ it "should not match if request url is not /" do
292
+ @request.stub!(:path => '/bla')
293
+ @item.stub!(:url => '/')
294
+ @item.send(:root_path_match?).should be_false
295
+ end
296
+ it "should not match if urls do not match" do
297
+ @request.stub!(:path => '/bla')
298
+ @item.stub!(:url => '/bli')
299
+ @item.send(:root_path_match?).should be_false
300
+ end
301
+ end
302
+
303
+ describe 'auto_highlight?' do
304
+ before(:each) do
305
+ @global = stub(:config)
306
+ SimpleNavigation.stub!(:config => @global)
307
+ end
308
+ context 'global auto_hightlight on' do
309
+ before(:each) do
310
+ @global.stub!(:auto_highlight => true)
311
+ end
312
+ context 'container auto_hightlight on' do
313
+ before(:each) do
314
+ @item_container.stub!(:auto_highlight => true)
315
+ end
316
+ it {@item.send(:auto_highlight?).should be_true}
317
+ end
318
+ context 'container auto_hightlight off' do
319
+ before(:each) do
320
+ @item_container.stub!(:auto_highlight => false)
321
+ end
322
+ it {@item.send(:auto_highlight?).should be_false}
323
+ end
324
+ end
325
+ context 'global auto_hightlight off' do
326
+ before(:each) do
327
+ @global.stub!(:auto_highlight => false)
328
+ end
329
+ context 'container auto_hightlight on' do
330
+ before(:each) do
331
+ @item_container.stub!(:auto_highlight => true)
332
+ end
333
+ it {@item.send(:auto_highlight?).should be_false}
334
+ end
335
+ context 'container auto_hightlight off' do
336
+ before(:each) do
337
+ @item_container.stub!(:auto_highlight => false)
338
+ end
339
+ it {@item.send(:auto_highlight?).should be_false}
340
+ end
341
+ end
342
+ end
343
+
344
+ end