2013-02-05 14:16:51 -05:00
require 'spec_helper'
describe SiteCustomization do
2014-12-23 12:46:10 +11:00
before do
SiteCustomization . clear_cache!
end
2013-02-05 14:16:51 -05:00
let :user do
Fabricate ( :user )
end
2013-09-16 12:21:49 -04:00
let :customization_params do
2013-09-16 12:55:44 -04:00
{ name : 'my name' , user_id : user . id , header : " my awesome header " , stylesheet : " my awesome css " , mobile_stylesheet : nil , mobile_header : nil }
2013-09-16 12:21:49 -04:00
end
2013-02-25 19:42:20 +03:00
let :customization do
2013-09-16 12:21:49 -04:00
SiteCustomization . create! ( customization_params )
end
let :customization_with_mobile do
SiteCustomization . create! ( customization_params . merge ( mobile_stylesheet : " .mobile {better: true;} " , mobile_header : " fancy mobile stuff " ) )
2013-02-05 14:16:51 -05:00
end
2013-02-25 19:42:20 +03:00
it 'should set default key when creating a new customization' do
2013-02-05 14:16:51 -05:00
s = SiteCustomization . create! ( name : 'my name' , user_id : user . id )
2015-01-05 13:04:23 -03:00
expect ( s . key ) . not_to eq ( nil )
2013-02-05 14:16:51 -05:00
end
2014-12-23 12:46:10 +11:00
it 'can enable more than one style at once' do
c1 = SiteCustomization . create! ( name : '2' , user_id : user . id , header : 'World' ,
enabled : true , mobile_header : 'hi' , footer : 'footer' ,
stylesheet : '.hello{.world {color: blue;}}' )
SiteCustomization . create! ( name : '1' , user_id : user . id , header : 'Hello' ,
enabled : true , mobile_footer : 'mfooter' ,
mobile_stylesheet : '.hello{margin: 1px;}' ,
stylesheet : 'p{width: 1px;}'
)
2015-01-05 13:04:23 -03:00
expect ( SiteCustomization . custom_header ) . to eq ( " Hello \n World " )
expect ( SiteCustomization . custom_header ( nil , :mobile ) ) . to eq ( " hi " )
expect ( SiteCustomization . custom_footer ( nil , :mobile ) ) . to eq ( " mfooter " )
expect ( SiteCustomization . custom_footer ) . to eq ( " footer " )
2014-12-23 12:46:10 +11:00
desktop_css = SiteCustomization . custom_stylesheet
2015-01-05 13:04:23 -03:00
expect ( desktop_css ) . to match ( Regexp . new ( " #{ SiteCustomization :: ENABLED_KEY } .css \\ ?target=desktop " ) )
2014-12-23 12:46:10 +11:00
mobile_css = SiteCustomization . custom_stylesheet ( nil , :mobile )
2015-01-05 13:04:23 -03:00
expect ( mobile_css ) . to match ( Regexp . new ( " #{ SiteCustomization :: ENABLED_KEY } .css \\ ?target=mobile " ) )
2014-12-23 12:46:10 +11:00
2015-01-05 13:04:23 -03:00
expect ( SiteCustomization . enabled_stylesheet_contents ) . to match ( / \ .hello \ .world / )
2014-12-23 12:46:10 +11:00
# cache expiry
c1 . enabled = false
c1 . save
2015-01-05 13:04:23 -03:00
expect ( SiteCustomization . custom_stylesheet ) . not_to eq ( desktop_css )
expect ( SiteCustomization . enabled_stylesheet_contents ) . not_to match ( / \ .hello \ .world / )
2014-12-23 12:46:10 +11:00
end
it 'should be able to look up stylesheets by key' do
c = SiteCustomization . create! ( name : '2' , user_id : user . id ,
enabled : true ,
stylesheet : '.hello{.world {color: blue;}}' ,
mobile_stylesheet : '.world{.hello{color: black;}}' )
2015-01-05 13:04:23 -03:00
expect ( SiteCustomization . custom_stylesheet ( c . key , :mobile ) ) . to match ( Regexp . new ( " #{ c . key } .css \\ ?target=mobile " ) )
expect ( SiteCustomization . custom_stylesheet ( c . key ) ) . to match ( Regexp . new ( " #{ c . key } .css \\ ?target=desktop " ) )
2014-12-23 12:46:10 +11:00
end
2013-02-05 14:16:51 -05:00
2014-12-23 12:46:10 +11:00
it 'should allow including discourse styles' do
c = SiteCustomization . create! ( user_id : user . id , name : " test " , stylesheet : '@import "desktop";' , mobile_stylesheet : '@import "mobile";' )
2015-01-05 13:04:23 -03:00
expect ( c . stylesheet_baked ) . not_to match ( / Syntax error / )
expect ( c . stylesheet_baked . length ) . to be > 1000
expect ( c . mobile_stylesheet_baked ) . not_to match ( / Syntax error / )
expect ( c . mobile_stylesheet_baked . length ) . to be > 1000
2013-02-05 14:16:51 -05:00
end
2014-12-23 12:46:10 +11:00
it 'should provide an awesome error on failure' do
c = SiteCustomization . create! ( user_id : user . id , name : " test " , stylesheet : " $black: # 000; # a { color: $black; } \n \n \n boom " , header : '' )
2015-01-05 13:04:23 -03:00
expect ( c . stylesheet_baked ) . to match ( / Syntax error / )
expect ( c . mobile_stylesheet_baked ) . not_to be_present
2014-12-23 12:46:10 +11:00
end
it 'should provide an awesome error on failure for mobile too' do
c = SiteCustomization . create! ( user_id : user . id , name : " test " , stylesheet : '' , header : '' , mobile_stylesheet : " $black: # 000; # a { color: $black; } \n \n \n boom " , mobile_header : '' )
2015-01-05 13:04:23 -03:00
expect ( c . mobile_stylesheet_baked ) . to match ( / Syntax error / )
expect ( c . stylesheet_baked ) . not_to be_present
2014-12-23 12:46:10 +11:00
end
2013-02-05 14:16:51 -05:00
end