simple-navigation 3.14.0 → 4.0.0

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.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +10 -2
  5. data/CHANGELOG.md +400 -0
  6. data/Guardfile +4 -2
  7. data/README.md +1 -1
  8. data/gemfiles/.bundle/config +2 -0
  9. data/gemfiles/rails-3-2-stable.gemfile +10 -0
  10. data/gemfiles/rails-4-1-stable.gemfile +6 -0
  11. data/gemfiles/rails-4-2-stable.gemfile +7 -0
  12. data/generators/navigation_config/navigation_config_generator.rb +0 -1
  13. data/generators/navigation_config/templates/config/navigation.rb +18 -15
  14. data/lib/simple_navigation.rb +25 -42
  15. data/lib/simple_navigation/adapters/rails.rb +1 -24
  16. data/lib/simple_navigation/adapters/sinatra.rb +2 -11
  17. data/lib/simple_navigation/config_file.rb +36 -0
  18. data/lib/simple_navigation/config_file_finder.rb +42 -0
  19. data/lib/simple_navigation/{core/configuration.rb → configuration.rb} +7 -1
  20. data/lib/simple_navigation/{rendering/helpers.rb → helpers.rb} +0 -4
  21. data/lib/simple_navigation/{core/item.rb → item.rb} +76 -85
  22. data/lib/simple_navigation/{core/item_adapter.rb → item_adapter.rb} +3 -17
  23. data/lib/simple_navigation/{core/item_container.rb → item_container.rb} +23 -14
  24. data/lib/simple_navigation/{core/items_provider.rb → items_provider.rb} +0 -0
  25. data/lib/simple_navigation/railtie.rb +7 -0
  26. data/lib/simple_navigation/renderer.rb +12 -0
  27. data/lib/simple_navigation/{rendering/renderer → renderer}/base.rb +1 -1
  28. data/lib/simple_navigation/{rendering/renderer → renderer}/breadcrumbs.rb +0 -0
  29. data/lib/simple_navigation/{rendering/renderer → renderer}/json.rb +2 -0
  30. data/lib/simple_navigation/{rendering/renderer → renderer}/links.rb +0 -0
  31. data/lib/simple_navigation/{rendering/renderer → renderer}/list.rb +0 -0
  32. data/lib/simple_navigation/{rendering/renderer → renderer}/text.rb +0 -0
  33. data/lib/simple_navigation/version.rb +1 -1
  34. data/simple-navigation.gemspec +5 -4
  35. data/spec/fake_app/config/navigation.rb +6 -0
  36. data/spec/fake_app/rails_app.rb +35 -0
  37. data/spec/initializers/coveralls.rb +3 -0
  38. data/spec/initializers/have_css_matcher.rb +8 -3
  39. data/spec/initializers/memfs.rb +7 -0
  40. data/spec/initializers/rails.rb +4 -0
  41. data/spec/initializers/rspec.rb +7 -0
  42. data/spec/integration/rendering_navigation_spec.rb +12 -0
  43. data/spec/{lib/simple_navigation → simple_navigation}/adapters/padrino_spec.rb +0 -2
  44. data/spec/{lib/simple_navigation → simple_navigation}/adapters/rails_spec.rb +36 -93
  45. data/spec/{lib/simple_navigation → simple_navigation}/adapters/sinatra_spec.rb +4 -6
  46. data/spec/simple_navigation/config_file_finder_spec.rb +50 -0
  47. data/spec/simple_navigation/config_file_spec.rb +25 -0
  48. data/spec/{lib/simple_navigation/core → simple_navigation}/configuration_spec.rb +29 -19
  49. data/spec/{lib/simple_navigation/rendering → simple_navigation}/helpers_spec.rb +10 -13
  50. data/spec/{lib/simple_navigation/core → simple_navigation}/item_adapter_spec.rb +14 -11
  51. data/spec/{lib/simple_navigation/core → simple_navigation}/item_container_spec.rb +130 -42
  52. data/spec/simple_navigation/item_spec.rb +467 -0
  53. data/spec/{lib/simple_navigation/core → simple_navigation}/items_provider_spec.rb +1 -3
  54. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/base_spec.rb +34 -36
  55. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/breadcrumbs_spec.rb +4 -7
  56. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/json_spec.rb +5 -11
  57. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/links_spec.rb +5 -8
  58. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/list_spec.rb +4 -7
  59. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/text_spec.rb +4 -7
  60. data/spec/simple_navigation_spec.rb +190 -0
  61. data/spec/spec_helper.rb +29 -35
  62. metadata +103 -68
  63. data/CHANGELOG +0 -292
  64. data/lib/simple_navigation/core.rb +0 -5
  65. data/lib/simple_navigation/rails_controller_methods.rb +0 -164
  66. data/lib/simple_navigation/rendering.rb +0 -12
  67. data/rails/init.rb +0 -1
  68. data/spec/lib/simple_navigation/core/item_spec.rb +0 -703
  69. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +0 -270
  70. data/spec/lib/simple_navigation_spec.rb +0 -300
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  describe ItemsProvider do
5
3
  let(:items_provider) { ItemsProvider.new(provider) }
