require 'spec_helper'
describe Locomotive::Steam::Liquid::Tags::Section do
let(:request) { instance_double('Request', env: {}) }
let(:services) { Locomotive::Steam::Services.build_instance(request) }
let(:finder) { services.section_finder }
let(:source) { 'Locomotive {% section header %}' }
let(:live_editing) { true }
let(:content) { {} }
let(:alt_content) { nil }
let(:file_system) { Locomotive::Steam::Liquid::FileSystem.new(section_finder: finder) }
let(:page) { liquid_instance_double('Page', sections_content: content)}
let(:assigns) { { 'page' => page } }
let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, live_editing: live_editing, _section_content: alt_content, file_system: file_system }, rethrow_errors: true) }
describe 'parsing' do
subject { parse_template(source) }
describe 'raises an error if the syntax is incorrect' do
let(:source) { 'Locomotive {% section %}' }
it { expect { subject }.to raise_exception(Liquid::SyntaxError) }
end
end
describe 'rendering' do
before do
allow(finder).to receive(:find).and_return(section)
end
subject { render_template(source, context) }
let(:definition) { {
type: 'header',
class: 'my-awesome-header',
settings: [
{ id: 'brand', type: 'text', label: 'Brand' },
{ id: 'image', type: 'image_picker' }
],
blocks: [
{ type: 'menu_item', settings: [
{ id: 'title', type: 'text' },
{ id: 'image', type: 'image_picker' }
]}
],
default: {
settings: { brand: 'NoCoffee', image: 'foo.png' },
blocks: [{ id: 42, type: 'menu_item', settings: { title: 'Home', image: 'foo.png' } }] }
}.deep_stringify_keys }
let(:section) { instance_double(
'Header',
slug: 'header',
type: 'header',
liquid_source: liquid_source,
definition: definition,
)}
context 'no block' do
let(:liquid_source) { %(built by \n\t{{ section.settings.brand }}) }
it { is_expected.to eq 'Locomotive'\
'
' }
context 'capturing the setting in a liquid variable' do
let(:liquid_source) { %({% capture brand %}{{ section.settings.brand }}{% endcapture %}built by \n\t{{ brand }}) }
it { is_expected.to eq 'Locomotive'\
' ' }
end
context 'with a non string type input' do
let(:liquid_source) { 'built by {{ section.settings.image }}' }
it { is_expected.to eq 'Locomotive'\
' ' }
end
context 'including the link_to liquid tag' do
let(:liquid_source) { 'go to {% link_to home %}' }
before { expect_any_instance_of(Locomotive::Steam::Liquid::Tags::LinkTo).to receive(:render) { 'HOME' } }
it { is_expected.to eq 'Locomotive'\
' ' }
end
context 'without the live editing feature enabled' do
let(:live_editing) { false }
it { is_expected.to eq 'Locomotive '\
'' }
end
context 'the developer wants to wrap herself/himself the section' do
let(:liquid_source) { '' }
it { is_expected.to eq 'Locomotive '\
'' }
end
end
context 'with blocks' do
let(:liquid_source) { '{% for foo in section.blocks %}{{ foo.settings.title }}{% endfor %}' }
it { is_expected.to eq 'Locomotive'\
' ' }
context 'with a non text type input' do
let(:liquid_source) { '{% for foo in section.blocks %}{{ foo.settings.image }}{% endfor %}' }
it { is_expected.to eq 'Locomotive'\
' ' }
end
end
context 'with page content' do
let(:liquid_source) { 'built by {{ section.settings.brand }}' }
context 'with on section' do
context 'with simple type' do
let(:content) {
{
header: {
settings: { brand: 'Locomotive' },
blocks: []
}
}.deep_stringify_keys
}
it { is_expected.to eq 'Locomotive '\
'' }
end
context 'with an id passed as an option' do
let(:source) { 'Locomotive {% section header, id: "my_header" %}'}
let(:content) {
{
'my_header': {
settings: { brand: 'Locomotive' },
blocks: []
}
}.deep_stringify_keys
}
it { is_expected.to eq 'Locomotive '\
'' }
end
context 'with an id within the content' do
let(:source) { 'Locomotive {% section header %}'}
let(:alt_content) {
{
id: 'site-header',
settings: { brand: 'Locomotive' },
blocks: []
}.deep_stringify_keys
}
it { is_expected.to eq 'Locomotive '\
'' }
end
end
end
context 'the section has a syntax error inside its liquid template' do
let(:liquid_source) { %(built by \n\t{{ section.settings.brand }}{% if %}) }
it 'raises a LiquidError' do
expect { subject }.to raise_exception(Locomotive::Steam::LiquidError)
end
end
context 'rendering error (action) found in the section' do
let(:live_editing) { false }
let(:liquid_source) { '{% action "Hello world" %}a.b(+}{% endaction %}' }
let(:section) { instance_double('section',
name: 'Hero',
liquid_source: liquid_source,
definition: { settings: [], blocks: [] }
)}
it 'raises a TemplateError' do
expect { subject }.to raise_exception(Locomotive::Steam::TemplateError)
end
end
end
end