require 'spec_helper' describe SmallVictories do let(:destination_css) { './fixtures/destination/_sv_custom.css' } let(:destination_js) { './fixtures/destination/_sv_custom.js' } let(:destination_html) { './fixtures/destination/index.html' } let(:compiler) { SmallVictories::Compiler.new(config: SmallVictories::Configuration.new) } before do allow_any_instance_of(SmallVictories::Configuration).to receive(:source).and_return('./spec/fixtures/source') allow_any_instance_of(SmallVictories::Configuration).to receive(:destination).and_return('./spec/fixtures/destination') end describe '#package' do context 'with valid css' do it 'package the css file' do compiler.package expect(File.open(destination_css).read).to include '.bootstrap{color:black;box-sizing:content-box}html{background:white}body div{background:red}p{font-size:30px}' end end context 'with valid js' do it 'package the js file' do compiler.package expect(File.open(destination_js).read).to include '(function(){alert("hi")}).call(this),console.log("hi");' end end end describe '#prefix' do before do FileUtils.cp('fixtures/_sv_custom.css', 'fixtures/destination') end it 'prefixes the css file' do compiler.prefix_css expect(File.open(destination_css).read).to include 'html{background:red;-webkit-box-sizing:content-box;box-sizing:content-box}' end end describe '#compile_css' do it 'compules and prefixes the css file' do compiler.compile_css expect(File.open(destination_css).read).to include '.bootstrap{color:black;-webkit-box-sizing:content-box;box-sizing:content-box}html{background:white}body div{background:red}p{font-size:30px}' end end describe '#compile_js' do it 'compules the js file' do compiler.compile_js expect(File.open(destination_js).read).to include '(function(){alert("hi")}).call(this),console.log("hi");' end end describe '#compile_html' do it 'compiles the liquid files' do compiler.compile_html expect(File.open(destination_html).read).to include "<html>\n<h1>Index</h1>\n<p>My snippet</p>\n\n\n</html>" end context 'with no layout' do before do allow_any_instance_of(SmallVictories::Configuration).to receive(:layout).and_return('no-file-here') end it 'compiles the liquid files' do compiler.compile_html expect(File.open(destination_html).read).to include "<h1>Index</h1>\n<p>My snippet</p>\n\n" end end end context 'with invalid files' do before do allow_any_instance_of(SmallVictories::Configuration).to receive(:source).and_return('./spec/fixtures/invalid') end it 'does not generate a css file' do compiler.package expect(File.exists?(destination_css)).to eq false end it 'does not package the js file' do compiler.package expect(File.exists?(destination_js)).to eq false end it 'shows the error in html' do compiler.compile_html expect(File.open(destination_html).read).to include "<html>\n<h1>Index</h1>\nLiquid error: No such template 'snippet'\n\n</html>" end end end