@@ -11,7 +9,7 @@ module SimpleNavigation
11
9
  let(:context) { double(:context, provider_method: items) }
12
10
  let(:provider) { :provider_method }
13
11
 
14
- before { SimpleNavigation.stub(context_for_eval: context) }
12
+ before { allow(SimpleNavigation).to receive_messages(context_for_eval: context) }
15
13
 
16
14
  it 'retrieves the items from the evaluation context' do
17
15
  expect(items_provider.items).to eq items
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  module Renderer
5
3
  describe Base do
@@ -8,15 +6,15 @@ module SimpleNavigation
8
6
  let(:adapter) { double(:adapter) }
9
7
  let(:options) { Hash.new }
10
8
 
11
- before { SimpleNavigation.stub(adapter: adapter) }
9
+ before { allow(SimpleNavigation).to receive_messages(adapter: adapter) }
12
10
 
13
11
  it 'delegates the :link_to method to adapter' do
14
- adapter.stub(link_to: 'link_to')
12
+ allow(adapter).to receive_messages(link_to: 'link_to')
15
13
  expect(base.link_to).to eq 'link_to'
16
14
  end
17
15
 
18
16
  it 'delegates the :content_tag method to adapter' do
19
- adapter.stub(content_tag: 'content_tag')
17
+ allow(adapter).to receive_messages(content_tag: 'content_tag')
20
18
  expect(base.content_tag).to eq 'content_tag'
21
19
  end
22
20
 
@@ -44,7 +42,7 @@ module SimpleNavigation
44
42
  let(:options) {{ expand_all: true }}
45
43
 
46
44
  it 'returns true' do
47
- expect(base.expand_all?).to be_true
45
+ expect(base.expand_all?).to be true
48
46
  end
49
47
  end
50
48
 
@@ -52,7 +50,7 @@ module SimpleNavigation
52
50
  let(:options) {{ expand_all: false }}
53
51
 
54
52
  it 'returns false' do
55
- expect(base.expand_all?).to be_false
53
+ expect(base.expand_all?).to be false
56
54
  end
57
55
  end
58
56
  end
@@ -61,7 +59,7 @@ module SimpleNavigation
61
59
  let(:options) { Hash.new }
62
60
 
63
61
  it 'returns false' do
64
- expect(base.expand_all?).to be_false
62
+ expect(base.expand_all?).to be false
65
63
  end
66
64
  end
67
65
  end
@@ -72,7 +70,7 @@ module SimpleNavigation
72
70
  let(:options) {{ skip_if_empty: true }}
73
71
 
74
72
  it 'returns true' do
75
- expect(base.skip_if_empty?).to be_true
73
+ expect(base.skip_if_empty?).to be true
76
74
  end
77
75
  end
78
76
 
@@ -80,7 +78,7 @@ module SimpleNavigation
80
78
  let(:options) {{ skip_if_empty: false }}
