navtastic 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,7 @@ RSpec.describe Navtastic::Menu do
46
46
  expect(menu.item("Test")).to eq menu.items.last
47
47
  end
48
48
 
49
- context "with 1 argument" do
49
+ context "with only a name" do
50
50
  before { menu.item name }
51
51
 
52
52
  let(:name) { "Test" }
@@ -61,7 +61,7 @@ RSpec.describe Navtastic::Menu do
61
61
  end
62
62
  end
63
63
 
64
- context "with 2 arguments" do
64
+ context "with a name and a url" do
65
65
  before { menu.item name, url }
66
66
 
67
67
  let(:name) { "Test" }
@@ -77,12 +77,74 @@ RSpec.describe Navtastic::Menu do
77
77
  end
78
78
  end
79
79
 
80
+ context "with a name, url and options" do
81
+ before { menu.item name, url, options }
82
+
83
+ let(:name) { "Test" }
84
+ let(:url) { "/url" }
85
+ let(:options) { { class: 'test' } }
86
+
87
+ let(:item) { menu.items.last }
88
+
89
+ it "sets the name" do
90
+ expect(item.name).to eq name
91
+ end
92
+
93
+ it "sets the url" do
94
+ expect(item.url).to eq url
95
+ end
96
+
97
+ it "sets the options" do
98
+ expect(item.options).to eq options
99
+ end
100
+ end
101
+
102
+ context "with a name and options" do
103
+ before { menu.item name, options }
104
+
105
+ let(:name) { "Test" }
106
+ let(:options) { { class: 'test' } }
107
+
108
+ let(:item) { menu.items.last }
109
+
110
+ it "sets the name" do
111
+ expect(item.name).to eq name
112
+ end
113
+
114
+ it "leaves the url empty" do
115
+ expect(item.url).to eq nil
116
+ end
117
+
118
+ it "sets the options" do
119
+ expect(item.options).to eq options
120
+ end
121
+ end
122
+
80
123
  context "when the item has a submenu" do
81
124
  let(:item) { menu.items.first }
82
125
 
83
- it "adds the menu to the itme" do
126
+ it "adds the menu to the item" do
84
127
  expect(item.submenu).not_to eq nil
85
128
  end
129
+
130
+ it "the submenu has no base_url" do
131
+ expect(item.submenu.base_url).to eq nil
132
+ end
133
+ end
134
+
135
+ context "with the base_url option" do
136
+ subject(:menu) do
137
+ menu = described_class.new
138
+ menu.item "Settings", '/settings', base_url: true do |submenu|
139
+ end
140
+ menu
141
+ end
142
+
143
+ let(:submenu) { menu.items.first.submenu }
144
+
145
+ it "makes the item url the base_url of the submenu" do
146
+ expect(submenu.base_url).to eq '/settings'
147
+ end
86
148
  end
87
149
  end
88
150
 
@@ -109,6 +171,14 @@ RSpec.describe Navtastic::Menu do
109
171
  it "returns nil when the url doesn't exist" do
110
172
  expect(menu['/foo']).to be nil
111
173
  end
174
+
175
+ context "when the menu has a base url" do
176
+ before { menu.config.base_url = '/admin' }
177
+
178
+ it "adds it in front of every url" do
179
+ expect(menu['/admin/about'].name).to eql 'About'
180
+ end
181
+ end
112
182
  end
113
183
 
114
184
  describe '#current_item' do
@@ -200,4 +270,38 @@ RSpec.describe Navtastic::Menu do
200
270
  end
201
271
  end
202
272
  end
273
+
274
+ describe '#base_url' do
275
+ subject(:url) { menu.base_url }
276
+
277
+ let(:menu) { described_class.new }
278
+
279
+ context "when the menu has no base_url" do
280
+ it { is_expected.to eq nil }
281
+ end
282
+
283
+ context "when the menu has a base_url" do
284
+ before { menu.config.base_url = '/admin' }
285
+
286
+ it "returns that base url" do
287
+ expect(url).to eq '/admin'
288
+ end
289
+ end
290
+
291
+ context "when a base url has been defined globally and on both menus" do
292
+ before do
293
+ set_configuration base_url: '/admin'
294
+ menu.config.base_url = '/settings'
295
+ submenu.config.base_url = '/general'
296
+ end
297
+
298
+ subject(:url) { submenu.base_url }
299
+
300
+ let(:submenu) { described_class.new menu }
301
+
302
+ it "combines all base urls together" do
303
+ expect(url).to eq '/admin/settings/general'
304
+ end
305
+ end
306
+ end
203
307
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Navtastic::Renderer::Simple do
4
+ describe '.render' do
5
+ subject(:renderer) { described_class.render menu }
6
+
7
+ let(:current_url) { '/' }
8
+ let(:menu) do
9
+ menu = Navtastic::Menu.new
10
+ menu.item "Home", '/'
11
+ menu.current_url = current_url
12
+ menu
13
+ end
14
+
15
+ it "returns an instance of Navtastic::Renderer::Simple" do
16
+ expect(renderer).to be_a described_class
17
+ end
18
+ end
19
+ end
@@ -1,19 +1,61 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Navtastic::Renderer do
4
- describe '.render' do
5
- subject(:renderer) { described_class.render menu }
4
+ describe '#menu_inside_container?' do
5
+ # Create a test rendered that chooses menu position fro options hash
6
+ class MenuInsideRenderer < described_class
7
+ def menu_inside_container?(_item)
8
+ options[:inside]
9
+ end
10
+ end
11
+
12
+ subject(:renderer) { MenuInsideRenderer.render(menu, inside: inside) }
6
13
 
