krasivotokak-simple-navigation 1.4.1

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,125 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SimpleNavigation::Helpers do
4
+ class ControllerMock
5
+ include SimpleNavigation::Helpers
6
+ end
7
+
8
+ before(:each) do
9
+ @controller = ControllerMock.new
10
+ SimpleNavigation.stub!(:load_config)
11
+ SimpleNavigation::Configuration.stub!(:eval_config)
12
+ @primary_navigation = stub(:primary_navigation, :null_object => true)
13
+ SimpleNavigation.stub!(:primary_navigation).and_return(@primary_navigation)
14
+ end
15
+
16
+ describe 'render_navigation' do
17
+ describe 'regarding loading of the config-file' do
18
+ context 'no options specified' do
19
+ it "should load the config-file for the default context" do
20
+ SimpleNavigation.should_receive(:load_config).with(:default)
21
+ @controller.render_navigation
22
+ end
23
+ end
24
+
25
+ context 'with options specified' do
26
+ it "should load the config-file for the specified context" do
27
+ SimpleNavigation.should_receive(:load_config).with(:my_context)
28
+ @controller.render_navigation(:context => :my_context)
29
+ end
30
+ end
31
+ end
32
+
33
+ it "should eval the config on every request" do
34
+ SimpleNavigation::Configuration.should_receive(:eval_config).with(@controller, :default)
35
+ @controller.render_navigation
36
+ end
37
+
38
+ context 'primary' do
39
+ before(:each) do
40
+ @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
41
+ end
42
+ it "should call render on the primary_navigation" do
43
+ @primary_navigation.should_receive(:render).with(:current_primary)
44
+ @controller.render_navigation(:primary)
45
+ end
46
+ it "should call render on the primary_navigation (specifying level through options)" do
47
+ @primary_navigation.should_receive(:render).with(:current_primary)
48
+ @controller.render_navigation(:level => :primary)
49
+ end
50
+ end
51
+
52
+ context 'secondary' do
53
+ context 'with current_primary_navigation set' do
54
+ before(:each) do
55
+ @sub_navigation = stub(:sub_navigation, :null_object => true)
56
+ @primary_navigation.stub!(:[]).and_return(@sub_navigation)
57
+ @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
58
+ @controller.instance_variable_set(:@current_secondary_navigation, :current_secondary)
59
+ end
60
+ it "should find the sub_navigation belonging to the current primary_navigation" do
61
+ @primary_navigation.should_receive(:[]).with(:current_primary)
62
+ @controller.render_navigation(:secondary)
63
+ end
64
+ it "should call render on the current primary_navigation's sub_navigation" do
65
+ @sub_navigation.should_receive(:render).with(:current_secondary)
66
+ @controller.render_navigation(:secondary)
67
+ end
68
+ end
69
+ context 'without current_primary_navigation set' do
70
+ before(:each) do
71
+ @primary_navigation.stub!(:[]).and_return(nil)
72
+ @controller.instance_variable_set(:@current_primary_navigation, nil)
73
+ end
74
+ it "should not raise an error" do
75
+ lambda{@controller.render_navigation(:secondary)}.should_not raise_error
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ context 'nested' do
82
+ before(:each) do
83
+ @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
84
+ @controller.instance_variable_set(:@current_secondary_navigation, :current_secondary)
85
+ end
86
+ it "should call render on the primary navigation" do
87
+ @primary_navigation.should_receive(:render).with(anything, anything, anything)
88
+ @controller.render_navigation(:nested)
89
+ end
90
+ it "should call render with the current_primary_navigation" do
91
+ @primary_navigation.should_receive(:render).with(:current_primary, anything, anything)
92
+ @controller.render_navigation(:nested)
93
+ end
94
+ it "should call render with the include_subnavigation option set" do
95
+ @primary_navigation.should_receive(:render).with(anything, true, anything)
96
+ @controller.render_navigation(:nested)
97
+ end
98
+ it "should call render with the current_sub_navigation" do
99
+ @primary_navigation.should_receive(:render).with(anything, anything, :current_secondary)
100
+ @controller.render_navigation(:nested)
101
+ end
102
+ end
103
+
104
+ context 'unknown level' do
105
+ it "should raise an error" do
106
+ lambda {@controller.render_navigation(:unknown)}.should raise_error(ArgumentError)
107
+ end
108
+ end
109
+ end
110
+
111
+ describe 'render_primary_navigation' do
112
+ it "should delegate to render_navigation(:primary)" do
113
+ @controller.should_receive(:render_navigation).with(:level => :primary)
114
+ @controller.render_primary_navigation
115
+ end
116
+ end
117
+
118
+ describe 'render_sub_navigation' do
119
+ it "should delegate to render_navigation(:secondary)" do
120
+ @controller.should_receive(:render_navigation).with(:level => :secondary)
121
+ @controller.render_sub_navigation
122
+ end
123
+ end
124
+
125
+ end
@@ -0,0 +1,174 @@
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 'item' do
19
+
20
+ context 'unconditional item' do
21
+
22
+ before(:each) do
23
+ @item_container.stub!(:should_add_item?).and_return(true)
24
+ @options = {}
25
+ end
26
+
27
+ context 'block given' do
28
+ before(:each) do
29
+ @sub_container = stub(:sub_container)
30
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
31
+ end
32
+
33
+ it "should should yield an new ItemContainer" do
34
+ @item_container.item('key', 'name', 'url', @options) do |container|
35
+ container.should == @sub_container
36
+ end
37
+ end
38
+ it "should create a new Navigation-Item with the given params and the specified block" do
39
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, @proc)
40
+ @item_container.item('key', 'name', 'url', @options, &@proc)
41
+ end
42
+ it "should add the created item to the list of items" do
43
+ @item_container.items.should_receive(:<<)
44
+ @item_container.item('key', 'name', 'url', @options) {}
45
+ end
46
+ end
47
+
48
+ context 'no block given' do
49
+ it "should create a new Navigation_item with the given params and nil as sub_navi" do
50
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, nil)
51
+ @item_container.item('key', 'name', 'url', @options)
52
+ end
53
+ it "should add the created item to the list of items" do
54
+ @item_container.items.should_receive(:<<)
55
+ @item_container.item('key', 'name', 'url', @options)
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ context 'conditions given for item' do
62
+
63
+ context '"if" given' do
64
+
65
+ before(:each) do
66
+ @options = {:if => Proc.new {@condition}}
67
+ end
68
+
69
+ it "should remove if from options" do
70
+ @item_container.item('key', 'name', 'url', @options)
71
+ @options[:if].should be_nil
72
+ end
73
+
74
+ context 'if evals to true' do
75
+ before(:each) do
76
+ @condition = true
77
+ end
78
+ it "should create a new Navigation-Item" do
79
+ SimpleNavigation::Item.should_receive(:new)
80
+ @item_container.item('key', 'name', 'url', @options)
81
+ end
82
+ end
83
+
84
+ context 'if evals to false' do
85
+ before(:each) do
86
+ @condition = false
87
+ end
88
+ it "should not create a new Navigation-Item" do
89
+ SimpleNavigation::Item.should_not_receive(:new)
90
+ @item_container.item('key', 'name', 'url', @options)
91
+ end
92
+ end
93
+
94
+ context '"unless" given' do
95
+
96
+ before(:each) do
97
+ @options = {:unless => Proc.new {@condition}}
98
+ end
99
+
100
+
101
+ it "should remove unless from options" do
102
+ @item_container.item('key', 'name', 'url', @options)
103
+ @options[:unless].should be_nil
104
+ end
105
+
106
+ context 'unless evals to false' do
107
+ before(:each) do
108
+ @condition = false
109
+ end
110
+ it "should create a new Navigation-Item" do
111
+ SimpleNavigation::Item.should_receive(:new)
112
+ @item_container.item('key', 'name', 'url', @options)
113
+ end
114
+ end
115
+
116
+ context 'unless evals to true' do
117
+ before(:each) do
118
+ @condition = true
119
+ end
120
+ it "should not create a new Navigation-Item" do
121
+ SimpleNavigation::Item.should_not_receive(:new)
122
+ @item_container.item('key', 'name', 'url', @options)
123
+ end
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ describe '[]' do
135
+
136
+ before(:each) do
137
+ @item_container.item(:first, 'first', 'bla')
138
+ @item_container.item(:second, 'second', 'bla')
139
+ @item_container.item(:third, 'third', 'bla')
140
+ end
141
+
142
+ it "should return the item with the specified navi_key" do
143
+ @item_container[:second].name.should == 'second'
144
+ end
145
+ it "should return nil if no item exists for the specified navi_key" do
146
+ @item_container[:invalid].should be_nil
147
+ end
148
+ end
149
+
150
+ describe 'render' do
151
+ before(:each) do
152
+ @renderer = stub(:renderer)
153
+ @renderer_instance = stub(:renderer_instance, :null_object => true)
154
+ @renderer.stub!(:new).and_return(@renderer_instance)
155
+ @item_container.stub!(:renderer).and_return(@renderer)
156
+ @items = stub(:items)
157
+ @item_container.stub!(:items).and_return(@items)
158
+ end
159
+ it "should instatiate a renderer with the current_primary and current_secondary" do
160
+ @renderer.should_receive(:new).with(:current_navigation, nil)
161
+ @item_container.render(:current_navigation)
162
+ end
163
+ it "should call render on the renderer and pass self" do
164
+ @renderer_instance.should_receive(:render).with(@item_container, anything)
165
+ @item_container.render(:current_navigation)
166
+ end
167
+ it "should call render on the renderer and pass the include_sub_navigation option" do
168
+ @renderer_instance.should_receive(:render).with(anything, true)
169
+ @item_container.render(:current_navigation, true, :current_sub_navigation)
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -0,0 +1,148 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SimpleNavigation::Item do
4
+
5
+ describe 'initialize' do
6
+ context 'method' do
7
+ context 'defined' do
8
+ before(:each) do
9
+ @options = {:method => :delete}
10
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
11
+ end
12
+ it 'should set the method as instance_var' do
13
+ @item.method.should == :delete
14
+ end
15
+ it 'should set the html-options without the method' do
16
+ @item.instance_variable_get(:@html_options).key?(:method).should be_false
17
+ end
18
+ end
19
+
20
+ context 'undefined' do
21
+ before(:each) do
22
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
23
+ end
24
+ it 'should set the instance-var to nil' do
25
+ @item.method.should be_nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ describe 'selected?' do
32
+ before(:each) do
33
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
34
+ end
35
+ it {@item.selected?(:my_key).should be_true}
36
+ it {@item.selected?(:my_other_key).should be_false}
37
+ end
38
+
39
+ describe 'selected_class' do
40
+ before(:each) do
41
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
42
+ end
43
+
44
+ context 'item is selected' do
45
+ before(:each) do
46
+ @item.stub!(:selected?).and_return(true)
47
+ end
48
+ it {@item.instance_eval {selected_class(:bla).should == 'selected'}}
49
+ end
50
+
51
+ context 'item is not selected' do
52
+ before(:each) do
53
+ @item.stub!(:selected?).and_return(false)
54
+ end
55
+ it {@item.instance_eval {selected_class(:bla).should == nil}}
56
+ end
57
+ end
58
+
59
+ describe 'html_options' do
60
+ describe 'class' do
61
+ context 'with classes defined in options' do
62
+ before(:each) do
63
+ @options = {:class => 'my_class'}
64
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
65
+ end
66
+ context 'with item selected' do
67
+ before(:each) do
68
+ @item.stub!(:selected?).and_return(true)
69
+ end
70
+ it {@item.html_options(:bla)[:class].should be_include 'my_class'}
71
+ it {@item.html_options(:bla)[:class].should be_include 'selected'}
72
+ end
73
+
74
+ context 'with item not selected' do
75
+ before(:each) do
76
+ @item.stub!(:selected?).and_return(false)
77
+ end
78
+ it {@item.html_options(:bla)[:class].should be_include 'my_class'}
79
+ end
80
+ end
81
+
82
+ context 'without classes in options' do
83
+ before(:each) do
84
+ @options = {}
85
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
86
+ end
87
+ context 'with item selected' do
88
+ before(:each) do
89
+ @item.stub!(:selected?).and_return(true)
90
+ end
91
+ it {@item.html_options(:bla)[:class].should be_include 'selected'}
92
+ end
93
+
94
+ context 'with item not selected' do
95
+ before(:each) do
96
+ @item.stub!(:selected?).and_return(false)
97
+ end
98
+ it {@item.html_options(:bla)[:class].should_not be_include 'selected'}
99
+ end
100
+ end
101
+
102
+ context 'with autogenerate_item_ids == true' do
103
+ before(:each) do
104
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
105
+ @item.stub!(:autogenerate_item_ids? => true)
106
+ end
107
+ context 'with id defined in options' do
108
+ before(:each) do
109
+ @item.html_options = {:class => 'my_id'}
110
+ end
111
+ it {@item.html_options(:bla)[:class].should be_include 'my_id'}
112
+ end
113
+
114
+ context 'with no id definied in options (using default id)' do
115
+ before(:each) do
116
+ @item.html_options = {}
117
+ end
118
+ it {@item.html_options(:bla)[:class].should == 'my_key'}
119
+ end
120
+ end
121
+
122
+ context 'with autogenerate_item_ids == false' do
123
+ before(:each) do
124
+ @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
125
+ @item.stub!(:autogenerate_item_ids? => false)
126
+ end
127
+ context 'with id defined in options' do
128
+ before(:each) do
129
+ @item.html_options = {:id => 'my_id'}
130
+ end
131
+ it {@item.html_options(:bla)[:id].should == 'my_id'}
132
+ end
133
+
134
+ context 'with no id definied in options (using default id)' do
135
+ before(:each) do
136
+ @item.html_options = {}
137
+ end
138
+ it {@item.html_options(:bla)[:id].should be_nil}
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe SimpleNavigation::Renderer::Base do
4
+ before(:each) do
5
+ @controller = stub(:controller)
6
+ SimpleNavigation.stub!(:controller).and_return(@controller)
7
+ @base_renderer = SimpleNavigation::Renderer::Base.new(:current_primary, :current_secondary)
8
+ end
9
+ it "should inclue ActionView::Helpers::UrlHelper" do
10
+ @base_renderer.should respond_to(:link_to)
11
+ end
12
+ it "should include ActionView::Helpers::TagHelper" do
13
+ @base_renderer.should respond_to(:content_tag)
14
+ end
15
+
16
+ describe 'delegated methods' do
17
+ it {@base_renderer.should respond_to(:form_authenticity_token)}
18
+ it {@base_renderer.should respond_to(:protect_against_forgery?)}
19
+ it {@base_renderer.should respond_to(:request_forgery_protection_token)}
20
+ end
21
+
22
+ describe 'initialize' do
23
+ it {@base_renderer.current_navigation.should == :current_primary}
24
+ it {@base_renderer.current_sub_navigation.should == :current_secondary}
25
+ it {@base_renderer.controller.should == @controller}
26
+ end
27
+
28
+ describe 'controller_method' do
29
+ context 'delegate a single method' do
30
+ before(:each) do
31
+ @base_renderer.class_eval do
32
+ controller_method :my_method
33
+ end
34
+ end
35
+ it 'should delegate a controller_method to the controller' do
36
+ @controller.should_receive(:my_method)
37
+ @base_renderer.my_method
38
+ end
39
+ end
40
+
41
+ context 'delegate multiple methods' do
42
+ before(:each) do
43
+ @base_renderer.class_eval do
44
+ controller_method :test1, :test2
45
+ end
46
+ end
47
+ it 'should delegate all controller_methods to the controller' do
48
+ @controller.should_receive(:test1)
49
+ @base_renderer.test1
50
+ @controller.should_receive(:test2)
51
+ @base_renderer.test2
52
+ end
53
+ end
54
+ end
55
+
56
+ end