81
79
 
82
80
  it 'returns true' do
83
- expect(base.skip_if_empty?).to be_false
81
+ expect(base.skip_if_empty?).to be false
84
82
  end
85
83
  end
86
84
  end
@@ -89,7 +87,7 @@ module SimpleNavigation
89
87
  let(:options) { Hash.new }
90
88
 
91
89
  it 'returns true' do
92
- expect(base.skip_if_empty?).to be_false
90
+ expect(base.skip_if_empty?).to be false
93
91
  end
94
92
  end
95
93
  end
@@ -115,13 +113,13 @@ module SimpleNavigation
115
113
  describe '#consider_sub_navigation?' do
116
114
  let(:item) { double(:item) }
117
115
 
118
- before { item.stub(sub_navigation: sub_navigation) }
116
+ before { allow(item).to receive_messages(sub_navigation: sub_navigation) }
119
117
 
120
118
  context 'when the item has no sub navigation' do
121
119
  let(:sub_navigation) { nil }
122
120
 
123
121
  it 'returns false' do
124
- expect(base.send(:consider_sub_navigation?, item)).to be_false
122
+ expect(base.send(:consider_sub_navigation?, item)).to be false
125
123
  end
126
124
  end
127
125
 
@@ -129,53 +127,53 @@ module SimpleNavigation
129
127
  let(:sub_navigation) { double(:sub_navigation) }
130
128
 
131
129
  context 'and the renderer has an unknown level' do
132
- before { base.stub(level: 'unknown') }
130
+ before { allow(base).to receive_messages(level: 'unknown') }
133
131
 
134
132
  it 'returns false' do
135
- expect(base.send(:consider_sub_navigation?, item)).to be_false
133
+ expect(base.send(:consider_sub_navigation?, item)).to be false
136
134
  end
137
135
  end
138
136
 
139
137
  context 'and the renderer has a level set to :all' do
140
- before { base.stub(level: :all) }
138
+ before { allow(base).to receive_messages(level: :all) }
141
139
 
142
140
  it 'returns false' do
143
- expect(base.send(:consider_sub_navigation?, item)).to be_true
141
+ expect(base.send(:consider_sub_navigation?, item)).to be true
144
142
  end
145
143
  end
146
144
 
147
145
  context "when the renderer's level is a number" do
148
- before { base.stub(level: 2) }
146
+ before { allow(base).to receive_messages(level: 2) }
149
147
 
150
148
  it 'returns false' do
151
- expect(base.send(:consider_sub_navigation?, item)).to be_false
149
+ expect(base.send(:consider_sub_navigation?, item)).to be false
152
150
  end
153
151
  end
154
152
 
155
153
  context "when the renderer's level is a Range" do
156
- before { base.stub(level: 2..3) }
154
+ before { allow(base).to receive_messages(level: 2..3) }
157
155
 
158
156
  context "and sub navigation's level is greater than range.max" do
159
- before { sub_navigation.stub(level: 4) }
157
+ before { allow(sub_navigation).to receive_messages(level: 4) }
160
158
 
161
159
  it 'returns false' do
162
- expect(base.send(:consider_sub_navigation?, item)).to be_false
160
+ expect(base.send(:consider_sub_navigation?, item)).to be false
163
161
  end
164
162
  end
165
163
 
166
164
  context "and sub navigation's level is equal to range.max" do
167
- before { sub_navigation.stub(level: 3) }
165
+ before { allow(sub_navigation).to receive_messages(level: 3) }
168
166
 
169
167
  it 'returns true' do
170
- expect(base.send(:consider_sub_navigation?, item)).to be_true
168
+ expect(base.send(:consider_sub_navigation?, item)).to be true
171
169
  end
172
170
  end
173
171
 
174
172
  context "and sub navigation's level is equal to range.min" do
175
- before { sub_navigation.stub(level: 2) }
173
+ before { allow(sub_navigation).to receive_messages(level: 2) }
176
174
 
177
175
  it 'returns true' do