7
- let(:current_url) { '/' }
8
14
  let(:menu) do
9
15
  menu = Navtastic::Menu.new
10
- menu.item "Home", '/'
11
- menu.current_url = current_url
16
+ menu.item "First" do |submenu|
17
+ submenu.item "Second"
18
+ end
12
19
  menu
13
20
  end
14
21
 
15
- it "returns an instance of Navtastic::Renderer" do
16
- expect(renderer).to be_a described_class
22
+ context "when it returns true" do
23
+ let(:inside) { true }
24
+
25
+ it "renders the submenu inside the item container" do
26
+ expect(renderer.find_by_tag('li').first.children.count).to eq 2
27
+ end
28
+ end
29
+
30
+ context "when it returns false" do
31
+ let(:inside) { false }
32
+
33
+ it "renders the submenu after the item container" do
34
+ expect(renderer.find_by_tag('li').first.children.count).to eq 1
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#render_item' do
40
+ context "when an item has custom classes as an option" do
41
+ subject(:output) { described_class.render(menu) }
42
+
43
+ let(:menu) do
44
+ menu = Navtastic::Menu.new
45
+ menu.item "a", '/a', class: 'class_a'
46
+ menu.item "b", '/b', content_class: 'class_b'
47
+ menu
48
+ end
49
+
50
+ it "adds custom classes to the item container" do
51
+ first_item = output.find_by_tag('li').first
52
+ expect(first_item.class_list).to include 'class_a'
53
+ end
54
+
55
+ it "adds custom classes to the item content" do
56
+ last_item = output.find_by_tag('li').last
57
+ expect(last_item.children.first.class_list).to include 'class_b'
58
+ end
17
59
  end
18
60
  end
19
61
  end
@@ -37,16 +37,53 @@ RSpec.describe Navtastic do
37
37
  describe '.render' do
38
38
  before { define_menu }
39
39
 
40
- subject(:renderer) { described_class.render(menu_name, '/') }
40
+ subject(:render) { described_class.render(menu_name, '/', params) }
41
+
42
+ let(:params) { {} }
41
43
 
42
44
  context "when the menu was found" do
43
- specify { expect(renderer).to be_a Navtastic::Renderer }
45
+ specify { expect(render).to be_a described_class.configuration.renderer }
44
46
  end
45
47
 
46
48
  context "when the menu was not found" do
47
- subject(:renderer) { described_class.render("foo", '/') }
49
+ subject(:render) { described_class.render("foo", '/') }
50
+
51
+ specify { expect { render }.to raise_error KeyError }
52
+ end
53
+
54
+ context "when the renderer was updated in the configuration" do
55
+ let(:mock_renderer) { class_double(Navtastic::Renderer::Simple) }
56
+
57
+ before { set_configuration renderer: mock_renderer }
58
+
59
+ it "calls `.render` on the class in the configuration" do
60
+ allow(mock_renderer).to receive :render
61
+ render
62
+ expect(mock_renderer)
63
+ .to have_received(:render).with(Navtastic::Menu, Hash)
64
+ end
65
+ end
66
+
67
+ context "when rendering options have been set in the configuration" do
68
+ before { set_configuration renderer_options: { foo: :bar } }
48
69
 
49
- specify { expect { renderer }.to raise_error KeyError }
70
+ it "passes the options to the renderer" do
71
+ expect(render.options).to eq(foo: :bar)
72
+ end
73
+ end
74
+
75
+ context "when params contains a renderer key" do
76
+ before { set_configuration renderer_options: { foo: :bar } }
77
+
78
+ let(:params) { { renderer: { foo: :foo, key: :value } } }
79
+
80
+ it "passes the options to the renderer" do
81
+ expect(render.options[:key]).to eq :value
82
+ end
83
+
84
+ it "merges them with the globally configured settings" do
85
+ expect(render.options[:foo]).to eq :foo
86
+ end
50
87
  end
