2011-11-16 20:28:02 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Jasmine::Headless::TestFile do
|
2011-11-18 03:16:04 +00:00
|
|
|
let(:source_root) { File.expand_path('source_root') }
|
|
|
|
let(:path) { File.join(source_root, 'path.js') }
|
|
|
|
|
|
|
|
let(:file) { described_class.new(path, source_root) }
|
2011-11-16 20:28:02 +00:00
|
|
|
|
|
|
|
subject { file }
|
|
|
|
|
|
|
|
its(:path) { should == path }
|
|
|
|
|
|
|
|
describe '#to_html' do
|
|
|
|
subject { file.to_html }
|
|
|
|
|
|
|
|
context '.js' do
|
|
|
|
let(:path) { 'path.js' }
|
|
|
|
|
|
|
|
it { should == %{<script type="text/javascript" src="#{path}"></script>} }
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.css' do
|
|
|
|
let(:path) { 'path.css' }
|
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
it { should == %{<link rel="stylesheet" href="#{path}" type="text/css" />} }
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context '.coffee' do
|
|
|
|
let(:path) { 'path.coffee' }
|
|
|
|
|
|
|
|
let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) }
|
|
|
|
|
|
|
|
context 'compilation error' do
|
|
|
|
let(:error) { CoffeeScript::CompilationError.new("fail") }
|
|
|
|
|
|
|
|
before do
|
2011-11-17 21:18:25 +00:00
|
|
|
handle_expectation.raises(error)
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should pass along the error' do
|
2011-11-18 03:16:04 +00:00
|
|
|
expect { subject }.to raise_error(CoffeeScript::CompilationError)
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'compiles fine' do
|
2011-11-17 21:18:25 +00:00
|
|
|
let(:source) { 'source' }
|
2011-11-16 20:28:02 +00:00
|
|
|
|
|
|
|
before do
|
2011-11-17 21:18:25 +00:00
|
|
|
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return)
|
|
|
|
handle_expectation.returns(source)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cached' do
|
|
|
|
let(:file_path) { 'dir/file.js' }
|
|
|
|
let(:cache_return) { true }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the cached file' do
|
|
|
|
subject.should include(%{<script type="text/javascript" src="#{file_path}"></script>})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'not cached' do
|
|
|
|
let(:cache_return) { false }
|
2011-11-16 20:28:02 +00:00
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
it 'should return the generated js' do
|
|
|
|
subject.should include(%{<script type="text/javascript">#{source}</script>})
|
|
|
|
end
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-18 21:00:24 +00:00
|
|
|
|
|
|
|
context '.jst' do
|
|
|
|
include FakeFS::SpecHelpers
|
|
|
|
|
|
|
|
let(:path) { 'file.jst' }
|
|
|
|
let(:content) { 'content' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
File.open(path, 'wb') { |fh| fh.print content }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the JST template processor to get the processed file' do
|
|
|
|
subject.should include('JST["file"]')
|
|
|
|
subject.should include(content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.jst.*' do
|
|
|
|
include FakeFS::SpecHelpers
|
|
|
|
|
|
|
|
let(:path) { 'file.jst.ejs' }
|
|
|
|
let(:content) { 'content' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
File.open(path, 'wb') { |fh| fh.print content }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the JST template processor to get the processed file' do
|
|
|
|
subject.should include('JST["file"]')
|
|
|
|
subject.should include(content)
|
|
|
|
end
|
|
|
|
end
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
|
|
|
|
describe '#dependencies' do
|
|
|
|
include FakeFS::SpecHelpers
|
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.mkdir_p File.dirname(path)
|
|
|
|
File.open(path, 'wb') { |fh| fh.print "//= require '#{req}'\njavascript" }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'absolute' do
|
|
|
|
let(:req) { 'test' }
|
|
|
|
|
|
|
|
subject { file.dependencies }
|
|
|
|
|
|
|
|
it { should == [ [ 'require', req ] ] }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'relative' do
|
|
|
|
let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') }
|
|
|
|
|
|
|
|
let(:req) { './test' }
|
|
|
|
|
|
|
|
subject { file.dependencies }
|
|
|
|
|
|
|
|
it { should == [ [ 'require', 'subdir/subsubdir/test' ] ] }
|
|
|
|
end
|
2011-11-18 16:50:48 +00:00
|
|
|
|
|
|
|
context 'dot' do
|
|
|
|
let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') }
|
|
|
|
|
|
|
|
let(:req) { '.' }
|
|
|
|
|
|
|
|
subject { file.dependencies }
|
|
|
|
|
|
|
|
it { should == [ [ 'require', 'subdir/subsubdir' ] ] }
|
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|