require 'spec_helper'
describe Locomotive::Liquid::Tags::SEO do
let(:site) do
FactoryGirl.build(:site, :seo_title => 'Site title (SEO)', :meta_description => 'A short site description', :meta_keywords => 'test only cat dog')
end
describe 'rendering' do
it 'renders everything' do
html = render_seo
html.should include '
Site title (SEO)'
html.should include ''
html.should include ''
end
it 'renders a seo title' do
render_seo_title.should include 'Site title (SEO)'
end
it 'renders the site title if no seo title is provided' do
site.seo_title = nil
render_seo_title.should include 'Acme Website'
end
it 'renders a meta description tag' do
render_seo_metadata.should include ''
end
it 'strips and removes quote characters from the description' do
site.meta_description = ' String with " " quotes '
render_seo_metadata.should include ''
end
it 'renders a meta keywords tag' do
render_seo_metadata.should include ''
end
it 'strips and removes quote characters from the keywords' do
site.meta_keywords = ' one " two " three '
render_seo_metadata.should include ''
end
it 'renders an empty string if no meta' do
site.meta_keywords = nil
render_seo_metadata.should include ''
end
context "when page" do
context "has seo title" do
let(:page) { site.pages.build(:seo_title => 'Page title (SEO)', :meta_keywords => 'hulk,gamma', :meta_description => "Bruce Banner") }
subject { render_seo_title('page' => page) }
it { should include(%Q[Page title (SEO)]) }
end
context "does not have seo title" do
let(:page) { site.pages.build }
subject { render_seo_title('page' => page) }
it { should include(%Q[Site title (SEO)]) }
end
context "has metadata" do
let(:page) { site.pages.build(:meta_keywords => 'hulk,gamma', :meta_description => "Bruce Banner") }
subject { render_seo_metadata('page' => page) }
it { should include(%Q[]) }
it { should include(%Q[]) }
end
context "does not have metadata" do
let(:page) { site.pages.build }
subject { render_seo_metadata('page' => page) }
it { should include(%Q[]) }
it { should include(%Q[]) }
end
end
context "when content instance" do
let(:content_type) do
FactoryGirl.build(:content_type, :site => site).tap do |ct|
ct.entries_custom_fields.build :label => 'anything', :type => 'String'
end
end
context "has seo title" do
let(:content_entry) { content_type.entries.build(:seo_title => 'Content title (SEO)', :meta_keywords => 'Libidinous, Angsty', :meta_description => "Quite the combination.") }
subject { render_seo_title('content_entry' => content_entry) }
it { should include(%Q[Content title (SEO)]) }
end
context "does not have seo title" do
let(:content_entry) { content_type.entries.build }
subject { render_seo_title('content_entry' => content_entry) }
it { should include(%Q[Site title (SEO)]) }
end
context "has metadata" do
let(:content_entry) { content_type.entries.build(:meta_keywords => 'Libidinous, Angsty', :meta_description => "Quite the combination.") }
subject { render_seo_metadata('content_entry' => content_entry) }
it { should include(%Q[]) }
it { should include(%Q[]) }
end
context "does not have metadata" do
let(:content_entry) { content_type.entries.build }
subject { render_seo_metadata('content_entry' => content_entry) }
it { should include(%Q[]) }
it { should include(%Q[]) }
end
end
end
def render_seo(assigns = {})
render_seo_tag('seo', assigns)
end
def render_seo_title(assigns = {})
render_seo_tag('seo_title', assigns)
end
def render_seo_metadata(assigns = {})
render_seo_tag('seo_metadata', assigns)
end
def render_seo_tag(tag_name, assigns = {})
registers = { :site => site }
liquid_context = ::Liquid::Context.new({}, assigns, registers)
output = Liquid::Template.parse("{% #{tag_name} %}").render(liquid_context)
end
end