diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 56cb4a9..aa349eb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,68 +6,12 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings. -# URISchemes: http, https -Layout/LineLength: - Exclude: - - 'generators/navigation_config/templates/config/navigation.rb' - - 'spec/simple_navigation/helpers_spec.rb' - -# Offense count: 1 -# Configuration parameters: MaxRangeSize. -Lint/MissingCopEnableDirective: - Exclude: - - 'spec/simple_navigation/configuration_spec.rb' - -# Offense count: 11 -# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. -# SupportedStyles: snake_case, normalcase, non_integer -# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 -Naming/VariableNumber: - Exclude: - - 'generators/navigation_config/templates/config/navigation.rb' - - 'spec/fake_app/config/navigation.rb' - - 'spec/simple_navigation/item_container_spec.rb' - -# Offense count: 122 -# Configuration parameters: Prefixes, AllowedPatterns. -# Prefixes: when, with, without -RSpec/ContextWording: - Exclude: - - 'spec/simple_navigation/adapters/rails_spec.rb' - - 'spec/simple_navigation/config_file_finder_spec.rb' - - 'spec/simple_navigation/configuration_spec.rb' - - 'spec/simple_navigation/helpers_spec.rb' - - 'spec/simple_navigation/item_adapter_spec.rb' - - 'spec/simple_navigation/item_container_spec.rb' - - 'spec/simple_navigation/item_spec.rb' - - 'spec/simple_navigation/items_provider_spec.rb' - - 'spec/simple_navigation/renderer/base_spec.rb' - - 'spec/simple_navigation/renderer/breadcrumbs_spec.rb' - - 'spec/simple_navigation/renderer/text_spec.rb' - - 'spec/simple_navigation_spec.rb' - -# Offense count: 4 -# Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns. -RSpec/IndexedLet: - Exclude: - - 'spec/simple_navigation/item_container_spec.rb' - # Offense count: 46 # Configuration parameters: . # SupportedStyles: have_received, receive RSpec/MessageSpies: EnforcedStyle: receive -# Offense count: 38 -# Configuration parameters: EnforcedStyle, IgnoreSharedExamples. -# SupportedStyles: always, named_only -RSpec/NamedSubject: - Exclude: - - 'spec/simple_navigation_spec.rb' - # Offense count: 24 RSpec/SubjectStub: Exclude: @@ -75,10 +19,11 @@ RSpec/SubjectStub: - 'spec/simple_navigation/renderer/base_spec.rb' - 'spec/simple_navigation_spec.rb' -# Offense count: 65 +# Offense count: 69 # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: Exclude: + - 'spec/simple_navigation/adapters/nanoc_spec.rb' - 'spec/simple_navigation/adapters/padrino_spec.rb' - 'spec/simple_navigation/adapters/rails_spec.rb' - 'spec/simple_navigation/adapters/sinatra_spec.rb' diff --git a/generators/navigation_config/templates/config/navigation.rb b/generators/navigation_config/templates/config/navigation.rb index 4858940..466bc69 100644 --- a/generators/navigation_config/templates/config/navigation.rb +++ b/generators/navigation_config/templates/config/navigation.rb @@ -35,7 +35,8 @@ # navigation.ignore_query_params_on_auto_highlight = true # navigation.ignore_anchors_on_auto_highlight = true - # If this option is set to true, all item names will be considered as safe (passed through html_safe). Defaults to false. + # If this option is set to true, all item names will be considered as safe + # (passed through html_safe). Defaults to false. # navigation.consider_item_names_as_safe = false # Define the primary navigation @@ -43,8 +44,10 @@ # Add an item to the primary navigation. The following params apply: # key - a symbol which uniquely defines your navigation item in the scope of the primary_navigation # name - will be displayed in the rendered navigation. This can also be a call to your I18n-framework. - # url - the address that the generated item links to. You can also use url_helpers (named routes, restful routes helper, url_for etc.) - # options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.) + # url - the address that the generated item links to. You can also use url_helpers + # (named routes, restful routes helper, url_for etc.) + # options - can be used to specify attributes that will be included in the rendered + # navigation item (e.g. id, class etc.) # some special options that can be set: # :html - Specifies html attributes that will be included in the rendered navigation item # :if - Specifies a proc to call to determine if the item should @@ -58,19 +61,19 @@ # when the item should be highlighted, you can set a regexp which is matched # against the current URI. You may also use a proc, or the symbol :subpath. # - primary.item :key_1, 'name', url, options + primary.item :key1, 'name', url, options # Add an item which has a sub navigation (same params, but with block) - primary.item :key_2, 'name', url, options do |sub_nav| + primary.item :key2, 'name', url, options do |sub_nav| # Add an item to the sub navigation (same params again) - sub_nav.item :key_2_1, 'name', url, options + sub_nav.item :key21, 'name', url, options end # You can also specify a condition-proc that needs to be fullfilled to display an item. # Conditions are part of the options. They are evaluated in the context of the views, # thus you can use all the methods and vars you have available in the views. - primary.item :key_3, 'Admin', url, html: { class: 'special' }, if: -> { current_user.admin? } - primary.item :key_4, 'Account', url, unless: -> { logged_in? } + primary.item :key3, 'Admin', url, html: { class: 'special' }, if: -> { current_user.admin? } + primary.item :key4, 'Account', url, unless: -> { logged_in? } # you can also specify html attributes to attach to this particular level # works for all levels of the menu diff --git a/lib/simple_navigation.rb b/lib/simple_navigation.rb index eaf57fe..808fce3 100644 --- a/lib/simple_navigation.rb +++ b/lib/simple_navigation.rb @@ -73,11 +73,13 @@ def set_env(root, environment) # Returns the current framework in which the plugin is running. def framework return :rails if defined?(Rails) + # :nocov: return :padrino if defined?(Padrino) return :sinatra if defined?(Sinatra) return :nanoc if defined?(Nanoc3) raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps' + # :nocov: end # Loads the adapter for the current framework diff --git a/lib/simple_navigation/adapters/nanoc.rb b/lib/simple_navigation/adapters/nanoc.rb index ba7811f..4bf9499 100644 --- a/lib/simple_navigation/adapters/nanoc.rb +++ b/lib/simple_navigation/adapters/nanoc.rb @@ -2,6 +2,9 @@ module SimpleNavigation module Adapters + # @deprecated This adapter is for Nanoc3 (circa 2011-2012) and is no longer actively maintained. + # Nanoc3 is obsolete and has been replaced by Nanoc 4+. This adapter is kept for + # backward compatibility but should not be used for new projects. class Nanoc < Base class << self def register(root) diff --git a/spec/fake_app/config/navigation.rb b/spec/fake_app/config/navigation.rb index 2c13896..c436383 100644 --- a/spec/fake_app/config/navigation.rb +++ b/spec/fake_app/config/navigation.rb @@ -2,7 +2,7 @@ SimpleNavigation::Configuration.run do |navigation| navigation.items do |nav| - nav.item :item_1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' } - nav.item :item_2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' } + nav.item :item1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' } + nav.item :item2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' } end end diff --git a/spec/simple_navigation/adapters/nanoc_spec.rb b/spec/simple_navigation/adapters/nanoc_spec.rb new file mode 100644 index 0000000..545a562 --- /dev/null +++ b/spec/simple_navigation/adapters/nanoc_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +# NOTE: This adapter is deprecated. It supports Nanoc3 (circa 2011-2012) which is obsolete. +# These tests are maintained for backward compatibility only. +RSpec.describe SimpleNavigation::Adapters::Nanoc do + let(:adapter) { described_class.new(context) } + let(:context) { double(:context, item: item) } + let(:item) { double(:item, path: '/test/path/') } + + describe '.register' do + let(:nanoc_context) { double(:nanoc_context) } + + before do + stub_const('Nanoc3::Context', nanoc_context) + allow(SimpleNavigation).to receive(:set_env) + allow(nanoc_context).to receive(:include) + end + + it 'calls SimpleNavigation.set_env with root and development environment' do + expect(SimpleNavigation).to receive(:set_env).with('/root/path', 'development') + described_class.register('/root/path') + end + + it 'includes SimpleNavigation::Helpers in Nanoc3::Context' do + expect(nanoc_context).to receive(:include).with(SimpleNavigation::Helpers) + described_class.register('/root/path') + end + end + + describe '#initialize' do + it 'sets the context' do + expect(adapter.instance_variable_get(:@context)).to eq context + end + end + + describe '#context_for_eval' do + it 'returns the context' do + expect(adapter.context_for_eval).to eq context + end + end + + describe '#current_page?' do + context 'when the path matches the url (after chomping the trailing slash)' do + it 'returns true' do + expect(adapter.current_page?('/test/path')).to be true + end + end + + context 'when the path does not match the url' do + it 'returns false' do + expect(adapter.current_page?('/other/path')).to be false + end + end + + context 'when path is nil' do + let(:item) { double(:item, path: nil) } + + it 'returns nil' do + expect(adapter.current_page?('/test/path')).to be_nil + end + end + end + + describe '#link_to' do + it 'returns a link with the correct attributes' do + link = adapter.link_to('Test Link', '/url', class: 'nav-link', id: 'link1') + expect(link).to eq "Test Link" + end + + it 'handles options without values' do + link = adapter.link_to('Test', '/url', {}) + expect(link).to eq "Test" + end + + it 'filters out nil values' do + link = adapter.link_to('Test', '/url', class: 'nav', id: nil) + expect(link).to eq "Test" + end + end + + describe '#content_tag' do + it 'returns a tag with the correct type, content and attributes' do + tag = adapter.content_tag(:div, 'Content', class: 'container', id: 'main') + expect(tag).to eq "
Paragraph
" + end + end +end diff --git a/spec/simple_navigation/adapters/padrino_spec.rb b/spec/simple_navigation/adapters/padrino_spec.rb index 0c07a21..a15e340 100644 --- a/spec/simple_navigation/adapters/padrino_spec.rb +++ b/spec/simple_navigation/adapters/padrino_spec.rb @@ -6,6 +6,29 @@ let(:context) { double(:context, request: request) } let(:request) { double(:request) } + describe '.register' do + let(:padrino_module) { Module.new } + let(:padrino_application) { Module.new } + + before do + stub_const('Padrino', padrino_module) + stub_const('Padrino::Application', padrino_application) + allow(padrino_module).to receive_messages(root: '/padrino/root', env: 'production') + allow(SimpleNavigation).to receive(:set_env) + allow(padrino_application).to receive(:send) + end + + it 'calls SimpleNavigation.set_env with Padrino root and env' do + expect(SimpleNavigation).to receive(:set_env).with('/padrino/root', 'production') + described_class.register(nil) + end + + it 'includes SimpleNavigation::Helpers in Padrino::Application' do + expect(padrino_application).to receive(:send).with(:helpers, SimpleNavigation::Helpers) + described_class.register(nil) + end + end + describe '#link_to' do it 'delegates to context' do expect(context).to receive(:link_to).with('name', 'url', { my_option: true }) diff --git a/spec/simple_navigation/adapters/rails_spec.rb b/spec/simple_navigation/adapters/rails_spec.rb index 4c7aa42..3d413ce 100644 --- a/spec/simple_navigation/adapters/rails_spec.rb +++ b/spec/simple_navigation/adapters/rails_spec.rb @@ -79,7 +79,7 @@ end context "when the controller's template is stored as view_context (Rails3)" do - context 'and the template is set' do + context 'when the template is set' do before { allow(controller).to receive_messages(view_context: template) } it "sets the adapter's template accordingly" do @@ -87,7 +87,7 @@ end end - context 'and the template is not set' do + context 'when the template is not set' do before { allow(controller).to receive_messages(view_context: nil) } it "sets the adapter's template to nil" do @@ -101,7 +101,7 @@ context "when the adapter's request is set" do before { allow(adapter).to receive_messages(request: request) } - context 'and request.fullpath is defined' do + context 'when request.fullpath is defined' do let(:request) { double(:request, fullpath: '/fullpath') } it "sets the adapter's request_uri to the request.fullpath" do @@ -109,7 +109,7 @@ end end - context 'and request.fullpath is not defined' do + context 'when request.fullpath is not defined' do let(:request) { double(:request, request_uri: '/request_uri') } before { allow(adapter).to receive_messages(request: request) } @@ -153,7 +153,7 @@ context "when the adapter's controller is set" do before { adapter.instance_variable_set(:@controller, controller) } - context "and the adapter's template is set" do + context "when the adapter's template is set" do before { adapter.instance_variable_set(:@template, template) } it "sets the adapter's context_for_eval to the template" do @@ -161,7 +161,7 @@ end end - context "and the adapter's template is not set" do + context "when the adapter's template is not set" do before { adapter.instance_variable_set(:@template, nil) } it "sets the adapter's context_for_eval to the controller" do @@ -173,7 +173,7 @@ context "when the adapter's controller is not set" do before { adapter.instance_variable_set(:@controller, nil) } - context "and the adapter's template is set" do + context "when the adapter's template is set" do before { adapter.instance_variable_set(:@template, template) } it "sets the adapter's context_for_eval to the template" do @@ -181,7 +181,7 @@ end end - context "and the adapter's template is not set" do + context "when the adapter's template is not set" do before { adapter.instance_variable_set(:@template, nil) } it 'raises an exception' do diff --git a/spec/simple_navigation/adapters/sinatra_spec.rb b/spec/simple_navigation/adapters/sinatra_spec.rb index 4a12bb1..f7ad247 100644 --- a/spec/simple_navigation/adapters/sinatra_spec.rb +++ b/spec/simple_navigation/adapters/sinatra_spec.rb @@ -7,6 +7,19 @@ before { allow(context).to receive_messages(request: request) } + describe '.register' do + let(:sinatra_app) { double(:app, root: '/sinatra/root', environment: 'test') } + + before do + allow(SimpleNavigation).to receive(:set_env) + end + + it 'calls SimpleNavigation.set_env with app root and environment' do + expect(SimpleNavigation).to receive(:set_env).with('/sinatra/root', 'test') + described_class.register(sinatra_app) + end + end + describe '#context_for_eval' do context "when adapter's context is not set" do it 'raises an exception' do diff --git a/spec/simple_navigation/config_file_finder_spec.rb b/spec/simple_navigation/config_file_finder_spec.rb index cd3ae47..e9611b4 100644 --- a/spec/simple_navigation/config_file_finder_spec.rb +++ b/spec/simple_navigation/config_file_finder_spec.rb @@ -14,7 +14,7 @@ context 'when the context is :default' do let(:context) { :default } - context 'and a navigation.rb file is found in one of the paths' do + context 'when a navigation.rb file is found in one of the paths' do before { FileUtils.touch('/path/one/navigation.rb') } it 'returns its full path' do @@ -22,7 +22,7 @@ end end - context 'and no navigation.rb file is found in the paths' do + context 'when no navigation.rb file is found in the paths' do it 'raises an exception' do expect do finder.find(context) @@ -34,7 +34,7 @@ context 'when the context is :other' do let(:context) { :other } - context 'and a other_navigation.rb file is found in one of the paths' do + context 'when a other_navigation.rb file is found in one of the paths' do before { FileUtils.touch('/path/two/other_navigation.rb') } it 'returns its full path' do @@ -42,7 +42,7 @@ end end - context 'and no other_navigation.rb file is found in the paths' do + context 'when no other_navigation.rb file is found in the paths' do it 'raise an exception' do expect do finder.find(context) diff --git a/spec/simple_navigation/configuration_spec.rb b/spec/simple_navigation/configuration_spec.rb index c53a860..c7b8a1a 100644 --- a/spec/simple_navigation/configuration_spec.rb +++ b/spec/simple_navigation/configuration_spec.rb @@ -86,7 +86,7 @@ before { allow(SimpleNavigation::ItemContainer).to receive_messages(new: container) } context 'when a block is given' do - context 'and items_provider is specified' do + context 'when items_provider is specified' do let(:provider) { double(:provider) } it 'raises an exception' do @@ -95,6 +95,7 @@ config.items(provider) do end end.to raise_error(RuntimeError, 'please specify either items_provider or block, but not both') + # rubocop:enable Lint/EmptyBlock end end @@ -116,7 +117,7 @@ end context 'when no block is given' do - context 'and items_provider is specified' do + context 'when items_provider is specified' do let(:external_provider) { double(:external_provider) } let(:items) { double(:items) } let(:items_provider) { double(:items_provider, items: items) } diff --git a/spec/simple_navigation/helpers_spec.rb b/spec/simple_navigation/helpers_spec.rb index 844a6cf..d6725a1 100644 --- a/spec/simple_navigation/helpers_spec.rb +++ b/spec/simple_navigation/helpers_spec.rb @@ -44,7 +44,7 @@ end context 'when an item is selected' do - context "and it's a primary item" do + context "when it's a primary item" do let(:item) { :invoices } it 'returns an empty string' do @@ -67,7 +67,7 @@ end end - context "and it's a sub navigation item" do + context "when it's a sub navigation item" do before do select_an_item(invoices_item) select_an_item(unpaid_item) @@ -118,7 +118,7 @@ end context 'when an item is selected' do - context "and it's a primary item" do + context "when it's a primary item" do let(:item) { :invoices } it 'returns nil for no parameters' do @@ -141,7 +141,7 @@ end end - context "and it's a sub navigation item" do + context "when it's a sub navigation item" do before do select_an_item(invoices_item) select_an_item(unpaid_item) @@ -192,7 +192,7 @@ end context 'when an item is selected' do - context "and it's a primary item" do + context "when it's a primary item" do let(:item) { :invoices } it 'returns nil for no parameters' do @@ -215,7 +215,7 @@ end end - context "and it's a sub navigation item" do + context "when it's a sub navigation item" do before do select_an_item(invoices_item) select_an_item(unpaid_item) @@ -271,7 +271,7 @@ end context 'when an item is selected' do - context "and it's a primary item" do + context "when it's a primary item" do let(:item) { :invoices } it_behaves_like 'returning items container' @@ -283,7 +283,7 @@ end end - context "and it's a sub navigation item" do + context "when it's a sub navigation item" do before do select_an_item(invoices_item) select_an_item(unpaid_item) @@ -339,15 +339,15 @@ end context 'when the :level option is set' do - context 'and its value is 1' do + context 'when its value is 1' do it 'calls render on the primary navigation' do expect(navigation).to receive(:render).with({ level: 1 }) controller.render_navigation(level: 1) end end - context 'and its value is 2' do - context 'and the active_item_container is set' do + context 'when its value is 2' do + context 'when the active_item_container is set' do let(:item_container) { double(:container).as_null_object } before do @@ -366,7 +366,7 @@ end end - context "and the active_item_container isn't set" do + context "when the active_item_container isn't set" do it "doesn't raise an exception" do expect do controller.render_navigation(level: 2) @@ -375,7 +375,7 @@ end end - context "and its value isn't a valid level" do + context "when its value isn't a valid level" do it 'raises an exception' do expect do controller.render_navigation(level: :invalid) @@ -405,10 +405,9 @@ before { allow(SimpleNavigation).to receive_messages(primary_navigation: nil) } it 'raises an exception' do - expect do - controller.render_navigation - end.to raise_error(RuntimeError, - 'no primary navigation defined, either use a navigation config file or pass items directly to render_navigation') + message = 'no primary navigation defined, either use a navigation config file or ' \ + 'pass items directly to render_navigation' + expect { controller.render_navigation }.to raise_error(RuntimeError, message) end end diff --git a/spec/simple_navigation/item_adapter_spec.rb b/spec/simple_navigation/item_adapter_spec.rb index 3b3dfda..6252389 100644 --- a/spec/simple_navigation/item_adapter_spec.rb +++ b/spec/simple_navigation/item_adapter_spec.rb @@ -39,7 +39,7 @@ class ModifiedHash < Hash; end describe '#items' do context 'when item responds to items' do - context 'and items is nil' do + context 'when items is nil' do before { allow(item).to receive_messages(items: nil) } it 'returns nil' do @@ -48,7 +48,7 @@ class ModifiedHash < Hash; end end context 'when items is not nil' do - context 'and items is empty' do + context 'when items is empty' do before { allow(item).to receive_messages(items: []) } it 'returns nil' do @@ -56,7 +56,7 @@ class ModifiedHash < Hash; end end end - context 'and items is not empty' do + context 'when items is not empty' do let(:items) { [true] } before { allow(item).to receive_messages(items: items) } @@ -124,7 +124,7 @@ class ModifiedHash < Hash; end describe '#items' do context 'when item responds to items' do - context 'and items is nil' do + context 'when items is nil' do before { item[:items] = nil } it 'returns nil' do @@ -133,13 +133,13 @@ class ModifiedHash < Hash; end end context 'when items is not nil' do - context 'and items is empty' do + context 'when items is empty' do it 'returns nil' do expect(item_adapter.items).to be_nil end end - context 'and items is not empty' do + context 'when items is not empty' do before { item[:items] = %w[not empty] } it 'returns the items' do diff --git a/spec/simple_navigation/item_container_spec.rb b/spec/simple_navigation/item_container_spec.rb index 77e483a..8ef91bd 100644 --- a/spec/simple_navigation/item_container_spec.rb +++ b/spec/simple_navigation/item_container_spec.rb @@ -37,13 +37,13 @@ context 'when the dom_attributes do not contain any id or class' do let(:dom_attributes) { { test: 'test' } } - context "and the container hasn't any dom_id" do + context "when the container hasn't any dom_id" do it "returns the contaier's dom_attributes without any id" do expect(item_container.dom_attributes).not_to include(:id) end end - context 'and the container has a dom_id' do + context 'when the container has a dom_id' do before { item_container.dom_id = 'test_id' } it "returns the contaier's dom_attributes including the #dom_id" do @@ -51,13 +51,13 @@ end end - context "and the container hasn't any dom_class" do + context "when the container hasn't any dom_class" do it "returns the contaier's dom_attributes without any class" do expect(item_container.dom_attributes).not_to include(:class) end end - context 'and the container has a dom_class' do + context 'when the container has a dom_class' do before { item_container.dom_class = 'test_class' } it "returns the contaier's dom_attributes including the #dom_class" do @@ -100,11 +100,11 @@ end describe '#selected?' do - let(:item_1) { double(:item, selected?: false) } - let(:item_2) { double(:item, selected?: false) } + let(:first_item) { double(:item, selected?: false) } + let(:second_item) { double(:item, selected?: false) } before do - item_container.instance_variable_set(:@items, [item_1, item_2]) + item_container.instance_variable_set(:@items, [first_item, second_item]) end context 'when no item is selected' do @@ -115,34 +115,34 @@ context 'when an item is selected' do it 'returns true' do - allow(item_1).to receive_messages(selected?: true) + allow(first_item).to receive_messages(selected?: true) expect(item_container).to be_selected end end end describe '#selected_item' do - let(:item_1) { double(:item, selected?: false) } - let(:item_2) { double(:item, selected?: false) } + let(:first_item) { double(:item, selected?: false) } + let(:second_item) { double(:item, selected?: false) } before do allow(SimpleNavigation).to receive_messages(current_navigation_for: :nav) allow(item_container).to receive_messages(:[] => nil) - item_container.instance_variable_set(:@items, [item_1, item_2]) + item_container.instance_variable_set(:@items, [first_item, second_item]) end context "when navigation isn't explicitely set" do - context 'and no item is selected' do + context 'when no item is selected' do it 'returns nil' do expect(item_container.selected_item).to be_nil end end - context 'and an item selected' do - before { allow(item_1).to receive_messages(selected?: true) } + context 'when an item is selected' do + before { allow(first_item).to receive_messages(selected?: true) } it 'returns the selected item' do - expect(item_container.selected_item).to be item_1 + expect(item_container.selected_item).to be first_item end end end @@ -156,7 +156,7 @@ end context "when the desired level is different than the container's" do - context 'and no subnavigation is selected' do + context 'when no subnavigation is selected' do before { allow(item_container).to receive_messages(selected_sub_navigation?: false) } it 'returns nil' do @@ -164,7 +164,7 @@ end end - context 'and a subnavigation is selected' do + context 'when a subnavigation is selected' do let(:sub_navigation) { double(:sub_navigation) } let(:selected_item) { double(:selected_item) } @@ -269,19 +269,19 @@ end context 'when item is specified with only options' do - context 'and options do not contain any condition' do + context 'when options do not contain any condition' do it_behaves_like 'adding the item to the list' do let(:args) { ['key', 'name', { option: true }] } end end - context 'and options contains a negative condition' do + context 'when options contains a negative condition' do it_behaves_like 'not adding the item to the list' do let(:args) { ['key', 'name', nil, { if: -> { false }, option: true }] } end end - context 'and options contains a positive condition' do + context 'when options contains a positive condition' do it_behaves_like 'adding the item to the list' do let(:args) { ['key', 'name', nil, { if: -> { true }, option: true }] } end @@ -289,19 +289,19 @@ end context 'when item is specified with a url and options' do - context 'and options do not contain any condition' do + context 'when options do not contain any condition' do it_behaves_like 'adding the item to the list' do let(:args) { ['key', 'name', 'url', { option: true }] } end end - context 'and options contains a negative condition' do + context 'when options contains a negative condition' do it_behaves_like 'not adding the item to the list' do let(:args) { ['key', 'name', 'url', { if: -> { false }, option: true }] } end end - context 'and options contains a positive condition' do + context 'when options contains a positive condition' do it_behaves_like 'adding the item to the list' do let(:args) { ['key', 'name', 'url', { if: -> { true }, option: true }] } end @@ -329,7 +329,7 @@ context 'when the container :id option is specified' do let(:options) { { container: { id: 'c_id' } } } - context 'and the item should be added' do + context 'when the item should be added' do let(:add_item) { true } it 'changes its dom_id' do @@ -337,7 +337,7 @@ end end - context "and the item shouldn't be added" do + context "when the item shouldn't be added" do let(:add_item) { false } it "doesn't change its dom_id" do @@ -349,7 +349,7 @@ context 'when the container :class option is specified' do let(:options) { { container: { class: 'c_class' } } } - context 'and the item should be added' do + context 'when the item should be added' do let(:add_item) { true } it 'changes its dom_class' do @@ -357,7 +357,7 @@ end end - context "and the item shouldn't be added" do + context "when the item shouldn't be added" do let(:add_item) { false } it "doesn't change its dom_class" do @@ -369,7 +369,7 @@ context 'when the container :attributes option is specified' do let(:options) { { container: { attributes: { option: true } } } } - context 'and the item should be added' do + context 'when the item should be added' do let(:add_item) { true } it 'changes its dom_attributes' do @@ -377,7 +377,7 @@ end end - context "and the item shouldn't be added" do + context "when the item shouldn't be added" do let(:add_item) { false } it "doesn't change its dom_attributes" do @@ -389,7 +389,7 @@ context 'when the container :selected_class option is specified' do let(:options) { { container: { selected_class: 'sel_class' } } } - context 'and the item should be added' do + context 'when the item should be added' do let(:add_item) { true } it 'changes its selected_class' do @@ -397,7 +397,7 @@ end end - context "and the item shouldn't be added" do + context "when the item shouldn't be added" do let(:add_item) { false } it "doesn't change its selected_class" do @@ -413,7 +413,7 @@ let(:options) { { if: proc { condition } } } let(:condition) { nil } - context 'and it evals to true' do + context 'when it evals to true' do let(:condition) { true } it 'creates a new Item' do @@ -422,7 +422,7 @@ end end - context 'and it evals to false' do + context 'when it evals to false' do let(:condition) { false } it "doesn't create a new Item" do @@ -431,7 +431,7 @@ end end - context 'and it is not a proc or a method' do + context 'when it is not a proc or a method' do it 'raises an error' do expect do item_container.item('key', 'name', 'url', { if: 'text' }) @@ -444,7 +444,7 @@ let(:options) { { unless: proc { condition } } } let(:condition) { nil } - context 'and it evals to false' do + context 'when it evals to false' do let(:condition) { false } it 'creates a new Navigation-Item' do @@ -453,7 +453,7 @@ end end - context 'and it evals to true' do + context 'when it evals to true' do let(:condition) { true } it "doesn't create a new Navigation-Item" do @@ -489,7 +489,7 @@ let(:renderer_class) { double(:renderer_class, new: renderer_instance) } context 'when renderer is specified as an option' do - context 'and is specified as a class' do + context 'when it is specified as a class' do it 'instantiates the passed renderer_class with the options' do expect(renderer_class).to receive(:new) .with({ renderer: renderer_class }) @@ -502,7 +502,7 @@ end end - context 'and is specified as a symbol' do + context 'when it is specified as a symbol' do before do SimpleNavigation.registered_renderers = { my_renderer: renderer_class diff --git a/spec/simple_navigation/item_spec.rb b/spec/simple_navigation/item_spec.rb index 01414f2..e889597 100644 --- a/spec/simple_navigation/item_spec.rb +++ b/spec/simple_navigation/item_spec.rb @@ -44,7 +44,7 @@ end context 'when no block is given' do - context 'and items are given' do + context 'when items are given' do let(:items) { [] } let(:options) { { items: items } } @@ -55,7 +55,7 @@ end end - context 'and no items are given' do + context 'when no items are given' do it "doesn't create a new ItemContainer" do item = described_class.new(*item_args) expect(item.sub_navigation).to be_nil @@ -94,13 +94,13 @@ end context 'when a url is given' do - context 'and it is a string' do + context 'when it is a string' do it "sets the item's url accordingly" do expect(item.url).to eq 'url' end end - context 'and it is a proc' do + context 'when it is a proc' do let(:url) { proc { 'my_url' } } it "sets the item's url accordingly" do @@ -108,7 +108,7 @@ end end - context 'and it is nil' do + context 'when it is nil' do let(:url) { nil } it "sets the item's url accordingly" do @@ -202,13 +202,13 @@ end context 'when no option is given' do - context 'and the name_generator uses only the name' do + context 'when the name_generator uses only the name' do it 'uses the default name_generator' do expect(item.name).to eq 'name' end end - context 'and the name_generator uses only the item itself' do + context 'when the name_generator uses only the item itself' do before do allow(SimpleNavigation.config).to \ receive_messages(name_generator: proc { |_name, item| "#{item.key}" }) @@ -239,7 +239,7 @@ context 'when the item has no :highlights_on option' do before { allow(SimpleNavigation).to receive_messages(config: config) } - context 'and auto highlighting is off' do + context 'when auto highlighting is off' do let(:config) { double(:config, auto_highlight: false) } it 'returns false' do @@ -247,13 +247,13 @@ end end - context 'and auto highlighting is on' do + context 'when auto highlighting is on' do let(:config) do double(:config, ignore_query_params_on_auto_highlight: true, ignore_anchors_on_auto_highlight: true, auto_highlight: true) end - context "and the current url matches the item's url" do + context "when the current url matches the item's url" do before { allow(adapter).to receive_messages(current_page?: true) } it 'returns true' do @@ -261,7 +261,7 @@ end end - context "and the current url does not match the item's url" do + context "when the current url does not match the item's url" do let(:config) do double(:config, auto_highlight: false, highlight_on_subpath: false) end @@ -273,13 +273,13 @@ end end - context 'and highlights_on_subpath is on' do + context 'when highlights_on_subpath is on' do let(:config) do double(:config, auto_highlight: true, highlight_on_subpath: true, ignore_query_params_on_auto_highlight: true, ignore_anchors_on_auto_highlight: true) end - context 'but item has no url' do + context 'when item has no url' do let(:url) { nil } it 'returns false' do @@ -287,7 +287,7 @@ end end - context "and the current url is a sub path of the item's url" do + context "when the current url is a sub path of the item's url" do before do allow(adapter).to \ receive_messages(current_page?: false, request_uri: 'url/test') @@ -298,7 +298,7 @@ end end - context "and the current url is not a sub path of the item's url" do + context "when the current url is not a sub path of the item's url" do before do allow(adapter).to \ receive_messages(current_page?: false, request_uri: 'other/test') @@ -313,10 +313,10 @@ end context 'when the item has a :highlights_on option' do - context 'and it is a regular expression' do + context 'when it is a regular expression' do before { allow(adapter).to receive_messages(request_uri: '/test') } - context 'and the current url matches the expression' do + context 'when the current url matches the expression' do let(:options) { { highlights_on: /test/ } } it 'returns true' do @@ -324,7 +324,7 @@ end end - context 'and the current url does not match the expression' do + context 'when the current url does not match the expression' do let(:options) { { highlights_on: /other/ } } it 'returns false' do @@ -333,8 +333,8 @@ end end - context 'and it is a callable object' do - context 'and the call returns true' do + context 'when it is a callable object' do + context 'when the call returns true' do let(:options) { { highlights_on: -> { true } } } it 'returns true' do @@ -342,7 +342,7 @@ end end - context 'and the call returns false' do + context 'when the call returns false' do let(:options) { { highlights_on: -> { false } } } it 'returns false' do @@ -351,10 +351,10 @@ end end - context 'and it is the :subpath symbol' do + context 'when it is the :subpath symbol' do let(:options) { { highlights_on: :subpath } } - context "and the current url is a sub path of the item's url" do + context "when the current url is a sub path of the item's url" do before do allow(adapter).to receive_messages(request_uri: 'url/test') end @@ -364,7 +364,7 @@ end end - context "and the current url is not a sub path of the item's url" do + context "when the current url is not a sub path of the item's url" do before do allow(adapter).to receive_messages(request_uri: 'other/test') end @@ -375,7 +375,7 @@ end end - context 'and it is non usable' do + context 'when it is non usable' do let(:options) { { highlights_on: :hello } } it 'raises an exception' do @@ -393,7 +393,7 @@ expect(item.selected_class).to eq 'selected' end - context 'and selected_class is defined in the context' do + context 'when selected_class is defined in the context' do before { allow(item_container).to receive_messages(selected_class: 'defined') } it "returns the context's selected_class" do @@ -417,7 +417,7 @@ context 'when the :class option is given' do let(:options) { { html: { class: 'my_class' } } } - context 'and the item is selected' do + context 'when the item is selected' do before { allow(item).to receive_messages(selected?: true, selected_by_condition?: true) } it "adds the specified class to the item's html classes" do @@ -429,7 +429,7 @@ end end - context "and the item isn't selected" do + context "when the item isn't selected" do before { allow(item).to receive_messages(selected?: false, selected_by_condition?: false) } it "sets the specified class as the item's html classes" do @@ -439,7 +439,7 @@ end context "when the :class option isn't given" do - context 'and the item is selected' do + context 'when the item is selected' do before { allow(item).to receive_messages(selected?: true, selected_by_condition?: true) } it 'sets the default html classes of a selected item' do @@ -447,7 +447,7 @@ end end - context "and the item isn't selected" do + context "when the item isn't selected" do before { allow(item).to receive_messages(selected?: false, selected_by_condition?: false) } it "doesn't set any html class on the item" do @@ -470,13 +470,13 @@ allow(item).to receive_messages(selected?: false, selected_by_condition?: false) end - context 'and :autogenerate_item_ids is true' do + context 'when :autogenerate_item_ids is true' do let(:generate_ids) { true } it_behaves_like 'generating id', 'my_id' end - context 'and :autogenerate_item_ids is false' do + context 'when :autogenerate_item_ids is false' do let(:generate_ids) { false } it_behaves_like 'generating id', 'my_id' @@ -489,13 +489,13 @@ allow(item).to receive_messages(selected?: false, selected_by_condition?: false) end - context 'and :autogenerate_item_ids is true' do + context 'when :autogenerate_item_ids is true' do let(:generate_ids) { true } it_behaves_like 'generating id', 'my_key' end - context 'and :autogenerate_item_ids is false' do + context 'when :autogenerate_item_ids is false' do let(:generate_ids) { false } it "doesn't set any html id on the item" do diff --git a/spec/simple_navigation/items_provider_spec.rb b/spec/simple_navigation/items_provider_spec.rb index 160d123..a221859 100644 --- a/spec/simple_navigation/items_provider_spec.rb +++ b/spec/simple_navigation/items_provider_spec.rb @@ -25,7 +25,7 @@ end end - context 'provider is a collection' do + context 'when provider is a collection' do let(:provider) { [] } it 'retrieves the items by returning the provider' do diff --git a/spec/simple_navigation/renderer/base_spec.rb b/spec/simple_navigation/renderer/base_spec.rb index f5d0c80..98dd5da 100644 --- a/spec/simple_navigation/renderer/base_spec.rb +++ b/spec/simple_navigation/renderer/base_spec.rb @@ -38,7 +38,7 @@ describe '#expand_all?' do context 'when :options is set' do - context 'and the :expand_all option is true' do + context 'when the :expand_all option is true' do let(:options) { { expand_all: true } } it 'returns true' do @@ -46,7 +46,7 @@ end end - context 'and the :expand_all option is false' do + context 'when the :expand_all option is false' do let(:options) { { expand_all: false } } it 'returns false' do @@ -66,7 +66,7 @@ describe '#skip_if_empty?' do context 'when :options is set' do - context 'and the :skip_if_empty option is true' do + context 'when the :skip_if_empty option is true' do let(:options) { { skip_if_empty: true } } it 'returns true' do @@ -74,7 +74,7 @@ end end - context 'and the :skip_if_empty option is false' do + context 'when the :skip_if_empty option is false' do let(:options) { { skip_if_empty: false } } it 'returns true' do @@ -93,7 +93,7 @@ end describe '#level' do - context 'and the :level option is set' do + context 'when the :level option is set' do let(:options) { { level: 1 } } it 'returns the specified level' do @@ -101,7 +101,7 @@ end end - context "and the :level option isn't set" do + context "when the :level option isn't set" do let(:options) { {} } it 'returns :all' do @@ -126,7 +126,7 @@ context 'when the item has sub navigation' do let(:sub_navigation) { double(:sub_navigation) } - context 'and the renderer has an unknown level' do + context 'when the renderer has an unknown level' do before { allow(base).to receive_messages(level: 'unknown') } it 'returns false' do @@ -134,7 +134,7 @@ end end - context 'and the renderer has a level set to :all' do + context 'when the renderer has a level set to :all' do before { allow(base).to receive_messages(level: :all) } it 'returns false' do @@ -153,7 +153,7 @@ context "when the renderer's level is a Range" do before { allow(base).to receive_messages(level: 2..3) } - context "and sub navigation's level is greater than range.max" do + context "when sub navigation's level is greater than range.max" do before { allow(sub_navigation).to receive_messages(level: 4) } it 'returns false' do @@ -161,7 +161,7 @@ end end - context "and sub navigation's level is equal to range.max" do + context "when sub navigation's level is equal to range.max" do before { allow(sub_navigation).to receive_messages(level: 3) } it 'returns true' do @@ -169,7 +169,7 @@ end end - context "and sub navigation's level is equal to range.min" do + context "when sub navigation's level is equal to range.min" do before { allow(sub_navigation).to receive_messages(level: 2) } it 'returns true' do @@ -186,7 +186,7 @@ context 'when consider_sub_navigation? is true' do before { allow(base).to receive_messages(consider_sub_navigation?: true) } - context 'and expand_sub_navigation? is true' do + context 'when expand_sub_navigation? is true' do before { allow(base).to receive_messages(expand_sub_navigation?: true) } it 'returns true' do @@ -194,7 +194,7 @@ end end - context 'and expand_sub_navigation? is false' do + context 'when expand_sub_navigation? is false' do before { allow(base).to receive_messages(expand_sub_navigation?: false) } it 'returns false' do @@ -203,10 +203,10 @@ end end - context 'consider_sub_navigation? is false' do + context 'when consider_sub_navigation? is false' do before { allow(base).to receive_messages(consider_sub_navigation?: false) } - context 'and expand_sub_navigation? is true' do + context 'when expand_sub_navigation? is true' do before { allow(base).to receive_messages(expand_sub_navigation?: true) } it 'returns false' do @@ -214,7 +214,7 @@ end end - context 'and expand_sub_navigation? is false' do + context 'when expand_sub_navigation? is false' do before { allow(base).to receive_messages(expand_sub_navigation?: false) } it 'returns false' do diff --git a/spec/simple_navigation/renderer/breadcrumbs_spec.rb b/spec/simple_navigation/renderer/breadcrumbs_spec.rb index 8737763..86340eb 100644 --- a/spec/simple_navigation/renderer/breadcrumbs_spec.rb +++ b/spec/simple_navigation/renderer/breadcrumbs_spec.rb @@ -44,20 +44,20 @@ expect(output).to have_no_css('div a[class]') end - context 'and the :allow_classes_and_ids option is true' do + context 'when the :allow_classes_and_ids option is true' do let(:options) { { level: :all, allow_classes_and_ids: true } } it "renders the 'a' tag with the selected class" do expect(output).to have_css('div a.selected') end - context "and the item hasn't any id explicitly set" do + context "when the item hasn't any id explicitly set" do it "renders the 'a' tag without any html id" do expect(output).to have_no_css('div a[id]') end end - context 'and the item has an explicitly set id' do + context 'when the item has an explicitly set id' do let(:item) { :users } it "renders the 'a' tag with an html id" do @@ -67,10 +67,10 @@ end end - context 'and the :prefix option is set' do + context 'when the :prefix option is set' do let(:options) { { prefix: 'You are here: ' } } - context 'and there are no items to render' do + context 'when there are no items to render' do let(:item) { nil } it "doesn't render the prefix before the breadcrumbs" do @@ -78,7 +78,7 @@ end end - context 'and there are items to render' do + context 'when there are items to render' do let(:item) { :invoices } it 'renders the prefix before the breadcrumbs' do diff --git a/spec/simple_navigation/renderer/list_spec.rb b/spec/simple_navigation/renderer/list_spec.rb index 67e8bcc..7e082da 100644 --- a/spec/simple_navigation/renderer/list_spec.rb +++ b/spec/simple_navigation/renderer/list_spec.rb @@ -101,5 +101,15 @@ expect(output).to have_no_css('li#invoices > a[aria-current=page]') end end + + context 'when skip_if_empty is true and container is empty' do + let(:empty_navigation) { SimpleNavigation::ItemContainer.new(1) } + let(:options) { { skip_if_empty: true } } + let(:output) { renderer.render(empty_navigation) } + + it 'returns an empty string' do + expect(output).to eq '' + end + end end end diff --git a/spec/simple_navigation/renderer/text_spec.rb b/spec/simple_navigation/renderer/text_spec.rb index 583afb2..1d3680b 100644 --- a/spec/simple_navigation/renderer/text_spec.rb +++ b/spec/simple_navigation/renderer/text_spec.rb @@ -37,7 +37,7 @@ expect(output).to eq 'Invoices Unpaid' end - context 'and the :join_with option is set' do + context 'when the :join_with option is set' do let(:options) { { level: :all, join_with: ' | ' } } it 'separates the items with the specified separator' do diff --git a/spec/simple_navigation_spec.rb b/spec/simple_navigation_spec.rb index 737e7c9..ae6e761 100644 --- a/spec/simple_navigation_spec.rb +++ b/spec/simple_navigation_spec.rb @@ -1,57 +1,59 @@ # frozen_string_literal: true RSpec.describe SimpleNavigation do - before { subject.config_file_path = 'path_to_config' } + subject(:simple_navigation) { described_class } + + before { simple_navigation.config_file_path = 'path_to_config' } describe 'config_file_path=' do - before { subject.config_file_paths = ['existing_path'] } + before { simple_navigation.config_file_paths = ['existing_path'] } it 'overrides the config_file_paths' do - subject.config_file_path = 'new_path' - expect(subject.config_file_paths).to eq ['new_path'] + simple_navigation.config_file_path = 'new_path' + expect(simple_navigation.config_file_paths).to eq ['new_path'] end end describe '.default_config_file_path' do - before { allow(subject).to receive_messages(root: 'root') } + before { allow(simple_navigation).to receive_messages(root: 'root') } it 'returns the config file path according to :root setting' do - expect(subject.default_config_file_path).to eq 'root/config' + expect(simple_navigation.default_config_file_path).to eq 'root/config' end end describe 'Regarding renderers' do it 'registers the builtin renderers by default' do - expect(subject.registered_renderers).not_to be_empty + expect(simple_navigation.registered_renderers).not_to be_empty end describe '.register_renderer' do let(:renderer) { double(:renderer) } it 'adds the specified renderer to the list of renderers' do - subject.register_renderer(my_renderer: renderer) - expect(subject.registered_renderers[:my_renderer]).to be renderer + simple_navigation.register_renderer(my_renderer: renderer) + expect(simple_navigation.registered_renderers[:my_renderer]).to be renderer end end end describe '.set_env' do before do - subject.config_file_paths = [] - allow(subject).to receive_messages(default_config_file_path: 'default_path') - subject.set_env('root', 'my_env') + simple_navigation.config_file_paths = [] + allow(simple_navigation).to receive_messages(default_config_file_path: 'default_path') + simple_navigation.set_env('root', 'my_env') end it 'sets the root' do - expect(subject.root).to eq 'root' + expect(simple_navigation.root).to eq 'root' end it 'sets the environment' do - expect(subject.environment).to eq 'my_env' + expect(simple_navigation.environment).to eq 'my_env' end it 'adds the default-config path to the list of config_file_paths' do - expect(subject.config_file_paths).to eq ['default_path'] + expect(simple_navigation.config_file_paths).to eq ['default_path'] end end @@ -60,7 +62,7 @@ before do FileUtils.mkdir_p(paths) - allow(subject).to receive_messages(config_file_paths: paths) + allow(simple_navigation).to receive_messages(config_file_paths: paths) end context 'when the config file for the context exists' do @@ -71,35 +73,35 @@ context 'when no context is provided' do it 'stores the configuration in config_files for the default context' do - subject.load_config - expect(subject.config_files[:default]).to eq "default content\n" + simple_navigation.load_config + expect(simple_navigation.config_files[:default]).to eq "default content\n" end end context 'when a context is provided' do it 'stores the configuration in config_files for the given context' do - subject.load_config(:other) - expect(subject.config_files[:other]).to eq "other content\n" + simple_navigation.load_config(:other) + expect(simple_navigation.config_files[:other]).to eq "other content\n" end end - context 'and environment is production' do - before { allow(subject).to receive_messages(environment: 'production') } + context 'when environment is production' do + before { allow(simple_navigation).to receive_messages(environment: 'production') } it 'loads the config file only for the first call' do - subject.load_config + simple_navigation.load_config File.open('/path/two/navigation.rb', 'w') { |f| f.puts 'new content' } - subject.load_config - expect(subject.config_files[:default]).to eq "default content\n" + simple_navigation.load_config + expect(simple_navigation.config_files[:default]).to eq "default content\n" end end - context "and environment isn't production" do + context "when environment isn't production" do it 'loads the config file for every call' do - subject.load_config + simple_navigation.load_config File.open('/path/two/navigation.rb', 'w') { |f| f.puts 'new content' } - subject.load_config - expect(subject.config_files[:default]).to eq "new content\n" + simple_navigation.load_config + expect(simple_navigation.config_files[:default]).to eq "new content\n" end end end @@ -107,7 +109,7 @@ context "when the config file for the context doesn't exists" do it 'raises an exception' do expect do - subject.load_config + simple_navigation.load_config end.to raise_error(RuntimeError, /Config file 'navigation.rb' not found in path\(s\)/) end end @@ -115,18 +117,18 @@ describe '.config' do it 'returns the Configuration singleton instance' do - expect(subject.config).to be SimpleNavigation::Configuration.instance + expect(simple_navigation.config).to be SimpleNavigation::Configuration.instance end end describe '.active_item_container_for' do let(:primary) { double(:primary) } - before { allow(subject.config).to receive_messages(primary_navigation: primary) } + before { allow(simple_navigation.config).to receive_messages(primary_navigation: primary) } context 'when level is :all' do it 'returns the primary_navigation' do - nav = subject.active_item_container_for(:all) + nav = simple_navigation.active_item_container_for(:all) expect(nav).to be primary end end @@ -134,28 +136,28 @@ context 'when level is :leaves' do it 'returns the currently active leaf-container' do expect(primary).to receive(:active_leaf_container) - subject.active_item_container_for(:leaves) + simple_navigation.active_item_container_for(:leaves) end end context 'when level is a Range' do it 'takes the min of the range to lookup the active container' do expect(primary).to receive(:active_item_container_for).with(2) - subject.active_item_container_for(2..3) + simple_navigation.active_item_container_for(2..3) end end context 'when level is an Integer' do it 'considers the Integer to lookup the active container' do expect(primary).to receive(:active_item_container_for).with(1) - subject.active_item_container_for(1) + simple_navigation.active_item_container_for(1) end end context 'when level is something else' do it 'raises an exception' do expect do - subject.active_item_container_for('something else') + simple_navigation.active_item_container_for('something else') end.to raise_error(ArgumentError, 'Invalid navigation level: something else') end end @@ -165,13 +167,13 @@ shared_examples 'loading the right adapter' do |framework, adapter| context "when the context is #{framework}" do before do - allow(subject).to receive_messages(framework: framework) - subject.load_adapter + allow(simple_navigation).to receive_messages(framework: framework) + simple_navigation.load_adapter end it "returns the #{framework} adapter" do adapter_class = SimpleNavigation::Adapters.const_get(adapter) - expect(subject.adapter_class).to be adapter_class + expect(simple_navigation.adapter_class).to be adapter_class end end end @@ -179,6 +181,7 @@ it_behaves_like 'loading the right adapter', :rails, :Rails it_behaves_like 'loading the right adapter', :padrino, :Padrino it_behaves_like 'loading the right adapter', :sinatra, :Sinatra + it_behaves_like 'loading the right adapter', :nanoc, :Nanoc end describe '.init_adapter_from' do @@ -186,9 +189,9 @@ let(:adapter_class) { double(:adapter_class, new: adapter) } it 'sets the adapter to a new instance of adapter_class' do - subject.adapter_class = adapter_class - subject.init_adapter_from(:default) - expect(subject.adapter).to be adapter + simple_navigation.adapter_class = adapter_class + simple_navigation.init_adapter_from(:default) + expect(simple_navigation.adapter).to be adapter end end end