jordanyeo-simple-navigation 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGELOG +265 -0
  2. data/Gemfile +17 -0
  3. data/README +22 -0
  4. data/Rakefile +47 -0
  5. data/VERSION +1 -0
  6. data/generators/navigation_config/USAGE +1 -0
  7. data/generators/navigation_config/navigation_config_generator.rb +8 -0
  8. data/generators/navigation_config/templates/config/navigation.rb +76 -0
  9. data/lib/generators/navigation_config/navigation_config_generator.rb +12 -0
  10. data/lib/simple-navigation.rb +1 -0
  11. data/lib/simple_navigation.rb +167 -0
  12. data/lib/simple_navigation/adapters.rb +10 -0
  13. data/lib/simple_navigation/adapters/base.rb +37 -0
  14. data/lib/simple_navigation/adapters/nanoc.rb +45 -0
  15. data/lib/simple_navigation/adapters/padrino.rb +20 -0
  16. data/lib/simple_navigation/adapters/rails.rb +93 -0
  17. data/lib/simple_navigation/adapters/sinatra.rb +69 -0
  18. data/lib/simple_navigation/core.rb +5 -0
  19. data/lib/simple_navigation/core/configuration.rb +72 -0
  20. data/lib/simple_navigation/core/item.rb +144 -0
  21. data/lib/simple_navigation/core/item_adapter.rb +63 -0
  22. data/lib/simple_navigation/core/item_container.rb +147 -0
  23. data/lib/simple_navigation/core/items_provider.rb +35 -0
  24. data/lib/simple_navigation/rails_controller_methods.rb +144 -0
  25. data/lib/simple_navigation/rendering.rb +12 -0
  26. data/lib/simple_navigation/rendering/helpers.rb +123 -0
  27. data/lib/simple_navigation/rendering/renderer/base.rb +107 -0
  28. data/lib/simple_navigation/rendering/renderer/breadcrumbs.rb +59 -0
  29. data/lib/simple_navigation/rendering/renderer/json.rb +29 -0
  30. data/lib/simple_navigation/rendering/renderer/links.rb +32 -0
  31. data/lib/simple_navigation/rendering/renderer/list.rb +29 -0
  32. data/lib/simple_navigation/rendering/renderer/text.rb +26 -0
  33. data/rails/init.rb +1 -0
  34. data/spec/lib/simple_navigation/adapters/padrino_spec.rb +31 -0
  35. data/spec/lib/simple_navigation/adapters/rails_spec.rb +287 -0
  36. data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +80 -0
  37. data/spec/lib/simple_navigation/core/configuration_spec.rb +128 -0
  38. data/spec/lib/simple_navigation/core/item_adapter_spec.rb +212 -0
  39. data/spec/lib/simple_navigation/core/item_container_spec.rb +451 -0
  40. data/spec/lib/simple_navigation/core/item_spec.rb +566 -0
  41. data/spec/lib/simple_navigation/core/items_provider_spec.rb +60 -0
  42. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +249 -0
  43. data/spec/lib/simple_navigation/rendering/helpers_spec.rb +276 -0
  44. data/spec/lib/simple_navigation/rendering/renderer/base_spec.rb +199 -0
  45. data/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb +101 -0
  46. data/spec/lib/simple_navigation/rendering/renderer/json_spec.rb +48 -0
  47. data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +64 -0
  48. data/spec/lib/simple_navigation/rendering/renderer/list_spec.rb +211 -0
  49. data/spec/lib/simple_navigation/rendering/renderer/text_spec.rb +41 -0
  50. data/spec/lib/simple_navigation_spec.rb +307 -0
  51. data/spec/spec_helper.rb +108 -0
  52. metadata +199 -0
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleNavigation::Configuration do
4
+
5
+ before(:each) do
6
+ @config = SimpleNavigation::Configuration.instance
7
+ end
8
+
9
+ describe 'self.run' do
10
+ it "should yield the singleton Configuration object" do
11
+ SimpleNavigation::Configuration.run do |c|
12
+ c.should == @config
13
+ end
14
+ end
15
+ end
16
+
17
+ describe 'self.eval_config' do
18
+ before(:each) do
19
+ @context = mock(:context)
20
+ @context.stub!(:instance_eval)
21
+ SimpleNavigation.stub!(:context_for_eval => @context)
22
+ @config_files = {:default => 'default', :my_context => 'my_context'}
23
+ SimpleNavigation.stub!(:config_files).and_return(@config_files)
24
+ end
25
+ context "with default navigation context" do
26
+ it "should instance_eval the default config_file-string inside the context" do
27
+ @context.should_receive(:instance_eval).with('default')
28
+ SimpleNavigation::Configuration.eval_config
29
+ end
30
+ end
31
+ context 'with non default navigation context' do
32
+ it "should instance_eval the specified config_file-string inside the context" do
33
+ @context.should_receive(:instance_eval).with('my_context')
34
+ SimpleNavigation::Configuration.eval_config(:my_context)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe 'initialize' do
40
+ it "should set the List-Renderer as default upon initialize" do
41
+ @config.renderer.should == SimpleNavigation::Renderer::List
42
+ end
43
+ it "should set the selected_class to 'selected' as default" do
44
+ @config.selected_class.should == 'selected'
45
+ end
46
+ it "should set the active_leaf_class to 'simple-navigation-active-leaf' as default" do
47
+ @config.active_leaf_class.should == 'simple-navigation-active-leaf'
48
+ end
49
+ it "should set autogenerate_item_ids to true as default" do
50
+ @config.autogenerate_item_ids.should be_true
51
+ end
52
+ it "should set auto_highlight to true as default" do
53
+ @config.auto_highlight.should be_true
54
+ end
55
+ it "should set the id_generator" do
56
+ @config.id_generator.should_not be_nil
57
+ end
58
+ it "should set the name_generator" do
59
+ @config.name_generator.should_not be_nil
60
+ end
61
+ end
62
+ describe 'items' do
63
+ before(:each) do
64
+ @container = stub(:items_container)
65
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@container)
66
+ end
67
+ context 'block given' do
68
+ context 'items_provider specified' do
69
+ it {lambda {@config.items(stub(:provider)) {}}.should raise_error}
70
+ end
71
+ context 'no items_provider specified' do
72
+ it "should should yield an new ItemContainer" do
73
+ @config.items do |container|
74
+ container.should == @container
75
+ end
76
+ end
77
+ it "should assign the ItemContainer to an instance-var" do
78
+ @config.items {}
79
+ @config.primary_navigation.should == @container
80
+ end
81
+ it "should not set the items on the container" do
82
+ @container.should_not_receive(:items=)
83
+ @config.items {}
84
+ end
85
+ end
86
+ end
87
+ context 'no block given' do
88
+ context 'items_provider specified' do
89
+ before(:each) do
90
+ @external_provider = stub(:external_provider)
91
+ @items = stub(:items)
92
+ @items_provider = stub(:items_provider, :items => @items)
93
+ SimpleNavigation::ItemsProvider.stub!(:new => @items_provider)
94
+ @container.stub!(:items=)
95
+ end
96
+ it "should create an new Provider object for the specified provider" do
97
+ SimpleNavigation::ItemsProvider.should_receive(:new).with(@external_provider)
98
+ @config.items(@external_provider)
99
+ end
100
+ it "should call items on the provider object" do
101
+ @items_provider.should_receive(:items)
102
+ @config.items(@external_provider)
103
+ end
104
+ it "should set the items on the container" do
105
+ @container.should_receive(:items=).with(@items)
106
+ @config.items(@external_provider)
107
+ end
108
+ end
109
+ context 'items_provider not specified' do
110
+ it {lambda {@config.items}.should raise_error}
111
+ end
112
+ end
113
+ end
114
+
115
+ describe 'loaded?' do
116
+ it "should return true if primary_nav is set" do
117
+ @config.instance_variable_set(:@primary_navigation, :bla)
118
+ @config.should be_loaded
119
+ end
120
+ it "should return false if no primary_nav is set" do
121
+ @config.instance_variable_set(:@primary_navigation, nil)
122
+ @config.should_not be_loaded
123
+ end
124
+ end
125
+
126
+ end
127
+
128
+
@@ -0,0 +1,212 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleNavigation::ItemAdapter, 'when item is an object' do
4
+
5
+ before(:each) do
6
+ @item = stub(:item)
7
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
8
+ end
9
+
10
+ describe 'key' do
11
+ it "should delegate key to item" do
12
+ @item.should_receive(:key)
13
+ @item_adapter.key
14
+ end
15
+ end
16
+
17
+ describe 'url' do
18
+ it "should delegate url to item" do
19
+ @item.should_receive(:url)
20
+ @item_adapter.url
21
+ end
22
+ end
23
+
24
+ describe 'name' do
25
+ it "should delegate name to item" do
26
+ @item.should_receive(:name)
27
+ @item_adapter.name
28
+ end
29
+ end
30
+
31
+ describe 'initialize' do
32
+ it "should set the item" do
33
+ @item_adapter.item.should == @item
34
+ end
35
+ end
36
+
37
+ describe 'options' do
38
+ context 'item does respond to options' do
39
+ before(:each) do
40
+ @options = stub(:options)
41
+ @item.stub!(:options => @options)
42
+ end
43
+ it "should return the item's options'" do
44
+ @item_adapter.options.should == @options
45
+ end
46
+ end
47
+ context 'item does not respond to options' do
48
+ it "should return an empty hash" do
49
+ @item_adapter.options.should == {}
50
+ end
51
+ end
52
+ end
53
+
54
+ describe 'items' do
55
+ context 'item does respond to items' do
56
+ context 'items is nil' do
57
+ before(:each) do
58
+ @item.stub!(:items => nil)
59
+ end
60
+ it "should return nil" do
61
+ @item_adapter.items.should be_nil
62
+ end
63
+ end
64
+ context 'items is not nil' do
65
+ context 'items is empty' do
66
+ before(:each) do
67
+ @item.stub!(:items => [])
68
+ end
69
+ it "should return nil" do
70
+ @item_adapter.items.should be_nil
71
+ end
72
+ end
73
+ context 'items is not empty' do
74
+ before(:each) do
75
+ @items = stub(:items, :empty? => false)
76
+ @item.stub!(:items => @items)
77
+ end
78
+ it "should return the items" do
79
+ @item_adapter.items.should == @items
80
+ end
81
+ end
82
+ end
83
+ end
84
+ context 'item does not respond to items' do
85
+ it "should return nil" do
86
+ @item_adapter.items.should be_nil
87
+ end
88
+ end
89
+ end
90
+
91
+ describe 'to_simple_navigation_item' do
92
+ before(:each) do
93
+ @container = stub(:container)
94
+ @item.stub!(:url => 'url', :name => 'name', :key => 'key', :options => {}, :items => [])
95
+ end
96
+ it "should create a SimpleNavigation::Item" do
97
+ SimpleNavigation::Item.should_receive(:new).with(@container, 'key', 'name', 'url', {}, nil)
98
+ @item_adapter.to_simple_navigation_item(@container)
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ describe SimpleNavigation::ItemAdapter, 'when item is a kind of hash' do
105
+
106
+ class ModifiedHash < Hash
107
+ end
108
+
109
+ before(:each) do
110
+ @item = ModifiedHash[:key => 'key', :url => 'url', :name => 'name']
111
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
112
+ end
113
+
114
+ describe 'key' do
115
+ it "should delegate key to item" do
116
+ @item_adapter.item.should_receive(:key)
117
+ @item_adapter.key
118
+ end
119
+ end
120
+
121
+ describe 'url' do
122
+ it "should delegate url to item" do
123
+ @item_adapter.item.should_receive(:url)
124
+ @item_adapter.url
125
+ end
126
+ end
127
+
128
+ describe 'name' do
129
+ it "should delegate name to item" do
130
+ @item_adapter.item.should_receive(:name)
131
+ @item_adapter.name
132
+ end
133
+ end
134
+
135
+ describe 'initialize' do
136
+ it "should set the item" do
137
+ @item_adapter.item.should_not be_nil
138
+ end
139
+ it "should have converted the item into an object" do
140
+ @item_adapter.item.should respond_to(:url)
141
+ end
142
+ end
143
+
144
+ describe 'options' do
145
+ context 'item does respond to options' do
146
+ before(:each) do
147
+ @item = {:key => 'key', :url => 'url', :name => 'name', :options => {:my => :options}}
148
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
149
+ end
150
+ it "should return the item's options'" do
151
+ @item_adapter.options.should == {:my => :options}
152
+ end
153
+ end
154
+ context 'item does not respond to options' do
155
+ it "should return an empty hash" do
156
+ @item_adapter.options.should == {}
157
+ end
158
+ end
159
+ end
160
+
161
+ describe 'items' do
162
+ context 'item does respond to items' do
163
+ context 'items is nil' do
164
+ before(:each) do
165
+ @item = {:key => 'key', :url => 'url', :name => 'name', :items => nil}
166
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
167
+ end
168
+ it "should return nil" do
169
+ @item_adapter.items.should be_nil
170
+ end
171
+ end
172
+ context 'items is not nil' do
173
+ context 'items is empty' do
174
+ before(:each) do
175
+ @item = {:key => 'key', :url => 'url', :name => 'name', :items => []}
176
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
177
+ end
178
+ it "should return nil" do
179
+ @item_adapter.items.should be_nil
180
+ end
181
+ end
182
+ context 'items is not empty' do
183
+ before(:each) do
184
+ @item = {:key => 'key', :url => 'url', :name => 'name', :items => ['not', 'empty']}
185
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
186
+ end
187
+ it "should return the items" do
188
+ @item_adapter.items.should == ['not', 'empty']
189
+ end
190
+ end
191
+ end
192
+ end
193
+ context 'item does not respond to items' do
194
+ it "should return nil" do
195
+ @item_adapter.items.should be_nil
196
+ end
197
+ end
198
+ end
199
+
200
+ describe 'to_simple_navigation_item' do
201
+ before(:each) do
202
+ @container = stub(:container)
203
+ @item = {:key => 'key', :url => 'url', :name => 'name', :items => [], :options => {}}
204
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
205
+ end
206
+ it "should create a SimpleNavigation::Item" do
207
+ SimpleNavigation::Item.should_receive(:new).with(@container, 'key', 'name', 'url', {}, nil)
208
+ @item_adapter.to_simple_navigation_item(@container)
209
+ end
210
+ end
211
+
212
+ end
@@ -0,0 +1,451 @@
1
+ require '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 'items=' do
19
+ before(:each) do
20
+ @item = stub(:item)
21
+ @items = [@item]
22
+ @item_adapter = stub(:item_adapter).as_null_object
23
+ SimpleNavigation::ItemAdapter.stub(:new => @item_adapter)
24
+ @item_container.stub!(:should_add_item? => true)
25
+ end
26
+ it "should wrap each item in an ItemAdapter" do
27
+ SimpleNavigation::ItemAdapter.should_receive(:new)
28
+ @item_container.items = @items
29
+ end
30
+ context 'item should be added' do
31
+ before(:each) do
32
+ @item_container.stub!(:should_add_item? => true)
33
+ @simple_navigation_item = stub(:simple_navigation_item)
34
+ @item_adapter.stub!(:to_simple_navigation_item => @simple_navigation_item)
35
+ end
36
+ it "should convert the item to a SimpleNavigation::Item" do
37
+ @item_adapter.should_receive(:to_simple_navigation_item).with(@item_container)
38
+ @item_container.items = @items
39
+ end
40
+ it "should add the item to the items-collection" do
41
+ @item_container.items.should_receive(:<<).with(@simple_navigation_item)
42
+ @item_container.items = @items
43
+ end
44
+ end
45
+ context 'item should not be added' do
46
+ before(:each) do
47
+ @item_container.stub!(:should_add_item? => false)
48
+ end
49
+ it "should not convert the item to a SimpleNavigation::Item" do
50
+ @item_adapter.should_not_receive(:to_simple_navigation_item)
51
+ @item_container.items = @items
52
+ end
53
+ it "should not add the item to the items-collection" do
54
+ @item_container.items.should_not_receive(:<<)
55
+ @item_container.items = @items
56
+ end
57
+ end
58
+ end
59
+
60
+ describe 'selected?' do
61
+ before(:each) do
62
+ @item_1 = stub(:item, :selected? => false)
63
+ @item_2 = stub(:item, :selected? => false)
64
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
65
+ end
66
+ it "should return nil if no item is selected" do
67
+ @item_container.should_not be_selected
68
+ end
69
+ it "should return true if one item is selected" do
70
+ @item_1.stub!(:selected? => true)
71
+ @item_container.should be_selected
72
+ end
73
+ end
74
+
75
+ describe 'selected_item' do
76
+ before(:each) do
77
+ SimpleNavigation.stub!(:current_navigation_for => :nav)
78
+ @item_container.stub!(:[] => nil)
79
+ @item_1 = stub(:item, :selected? => false)
80
+ @item_2 = stub(:item, :selected? => false)
81
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
82
+ end
83
+ context 'navigation not explicitely set' do
84
+ context 'no item selected' do
85
+ it "should return nil" do
86
+ @item_container.selected_item.should be_nil
87
+ end
88
+ end
89
+ context 'one item selected' do
90
+ before(:each) do
91
+ @item_1.stub!(:selected? => true)
92
+ end
93
+ it "should return the selected item" do
94
+ @item_container.selected_item.should == @item_1
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe 'selected_sub_navigation?' do
101
+ context 'with an item selected' do
102
+ before(:each) do
103
+ @selected_item = stub(:selected_item)
104
+ @item_container.stub!(:selected_item => @selected_item)
105
+ end
106
+ context 'selected item has sub_navigation' do
107
+ before(:each) do
108
+ @sub_navigation = stub(:sub_navigation)
109
+ @selected_item.stub!(:sub_navigation => @sub_navigation)
110
+ end
111
+ it {@item_container.send(:selected_sub_navigation?).should be_true}
112
+ end
113
+ context 'selected item does not have sub_navigation' do
114
+ before(:each) do
115
+ @selected_item.stub!(:sub_navigation => nil)
116
+ end
117
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
118
+ end
119
+ end
120
+ context 'without an item selected' do
121
+ before(:each) do
122
+ @item_container.stub!(:selected_item => nil)
123
+ end
124
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
125
+ end
126
+
127
+ end
128
+
129
+ describe 'active_item_container_for' do
130
+ context "the desired level is the same as the container's" do
131
+ it {@item_container.active_item_container_for(1).should == @item_container}
132
+ end
133
+ context "the desired level is different than the container's" do
134
+ context 'with no selected subnavigation' do
135
+ before(:each) do
136
+ @item_container.stub!(:selected_sub_navigation? => false)
137
+ end
138
+ it {@item_container.active_item_container_for(2).should be_nil}
139
+ end
140
+ context 'with selected subnavigation' do
141
+ before(:each) do
142
+ @item_container.stub!(:selected_sub_navigation? => true)
143
+ @sub_nav = stub(:sub_nav)
144
+ @selected_item = stub(:selected_item)
145
+ @item_container.stub!(:selected_item => @selected_item)
146
+ @selected_item.stub!(:sub_navigation => @sub_nav)
147
+ end
148
+ it "should call recursively on the sub_navigation" do
149
+ @sub_nav.should_receive(:active_item_container_for).with(2)
150
+ @item_container.active_item_container_for(2)
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ describe 'active_leaf_container' do
157
+ context 'the current container has a selected subnavigation' do
158
+ before(:each) do
159
+ @item_container.stub!(:selected_sub_navigation? => true)
160
+ @sub_nav = stub(:sub_nav)
161
+ @selected_item = stub(:selected_item)
162
+ @item_container.stub!(:selected_item => @selected_item)
163
+ @selected_item.stub!(:sub_navigation => @sub_nav)
164
+ end
165
+ it "should call recursively on the sub_navigation" do
166
+ @sub_nav.should_receive(:active_leaf_container)
167
+ @item_container.active_leaf_container
168
+ end
169
+ end
170
+ context 'the current container is the leaf already' do
171
+ before(:each) do
172
+ @item_container.stub!(:selected_sub_navigation? => false)
173
+ end
174
+ it "should return itsself" do
175
+ @item_container.active_leaf_container.should == @item_container
176
+ end
177
+ end
178
+ end
179
+
180
+ describe 'item' do
181
+
182
+ context 'unconditional item' do
183
+
184
+ before(:each) do
185
+ @item_container.stub!(:should_add_item?).and_return(true)
186
+ @options = {}
187
+ end
188
+
189
+ context 'block given' do
190
+ before(:each) do
191
+ @sub_container = stub(:sub_container)
192
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
193
+ end
194
+
195
+ it "should should yield an new ItemContainer" do
196
+ @item_container.item('key', 'name', 'url', @options) do |container|
197
+ container.should == @sub_container
198
+ end
199
+ end
200
+ it "should create a new Navigation-Item with the given params and the specified block" do
201
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil, &@proc)
202
+ @item_container.item('key', 'name', 'url', @options, &@proc)
203
+ end
204
+ it "should add the created item to the list of items" do
205
+ @item_container.items.should_receive(:<<)
206
+ @item_container.item('key', 'name', 'url', @options) {}
207
+ end
208
+ end
209
+
210
+ context 'no block given' do
211
+ it "should create a new Navigation_item with the given params and nil as sub_navi" do
212
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil)
213
+ @item_container.item('key', 'name', 'url', @options)
214
+ end
215
+ it "should add the created item to the list of items" do
216
+ @item_container.items.should_receive(:<<)
217
+ @item_container.item('key', 'name', 'url', @options)
218
+ end
219
+ end
220
+
221
+ end
222
+
223
+ context 'optional url and optional options' do
224
+ context 'item specifed without url or options' do
225
+ it 'should add the create item to the list of items' do
226
+ @item_container.items.should_receive(:<<)
227
+ @item_container.item('key', 'name')
228
+ end
229
+ end
230
+ context 'item specified with only a url' do
231
+ it 'should add the item to the list' do
232
+ @item_container.items.should_receive(:<<)
233
+ @item_container.item('key', 'name', 'url')
234
+ end
235
+ end
236
+ context 'item specified with only options' do
237
+ context 'containing no conditions' do
238
+ it 'should add the created item to the list of items' do
239
+ @item_container.items.should_receive(:<<)
240
+ @item_container.item('key', 'name', {:option => true})
241
+ end
242
+ end
243
+ context 'containing negative condition' do
244
+ it 'should not add the created item to the list of items' do
245
+ @item_container.items.should_not_receive(:<<)
246
+ @item_container.item('key', 'name', {:if => lambda{false}, :option => true})
247
+ end
248
+ end
249
+ context 'containing positive condition' do
250
+ it 'should add the created item to the list of items' do
251
+ @item_container.items.should_receive(:<<)
252
+ @item_container.item('key', 'name', {:if => lambda{true}, :option => true})
253
+ end
254
+ end
255
+ end
256
+ context 'item specified with a url and options' do
257
+ context 'containing no conditions' do
258
+ it 'should add the created item to the list of items' do
259
+ @item_container.items.should_receive(:<<)
260
+ @item_container.item('key', 'name', 'url', {:option => true})
261
+ end
262
+ end
263
+ context 'containing negative condition' do
264
+ it 'should not add the created item to the list of items' do
265
+ @item_container.items.should_not_receive(:<<)
266
+ @item_container.item('key', 'name', 'url', {:if => lambda{false}, :option => true})
267
+ end
268
+ end
269
+ context 'containing positive condition' do
270
+ it 'should add the created item to the list of items' do
271
+ @item_container.items.should_receive(:<<)
272
+ @item_container.item('key', 'name', 'url', {:if => lambda{true}, :option => true})
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ context 'conditions given for item' do
279
+
280
+ context '"if" given' do
281
+
282
+ before(:each) do
283
+ @options = {:if => Proc.new {@condition}}
284
+ end
285
+
286
+ it "should remove if from options" do
287
+ @item_container.item('key', 'name', 'url', @options)
288
+ @options[:if].should be_nil
289
+ end
290
+
291
+ context 'if evals to true' do
292
+ before(:each) do
293
+ @condition = true
294
+ end
295
+ it "should create a new Navigation-Item" do
296
+ SimpleNavigation::Item.should_receive(:new)
297
+ @item_container.item('key', 'name', 'url', @options)
298
+ end
299
+ end
300
+
301
+ context 'if evals to false' do
302
+ before(:each) do
303
+ @condition = false
304
+ end
305
+ it "should not create a new Navigation-Item" do
306
+ SimpleNavigation::Item.should_not_receive(:new)
307
+ @item_container.item('key', 'name', 'url', @options)
308
+ end
309
+ end
310
+
311
+ context 'if is not a proc or method' do
312
+ it "should raise an error" do
313
+ lambda {@item_container.item('key', 'name', 'url', {:if => 'text'})}.should raise_error
314
+ end
315
+ end
316
+
317
+ context '"unless" given' do
318
+
319
+ before(:each) do
320
+ @options = {:unless => Proc.new {@condition}}
321
+ end
322
+
323
+
324
+ it "should remove unless from options" do
325
+ @item_container.item('key', 'name', 'url', @options)
326
+ @options[:unless].should be_nil
327
+ end
328
+
329
+ context 'unless evals to false' do
330
+ before(:each) do
331
+ @condition = false
332
+ end
333
+ it "should create a new Navigation-Item" do
334
+ SimpleNavigation::Item.should_receive(:new)
335
+ @item_container.item('key', 'name', 'url', @options)
336
+ end
337
+ end
338
+
339
+ context 'unless evals to true' do
340
+ before(:each) do
341
+ @condition = true
342
+ end
343
+ it "should not create a new Navigation-Item" do
344
+ SimpleNavigation::Item.should_not_receive(:new)
345
+ @item_container.item('key', 'name', 'url', @options)
346
+ end
347
+ end
348
+
349
+ end
350
+ end
351
+ end
352
+ end
353
+
354
+ describe '[]' do
355
+
356
+ before(:each) do
357
+ @item_container.item(:first, 'first', 'bla')
358
+ @item_container.item(:second, 'second', 'bla')
359
+ @item_container.item(:third, 'third', 'bla')
360
+ end
361
+
362
+ it "should return the item with the specified navi_key" do
363
+ @item_container[:second].name.should == 'second'
364
+ end
365
+ it "should return nil if no item exists for the specified navi_key" do
366
+ @item_container[:invalid].should be_nil
367
+ end
368
+ end
369
+
370
+ describe 'render' do
371
+ before(:each) do
372
+ @renderer_instance = stub(:renderer).as_null_object
373
+ @renderer_class = stub(:renderer_class, :new => @renderer_instance)
374
+ end
375
+ context 'renderer specified as option' do
376
+ context 'renderer-class specified' do
377
+ it "should instantiate the passed renderer_class with the options" do
378
+ @renderer_class.should_receive(:new).with(:renderer => @renderer_class)
379
+ end
380
+ it "should call render on the renderer and pass self" do
381
+ @renderer_instance.should_receive(:render).with(@item_container)
382
+ end
383
+ after(:each) do
384
+ @item_container.render(:renderer => @renderer_class)
385
+ end
386
+ end
387
+ context 'renderer-symbol specified' do
388
+ before(:each) do
389
+ SimpleNavigation.registered_renderers = {:my_renderer => @renderer_class}
390
+ end
391
+ it "should instantiate the passed renderer_class with the options" do
392
+ @renderer_class.should_receive(:new).with(:renderer => :my_renderer)
393
+ end
394
+ it "should call render on the renderer and pass self" do
395
+ @renderer_instance.should_receive(:render).with(@item_container)
396
+ end
397
+ after(:each) do
398
+ @item_container.render(:renderer => :my_renderer)
399
+ end
400
+ end
401
+ end
402
+ context 'no renderer specified' do
403
+ before(:each) do
404
+ @item_container.stub!(:renderer => @renderer_class)
405
+ @options = {}
406
+ end
407
+ it "should instantiate the container's renderer with the options" do
408
+ @renderer_class.should_receive(:new).with(@options)
409
+ end
410
+ it "should call render on the renderer and pass self" do
411
+ @renderer_instance.should_receive(:render).with(@item_container)
412
+ end
413
+ after(:each) do
414
+ @item_container.render(@options)
415
+ end
416
+ end
417
+ end
418
+
419
+ describe 'level_for_item' do
420
+ before(:each) do
421
+ @item_container.item(:p1, 'p1', 'p1')
422
+ @item_container.item(:p2, 'p2', 'p2') do |p2|
423
+ p2.item(:s1, 's1', 's1')
424
+ p2.item(:s2, 's2', 's2') do |s2|
425
+ s2.item(:ss1, 'ss1', 'ss1')
426
+ s2.item(:ss2, 'ss2', 'ss2')
427
+ end
428
+ p2.item(:s3, 's3', 's3')
429
+ end
430
+ @item_container.item(:p3, 'p3', 'p3')
431
+ end
432
+ it {@item_container.level_for_item(:p1).should == 1}
433
+ it {@item_container.level_for_item(:p3).should == 1}
434
+ it {@item_container.level_for_item(:s1).should == 2}
435
+ it {@item_container.level_for_item(:ss1).should == 3}
436
+ it {@item_container.level_for_item(:x).should be_nil}
437
+
438
+ end
439
+
440
+ describe 'empty?' do
441
+ it "should be empty if there are no items" do
442
+ @item_container.instance_variable_set(:@items, [])
443
+ @item_container.should be_empty
444
+ end
445
+ it "should not be empty if there are some items" do
446
+ @item_container.instance_variable_set(:@items, [stub(:item)])
447
+ @item_container.should_not be_empty
448
+ end
449
+ end
450
+
451
+ end