178
- expect(base.send(:consider_sub_navigation?, item)).to be_true
176
+ expect(base.send(:consider_sub_navigation?, item)).to be true
179
177
  end
180
178
  end
181
179
  end
@@ -186,41 +184,41 @@ module SimpleNavigation
186
184
  let(:item) { double(:item) }
187
185
 
188
186
  context 'when consider_sub_navigation? is true' do
189
- before { base.stub(consider_sub_navigation?: true) }
187
+ before { allow(base).to receive_messages(consider_sub_navigation?: true) }
190
188
 
191
189
  context 'and expand_sub_navigation? is true' do
192
- before { base.stub(expand_sub_navigation?: true) }
190
+ before { allow(base).to receive_messages(expand_sub_navigation?: true) }
193
191
 
194
192
  it 'returns true' do
195
- expect(base.include_sub_navigation?(item)).to be_true
193
+ expect(base.include_sub_navigation?(item)).to be true
196
194
  end
197
195
  end
198
196
 
199
197
  context 'and expand_sub_navigation? is false' do
200
- before { base.stub(expand_sub_navigation?: false) }
198
+ before { allow(base).to receive_messages(expand_sub_navigation?: false) }
201
199
 
202
200
  it 'returns false' do
203
- expect(base.include_sub_navigation?(item)).to be_false
201
+ expect(base.include_sub_navigation?(item)).to be false
204
202
  end
205
203
  end
206
204
  end
207
205
 
208
206
  context 'consider_sub_navigation? is false' do
209
- before { base.stub(consider_sub_navigation?: false) }
207
+ before { allow(base).to receive_messages(consider_sub_navigation?: false) }
210
208
 
211
209
  context 'and expand_sub_navigation? is true' do
212
- before { base.stub(expand_sub_navigation?: true) }
210
+ before { allow(base).to receive_messages(expand_sub_navigation?: true) }
213
211
 
214
212
  it 'returns false' do
215
- expect(base.include_sub_navigation?(item)).to be_false
213
+ expect(base.include_sub_navigation?(item)).to be false
216
214
  end
217
215
  end
218
216
 
219
217
  context 'and expand_sub_navigation? is false' do
220
- before { base.stub(expand_sub_navigation?: false) }
218
+ before { allow(base).to receive_messages(expand_sub_navigation?: false) }
221
219
 
222
220
  it 'returns false' do
223
- expect(base.include_sub_navigation?(item)).to be_false
221
+ expect(base.include_sub_navigation?(item)).to be false
224
222
  end
225
223
  end
226
224
  end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  module Renderer
5
3
  describe Breadcrumbs do
@@ -9,7 +7,7 @@ module SimpleNavigation
9
7
  let(:options) {{ level: :all }}
10
8
  let(:output) { HTML::Document.new(raw_output).root }
11
9
  let(:raw_output) { renderer.render(navigation) }
12
- let(:renderer) { setup_renderer(Breadcrumbs, options) }
10
+ let(:renderer) { Breadcrumbs.new(options) }
13
11
 
14
12
  before { select_an_item(navigation[item]) if item }
15
13
 
@@ -92,11 +90,10 @@ module SimpleNavigation
92
90
 
93
91
  context 'when a sub navigation item is selected' do
94
92
  before do
95
- navigation[:invoices].stub(selected?: true)
93
+ allow(navigation[:invoices]).to receive_messages(selected?: true)
96
94
 
97
- navigation[:invoices]
98
- .sub_navigation[:unpaid]
99
- .stub(selected?: true, selected_by_condition?: true)
95
+ allow(navigation[:invoices].sub_navigation[:unpaid]).to \
96
+ receive_messages(selected?: true, selected_by_condition?: true)
100
97
  end
101
98
 
102
99
  it 'renders all items as links' do
@@ -1,8 +1,3 @@
1
- require 'spec_helper'
2
- require 'json_spec'
3
-
4
- RSpec.configure { |config| config.include JsonSpec::Helpers }
5
-
6
1
  module SimpleNavigation
