require 'spec_helper' describe 'Locomotive::Steam::Liquid::Tags::Nav' do let(:index) { instance_double('IndexPage', fullpath: 'index', published: true) } let(:depth_1) do [ instance_double('Child1', title: 'Child #1', slug: 'child-1', fullpath: 'child-1', published?: true, listed?: true, templatized?: false, localized_attributes: [], to_liquid: { 'title' => 'Child #1' }), instance_double('Child2', title: 'Child #2', slug: 'child-2', fullpath: 'child-2', published?: true, listed?: true, templatized?: false, localized_attributes: [], to_liquid: { 'title' => 'Child #2' }) ] end let(:depth_2) do [ instance_double('Child2_1', title: 'Child #2.1', slug: 'child-2-1', fullpath: 'child-2/child-2-1', published?: true, listed?: true, templatized?: false, localized_attributes: []), instance_double('Child2_2', title: 'Child #2.2', slug: 'child-2-2', fullpath: 'child-2/child-2-2', published?: true, listed?: true, templatized?: false, localized_attributes: []), instance_double('UnpublishedChild2_3', title: 'Child #2.3', slug: 'child-2-3', fullpath: 'child-2/child-2-3', published?: false, listed?: true, templatized?: false, localized_attributes: []), instance_double('TemplatizedChild2_4', title: 'Child #2.4', slug: 'child-2-4', fullpath: 'child-2/child-2-4', published?: true, listed?: true, templatized?: true, localized_attributes: []), instance_double('UnlistedChild2_4', title: 'Child #2.5', slug: 'child-2-5', fullpath: 'child-2/child-2-5', published?: true, listed?: false, templatized?: false, localized_attributes: []) ] end let(:source) { '{% nav site %}' } let(:site) { instance_double('Site', name: 'My portfolio', default_locale: 'en') } let(:page) { index } let(:services) { Locomotive::Steam::Services.build_instance } let(:repository) { services.repositories.page } let(:assigns) { {} } let(:registers) { { services: services, site: site, page: page } } let(:context) { ::Liquid::Context.new(assigns, {}, registers) } let(:options) { { services: services } } let(:output) { render_template(source, context, options) } before { allow(services).to receive(:current_site).and_return(site) } describe 'rendering' do subject { output } describe 'from a site' do before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to eq %{} } end describe 'from a page' do let(:source) { '{% nav page %}' } before do allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to eq %{} } end describe 'from the parent page' do let(:source) { '{% nav parent %}' } describe 'no parent page, use the current page instead' do before do allow(repository).to receive(:parent_of).with(index).and_return(nil) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to eq %{} } end end describe 'from a page' do let(:source) { '{% nav index %}' } describe 'no parent page, use the current page instead' do before do allow(repository).to receive(:by_fullpath).with('index').and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to eq %{} } end end describe 'no wrapper' do let(:source) { '{% nav site, no_wrapper: true %}' } before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to eq %{\n} } end describe 'with icons' do before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end describe 'before' do let(:source) { '{% nav site, icon: before, no_wrapper: true %}' } it { is_expected.to include %{} } end describe 'after' do let(:source) { '{% nav site, icon: after, no_wrapper: true %}' } it { is_expected.to include %{} } end end describe 'including the second levels of pages (depth = 2)' do let(:source) { '{% nav site, depth: 2 %}' } before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) allow(repository).to receive(:children_of).with(depth_1.first).and_return([]) allow(repository).to receive(:children_of).with(depth_1.last).and_return(depth_2) end it { is_expected.to eq %{} } describe 'with bootstrap' do let(:source) { '{% nav site, bootstrap: true, depth: 2 %}' } it { is_expected.to eq %{} } end describe 'excluding pages' do let(:source) { '{% nav site, depth: 2, exclude: "child-2/child-2-2" %}' } it { is_expected.to eq %{} } end end describe 'using a snippet to render the title' do let(:source) { %({% nav site, snippet: "{{page.title}}!" %}) } before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) allow(repository).to receive(:root).and_return(index) end it { is_expected.to include %{Child #1!} } describe 'from a registered snippet' do let(:source) { %({% nav site, snippet: nav_title %}) } let(:snippet) { instance_double('Snippet', source: '{{ page.title }}!') } before do allow(services.snippet_finder).to receive(:find).with('nav_title').and_return(snippet) end it { is_expected.to include %{Child #1!} } end end describe 'changing the dom id and the class' do let(:source) { %({% nav site, id: "main-nav", class: "nav" %}) } before do allow(repository).to receive(:root).and_return(index) allow(repository).to receive(:children_of).with(index).and_return(depth_1) end it { is_expected.to include %{