require 'spec_helper' describe Alchemy::Admin::NavigationHelper do let(:alchemy_module) { { 'name' => 'dashboard', 'engine_name' => 'alchemy', 'navigation' => { 'name' => 'modules.dashboard', 'controller' => 'alchemy/admin/dashboard', 'action' => 'index', 'icon' => 'dashboard', 'sub_navigation' => [{ 'controller' => 'alchemy/admin/layoutpages', 'action' => 'index' }] } } } let(:event_module) { { 'navigation' => { 'controller' => '/admin/events', 'action' => 'index', 'sub_navigation' => [{ 'controller' => '/admin/events', 'action' => 'index' }] } } } let(:event_module_with_params) { { 'navigation' => { 'controller' => '/admin/events', 'action' => 'index', 'params' => { 'key' => 'value' }, 'sub_navigation' => [{ 'controller' => '/admin/events', 'action' => 'index', 'params' => { 'key' => 'value', 'key2' => 'value2' } }] } } } let(:navigation) { alchemy_module['navigation'] } describe '#alchemy_main_navigation_entry' do before do allow(helper).to receive(:url_for_module).and_return('') allow(helper).to receive(:_t).and_return(alchemy_module['name']) end context "with permission" do before do expect(helper).to receive(:can?).and_return(true) end it "renders the main navigation entry partial" do expect(helper.alchemy_main_navigation_entry(alchemy_module)).to match / 'alchemy/admin/dashboard', 'action' => 'index'} end context "with active entry" do before do allow(helper).to receive(:params).and_return({ controller: 'alchemy/admin/dashboard', action: 'index' }) end it "returns true" do expect(helper.entry_active?(entry)).to be_truthy end context "and with leading slash in controller name" do before { entry['controller'] = '/alchemy/admin/dashboard' } it "returns true" do expect(helper.entry_active?(entry)).to be_truthy end end context "but with action listed in nested_actions key" do before do entry['action'] = nil entry['nested_actions'] = %w(index) end it "returns true" do expect(helper.entry_active?(entry)).to be_truthy end end end context "with inactive entry" do before do expect(helper).to receive(:params).and_return({ controller: 'alchemy/admin/users', action: 'index' }) end it "returns false" do expect(helper.entry_active?(entry)).to be_falsey end end end describe '#url_for_module' do context "with module within an engine" do it "returns correct url string" do expect(helper.url_for_module(alchemy_module)).to eq('/admin/dashboard') end end context "with module within host app" do it "returns correct url string" do expect(helper.url_for_module(event_module)).to eq('/admin/events') end it "returns correct url string with params" do expect(helper.url_for_module(event_module_with_params)).to eq('/admin/events?key=value') end end end describe '#url_for_module_sub_navigation' do subject { helper.url_for_module_sub_navigation(navigation) } let(:current_module) { alchemy_module } let(:navigation) { current_module['navigation']['sub_navigation'].first } context 'with module found' do before do expect(helper).to receive(:module_definition_for).and_return current_module end context "with module within an engine" do let(:current_module) { alchemy_module } it "returns correct url string" do is_expected.to eq('/admin/layoutpages') end end context "with module within host app" do let(:current_module) { event_module } it "returns correct url string" do is_expected.to eq('/admin/events') end end context "with module within host app with params" do let(:current_module) { event_module_with_params } it "returns correct url string with params" do is_expected.to eq('/admin/events?key2=value2&key=value') end end end context 'without module found' do before do expect(helper).to receive(:module_definition_for).and_return nil end it { is_expected.to be_nil } end end describe "#sorted_alchemy_modules" do subject { helper.sorted_alchemy_modules } context 'with position attribute on modules' do before do alchemy_module['position'] = 1 event_module['position'] = 2 expect(helper).to receive(:alchemy_modules).and_return [event_module, alchemy_module] end it "returns sorted alchemy modules" do is_expected.to eq([alchemy_module, event_module]) end end context 'with no position attribute on one module' do before do event_module['position'] = 2 expect(helper).to receive(:alchemy_modules).and_return [alchemy_module, event_module] end it "appends this module at the end" do is_expected.to eq([event_module, alchemy_module]) end end end end