2010-05-02 23:33:17 +00:00
require 'spec_helper'
2010-07-23 20:09:54 +00:00
2011-11-20 11:47:41 +00:00
describe Locomotive :: Snippet do
2010-07-23 20:09:54 +00:00
2010-05-02 23:33:17 +00:00
it 'should have a valid factory' do
2011-08-25 21:28:56 +00:00
FactoryGirl . build ( :snippet ) . should be_valid
2010-05-02 23:33:17 +00:00
end
2011-08-25 21:28:56 +00:00
2010-08-21 22:48:24 +00:00
# Validations ##
%w{ site name template } . each do | field |
it " should validate presence of #{ field } " do
2011-08-25 21:28:56 +00:00
template = FactoryGirl . build ( :snippet , field . to_sym = > nil )
2010-08-21 22:48:24 +00:00
template . should_not be_valid
template . errors [ field . to_sym ] . should == [ " can't be blank " ]
end
end
2010-07-23 20:09:54 +00:00
2011-02-16 09:37:26 +00:00
describe '#update_templates' do
2011-08-25 21:28:56 +00:00
2011-02-16 09:37:26 +00:00
before :each do
2011-08-25 21:28:56 +00:00
@site = FactoryGirl . create ( :site , :subdomain = > 'omg' )
2012-04-07 17:25:36 +00:00
@snippet = FactoryGirl . create ( :snippet , :site = > @site , :slug = > 'my-test-snippet' , :template = > 'a testing template' )
2011-02-16 09:37:26 +00:00
end
context 'with a normal top level snippet' do
before :each do
2012-04-07 17:25:36 +00:00
@page = FactoryGirl . create ( :page , :site = > @site , :slug = > 'my_page_here' , :raw_template = > " {% include 'my-test-snippet' %} " )
2011-02-16 09:37:26 +00:00
end
it 'updates templates with the new snippet template' do
@snippet . update_attributes ( :template = > 'a new template' )
2011-11-26 06:24:34 +00:00
Locomotive :: Page . find ( @page . id ) . render ( { } ) . should == 'a new template'
2011-02-16 09:37:26 +00:00
end
end
context 'for snippets inside of a block' do
before :each do
2012-04-07 17:25:36 +00:00
@page = FactoryGirl . create ( :page , :site = > @site , :slug = > 'my_page_here' , :raw_template = > " {% block main %}{% include 'my-test-snippet' %}{% endblock %} " )
2011-02-16 09:37:26 +00:00
end
it 'updates templates with the new snippet template' do
@snippet . update_attributes ( :template = > 'a new template' )
2011-11-26 06:24:34 +00:00
Locomotive :: Page . find ( @page . id ) . render ( { } ) . should == 'a new template'
2011-02-16 09:37:26 +00:00
end
end
2012-03-02 16:37:54 +00:00
context '#i18n' do
before :each do
Mongoid :: Fields :: I18n . with_locale ( :fr ) do
@snippet = FactoryGirl . create ( :snippet , :site = > @site , :slug = > 'my_localized_test_snippet' , :template = > 'a testing template' )
2012-04-07 17:25:36 +00:00
@page = FactoryGirl . create ( :page , :site = > @site , :slug = > 'my_localized_test_snippet' , :raw_template = > " {% block main %}{% include 'my-localized-test-snippet' %}{% endblock %} " )
2012-03-02 16:37:54 +00:00
end
end
it 'returns the snippet dependencies depending on the UI locale' do
Mongoid :: Fields :: I18n . with_locale ( :fr ) { @page . snippet_dependencies . should_not be_empty }
Mongoid :: Fields :: I18n . with_locale ( :en ) { @page . snippet_dependencies . should be_nil }
end
it 'updates the templates with the new snippet' do
Mongoid :: Fields :: I18n . with_locale ( :fr ) do
@snippet . update_attributes ( :template = > 'a new template' )
Locomotive :: Page . find ( @page . id ) . render ( { } ) . should == 'a new template'
end
end
end
2011-02-16 09:37:26 +00:00
end
2010-07-23 20:09:54 +00:00
end