7
2
  module Renderer
8
3
  describe Json do
@@ -12,8 +7,8 @@ module SimpleNavigation
12
7
  let(:item) { :invoices }
13
8
  let(:options) {{ level: :all }}
14
9
  let(:output) { renderer.render(navigation) }
15
- let(:parsed_output) { parse_json(output) }
16
- let(:renderer) { setup_renderer(Json, options) }
10
+ let(:parsed_output) { JSON.parse(output) }
11
+ let(:renderer) { Json.new(options) }
17
12
 
18
13
  before { select_an_item(navigation[item]) if item }
19
14
 
@@ -48,11 +43,10 @@ module SimpleNavigation
48
43
  end
49
44
 
50
45
  before do
51
- navigation[:invoices].stub(selected?: true)
46
+ allow(navigation[:invoices]).to receive_messages(selected?: true)
52
47
 
53
- navigation[:invoices]
54
- .sub_navigation[:unpaid]
55
- .stub(selected?: true, selected_by_condition?: true)
48
+ allow(navigation[:invoices].sub_navigation[:unpaid]).to \
49
+ receive_messages(selected?: true, selected_by_condition?: true)
56
50
  end
57
51
 
58
52
  it 'marks all the parent items as selected' do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  module Renderer
5
3
  describe Links do
@@ -10,7 +8,7 @@ module SimpleNavigation
10
8
  let(:options) {{ level: :all }}
11
9
  let(:output) { HTML::Document.new(raw_output).root }
12
10
  let(:raw_output) { renderer.render(navigation) }
13
- let(:renderer) { setup_renderer(Links, options) }
11
+ let(:renderer) { Links.new(options) }
14
12
 
15
13
  before { select_an_item(navigation[item]) if item }
16
14
 
@@ -64,17 +62,16 @@ module SimpleNavigation
64
62
  let(:options) {{ level: :all, join_with: ' | ' }}
65
63
 
66
64
  it 'separates the items with the specified separator' do
67
- expect(raw_output.scan(' | ')).to have(3).items
65
+ expect(raw_output.scan(' | ').size).to eq 3
68
66
  end
69
67
  end
70
68
 
71
69
  context 'when a sub navigation item is selected' do
72
70
  before do
73
- navigation[:invoices].stub(selected?: true)
71
+ allow(navigation[:invoices]).to receive_messages(selected?: true)
74
72
 
75
- navigation[:invoices]
76
- .sub_navigation[:unpaid]
77
- .stub(selected?: true, selected_by_condition?: true)
73
+ allow(navigation[:invoices].sub_navigation[:unpaid]).to \
74
+ receive_messages(selected?: true, selected_by_condition?: true)
78
75
  end
79
76
 
80
77
  it 'renders the main parent as selected' do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  module Renderer
5
3
  describe List do
@@ -9,7 +7,7 @@ module SimpleNavigation
9
7
  let(:options) {{ level: :all }}
10
8
  let(:output) { HTML::Document.new(raw_output).root }
11
9
  let(:raw_output) { renderer.render(navigation) }
12
- let(:renderer) { setup_renderer(List, options) }
10
+ let(:renderer) { List.new(options) }
13
11
 
14
12
  before { select_an_item(navigation[item]) if item }
15
13
 
@@ -78,11 +76,10 @@ module SimpleNavigation
78
76
 
79
77
  context 'when a sub navigation item is selected' do
80
78
  before do
81
- navigation[:invoices].stub(selected?: true)
79
+ allow(navigation[:invoices]).to receive_messages(selected?: true)
82
80
 
83
- navigation[:invoices]
84
- .sub_navigation[:unpaid]
85
- .stub(selected?: true, selected_by_condition?: true)
81
+ allow(navigation[:invoices].sub_navigation[:unpaid]).to \
82
+ receive_messages(selected?: true, selected_by_condition?: true)
86
83
  end
87
84
 
88
85
  it 'renders the parent items as selected' do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module SimpleNavigation
4
2
  module Renderer
