spec/lib/sprockets/processor_spec.rb in opal-0.7.2 vs spec/lib/sprockets/processor_spec.rb in opal-0.8.0.beta1

- old
+ new

@@ -1,35 +1,67 @@ require 'lib/spec_helper' require 'opal/sprockets/processor' describe Opal::Processor do let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") } - let(:_context) { double('_context', :logical_path => "foo.#{ext}", :pathname => pathname) } - let(:env) { double('env') } + let(:environment) { double(Sprockets::Environment, + cache: nil, + :[] => nil, + resolve: pathname.expand_path.to_s, + engines: double(keys: %w[.rb .js .opal]), + ) } + let(:sprockets_context) { double(Sprockets::Context, + logical_path: "foo.#{ext}", + environment: environment, + pathname: pathname, + filename: pathname.to_s, + root_path: '/Code/app/mylib', + is_a?: true, + ) } - before do - env.stub(:resolve) { pathname.expand_path.to_s } - env.stub(:[]) - _context.stub(:environment) { env } - end - - %w[rb js.rb opal js.opal].each do |ext| + %w[.rb .opal].each do |ext| let(:ext) { ext } - describe %Q{with extension ".#{ext}"} do - it "is registered for '.#{ext}' files" do - expect(Tilt["test.#{ext}"]).to eq(described_class) + describe %Q{with extension "#{ext}"} do + it "is registered for '#{ext}' files" do + expect(Sprockets.engines[ext]).to eq(described_class) end it "compiles and evaluates the template on #render" do template = described_class.new { |t| "puts 'Hello, World!'\n" } - expect(template.render(_context)).to include('"Hello, World!"') + expect(template.render(sprockets_context)).to include('"Hello, World!"') end - - it "can be rendered more than once" do - template = described_class.new(_context) { |t| "puts 'Hello, World!'\n" } - 3.times { expect(template.render(_context)).to include('"Hello, World!"') } - end end end + describe '.stubbed_files' do + around do |e| + described_class.stubbed_files.clear + e.run + described_class.stubbed_files.clear + end + + let(:stubbed_file) { 'foo' } + let(:template) { described_class.new { |t| "require #{stubbed_file.inspect}" } } + + it 'usually require files' do + sprockets_context.should_receive(:require_asset).with(stubbed_file) + template.render(sprockets_context) + end + + it 'skips require of stubbed file' do + described_class.stub_file stubbed_file + sprockets_context.should_not_receive(:require_asset).with(stubbed_file) + template.render(sprockets_context) + end + + it 'marks a stubbed file as loaded' do + described_class.stub_file stubbed_file + asset = double(dependencies: [], pathname: Pathname('bar'), logical_path: 'bar') + environment.stub(:[]).with('bar.js') { asset } + environment.stub(:engines) { {'.rb' => described_class, '.opal' => described_class} } + + code = described_class.load_asset_code(environment, 'bar') + code.should match stubbed_file + end + end end