51
88
  end
52
89
  end
data/spec/spec_helper.rb CHANGED
@@ -25,7 +25,8 @@ Bundler.setup
25
25
  require 'navtastic'
26
26
  require 'pp'
27
27
 
28
- require 'support/navtastic_store'
28
+ require 'support/navtastic_helpers'
29
+ require 'support/navtastic_reset'
29
30
 
30
31
  RSpec.configure do |config|
31
32
  # rspec-expectations config goes here. You can use an alternate
@@ -0,0 +1,17 @@
1
+ # Helpers that are used when testing
2
+ module NavtasticHelpers
3
+ # Update the current configuration
4
+ #
5
+ # @param options [Hash] configuration settings to override
6
+ def set_configuration(options)
7
+ Navtastic.configure do |configuration|
8
+ options.each do |key, value|
9
+ configuration.send("#{key}=", value)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ RSpec.configure do |c|
16
+ c.include NavtasticHelpers
17
+ end
@@ -1,6 +1,7 @@
1
- # Remove all stored menus before every test
1
+ # Remove all stored menus and reset configuration before every test
2
2
  RSpec.configure do |config|
3
3
  config.before(:each) do
4
4
  Navtastic.instance_variable_get(:@menu_store).clear
5
+ Navtastic.reset_configuration
5
6
  end
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: navtastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aram Visser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-21 00:00:00.000000000 Z
11
+ date: 2017-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: arbre
@@ -111,21 +111,35 @@ files:
111
111
  - LICENSE
112
112
  - README.md
113
113
  - Rakefile
114
+ - docs/bulma_headers_preview.png
115
+ - docs/foundation_styles_preview.png
114
116
  - lib/navtastic.rb
117
+ - lib/navtastic/configuration.rb
115
118
  - lib/navtastic/item.rb
116
119
  - lib/navtastic/menu.rb
117
120
  - lib/navtastic/renderer.rb
121
+ - lib/navtastic/renderer/bootstrap4.rb
122
+ - lib/navtastic/renderer/bulma.rb
123
+ - lib/navtastic/renderer/foundation6.rb
124
+ - lib/navtastic/renderer/simple.rb
118
125
  - lib/navtastic/version.rb
119
126
  - navtastic.gemspec
120
127
  - spec/demo/index.rhtml
128
+ - spec/demo/renderer/bootstrap4.rhtml
129
+ - spec/demo/renderer/bulma.rhtml
130
+ - spec/demo/renderer/foundation6.rhtml
131
+ - spec/demo/renderer/simple.rhtml
121
132
  - spec/demo/server.rb
133
+ - spec/navtastic/configuration_spec.rb
122
134
  - spec/navtastic/item_spec.rb
123
135
  - spec/navtastic/menu_spec.rb
136
+ - spec/navtastic/renderer/simple_spec.rb
124
137
  - spec/navtastic/renderer_spec.rb
125
138
  - spec/navtastic_spec.rb
126
139
  - spec/spec_helper.rb
127
140
  - spec/support/matchers/current_item.rb
128
- - spec/support/navtastic_store.rb
141
+ - spec/support/navtastic_helpers.rb
142
+ - spec/support/navtastic_reset.rb
129
143
  homepage: http://github.com/aramvisser/navtastic
130
144
  licenses:
131
145
  - MIT
@@ -152,11 +166,18 @@ specification_version: 4
152
166
  summary: Define and render complex navigation menus
153
167
  test_files:
154
168
  - spec/demo/index.rhtml
169
+ - spec/demo/renderer/bootstrap4.rhtml
170
+ - spec/demo/renderer/bulma.rhtml
171
+ - spec/demo/renderer/foundation6.rhtml
172
+ - spec/demo/renderer/simple.rhtml
155
173
  - spec/demo/server.rb
174
+ - spec/navtastic/configuration_spec.rb
156
175
  - spec/navtastic/item_spec.rb
157
176
  - spec/navtastic/menu_spec.rb
177
+ - spec/navtastic/renderer/simple_spec.rb
158
178
  - spec/navtastic/renderer_spec.rb
159
179
  - spec/navtastic_spec.rb
160
180
  - spec/spec_helper.rb
161
181
  - spec/support/matchers/current_item.rb
162
- - spec/support/navtastic_store.rb
182
+ - spec/support/navtastic_helpers.rb
183
+ - spec/support/navtastic_reset.rb