5
3
  describe Text do
@@ -8,7 +6,7 @@ module SimpleNavigation
8
6
  let(:item) { nil }
9
7
  let(:options) {{ level: :all }}
10
8
  let(:output) { renderer.render(navigation) }
11
- let(:renderer) { setup_renderer(Text, options) }
9
+ let(:renderer) { Text.new(options) }
12
10
 
13
11
  before { select_an_item(navigation[item]) if item }
14
12
 
@@ -29,11 +27,10 @@ module SimpleNavigation
29
27
 
30
28
  context 'when a sub navigation item is selected' do
31
29
  before do
32
- navigation[:invoices].stub(selected?: true)
30
+ allow(navigation[:invoices]).to receive_messages(selected?: true)
33
31
 
34
- navigation[:invoices]
35
- .sub_navigation[:unpaid]
36
- .stub(selected?: true, selected_by_condition?: true)
32
+ allow(navigation[:invoices].sub_navigation[:unpaid]).to \
33
+ receive_messages(selected?: true, selected_by_condition?: true)
37
34
  end
38
35
 
39
36
  it 'separates the items with a space' do
@@ -0,0 +1,190 @@
1
+ describe SimpleNavigation do
2
+ before { subject.config_file_path = 'path_to_config' }
3
+
4
+ describe 'config_file_path=' do
5
+ before { subject.config_file_paths = ['existing_path'] }
6
+
7
+ it 'overrides the config_file_paths' do
8
+ subject.config_file_path = 'new_path'
9
+ expect(subject.config_file_paths).to eq ['new_path']
10
+ end
11
+ end
12
+
13
+ describe '.default_config_file_path' do
14
+ before { allow(subject).to receive_messages(root: 'root') }
15
+
16
+ it 'returns the config file path according to :root setting' do
17
+ expect(subject.default_config_file_path).to eq 'root/config'
18
+ end
19
+ end
20
+
21
+ describe 'Regarding renderers' do
22
+ it 'registers the builtin renderers by default' do
23
+ expect(subject.registered_renderers).not_to be_empty
24
+ end
25
+
26
+ describe '.register_renderer' do
27
+ let(:renderer) { double(:renderer) }
28
+
29
+ it 'adds the specified renderer to the list of renderers' do
30
+ subject.register_renderer(my_renderer: renderer)
31
+ expect(subject.registered_renderers[:my_renderer]).to be renderer
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.set_env' do
37
+ before do
38
+ subject.config_file_paths = []
39
+ allow(subject).to receive_messages(default_config_file_path: 'default_path')
40
+ subject.set_env('root', 'my_env')
41
+ end
42
+
43
+ it 'sets the root' do
44
+ expect(subject.root).to eq 'root'
45
+ end
46
+
47
+ it 'sets the environment' do
48
+ expect(subject.environment).to eq 'my_env'
49
+ end
50
+
51
+ it 'adds the default-config path to the list of config_file_paths' do
52
+ expect(subject.config_file_paths).to eq ['default_path']
53
+ end
54
+ end
55
+
56
+ describe '.load_config', memfs: true do
57
+ let(:paths) { ['/path/one', '/path/two'] }
58
+
59
+ before do
60
+ FileUtils.mkdir_p(paths)
61
+ allow(subject).to receive_messages(config_file_paths: paths)
62
+ end
63
+
64
+ context 'when the config file for the context exists' do
65
+ before do
66
+ File.open('/path/two/navigation.rb', 'w') { |f| f.puts 'default content' }
67
+ File.open('/path/one/other_navigation.rb', 'w') { |f| f.puts 'other content' }
68
+ end
69
+
70
+ context 'when no context is provided' do
71
+ it 'stores the configuration in config_files for the default context' do
72
+ subject.load_config
73
+ expect(subject.config_files[:default]).to eq "default content\n"
74
+ end
75
+ end
76
+
77
+ context 'when a context is provided' do
78
+ it 'stores the configuration in config_files for the given context' do
79
+ subject.load_config(:other)
80
+ expect(subject.config_files[:other]).to eq "other content\n"
81
+ end
82
+ end
83
+
84
+ context 'and environment is production' do
85
+ before { allow(subject).to receive_messages(environment: 'production') }
86
+
87
+ it 'loads the config file only for the first call' do
88
+ subject.load_config
89
+ File.open('/path/two/navigation.rb', 'w') { |f| f.puts 'new content' }
90
+ subject.load_config
91
+ expect(subject.config_files[:default]).to eq "default content\n"
92
+ end
93
+ end
94
+
95
+ context "and environment isn't production" do
96
+ it 'loads the config file for every call' do
97
+ subject.load_config
98
+ File.open('/path/two/navigation.rb', 'w') { |f| f.puts 'new content' }
99
+ subject.load_config
100
+ expect(subject.config_files[:default]).to eq "new content\n"
101
+ end
102
+ end
103
+ end
104
+
105
+ context "when the config file for the context doesn't exists" do
106
+ it 'raises an exception' do
107
+ expect{ subject.load_config }.to raise_error
108
+ end
109
+ end
110
+ end
111
+
112
+ describe '.config' do
113
+ it 'returns the Configuration singleton instance' do
114
+ expect(subject.config).to be SimpleNavigation::Configuration.instance
115
+ end
116
+ end
117
+
118
+ describe '.active_item_container_for' do
119
+ let(:primary) { double(:primary) }
120
+
121
+ before { allow(subject.config).to receive_messages(primary_navigation: primary) }
122
+
123
+ context 'when level is :all' do
124
+ it 'returns the primary_navigation' do
125
+ nav = subject.active_item_container_for(:all)
126
+ expect(nav).to be primary
127
+ end
128
+ end
129
+
130
+ context 'when level is :leaves' do
131
+ it 'returns the currently active leaf-container' do
132
+ expect(primary).to receive(:active_leaf_container)
133
+ subject.active_item_container_for(:leaves)
134
+ end
135
+ end
136
+
137
+ context 'when level is a Range' do
138
+ it 'takes the min of the range to lookup the active container' do
139
+ expect(primary).to receive(:active_item_container_for).with(2)
140
+ subject.active_item_container_for(2..3)
141
+ end
142
+ end
143
+
144
+ context 'when level is an Integer' do
145
+ it 'considers the Integer to lookup the active container' do
146
+ expect(primary).to receive(:active_item_container_for).with(1)
147
+ subject.active_item_container_for(1)
148
+ end
149
+ end
150
+
151
+ context 'when level is something else' do
152
+ it 'raises an exception' do
153
+ expect{
154
+ subject.active_item_container_for('something else')
155
+ }.to raise_error
156
+ end
157
+ end
158
+ end
159
+
160
+ describe '.load_adapter' do
161
+ shared_examples 'loading the right adapter' do |framework, adapter|
162
+ context "when the context is #{framework}" do
163
+ before do
164
+ allow(subject).to receive_messages(framework: framework)
165
+ subject.load_adapter
166
+ end
167
+
168
+ it "returns the #{framework} adapter" do
169
+ adapter_class = SimpleNavigation::Adapters.const_get(adapter)
170
+ expect(subject.adapter_class).to be adapter_class
171
+ end
172
+ end
173
+ end
174
+
175
+ it_behaves_like 'loading the right adapter', :rails, :Rails
176
+ it_behaves_like 'loading the right adapter', :padrino, :Padrino
177
+ it_behaves_like 'loading the right adapter', :sinatra, :Sinatra
178
+ end
179
+
180
+ describe '.init_adapter_from' do
181
+ let(:adapter) { double(:adapter) }
182
+ let(:adapter_class) { double(:adapter_class, new: adapter) }
183
+
184
+ it 'sets the adapter to a new instance of adapter_class' do
185
+ subject.adapter_class = adapter_class
186
+ subject.init_adapter_from(:default)
187
+ expect(subject.adapter).to be adapter
188
+ end
189
+ end
190
+ end