# frozen_string_literal: true
RSpec.describe Hanami::Helpers::AssetsHelper, "#javascript", :app_integration do
subject(:obj) {
helpers = described_class
Class.new {
include helpers
attr_reader :_context
def initialize(context)
@_context = context
end
}.new(context)
}
def javascript_tag(...)
subject.javascript_tag(...)
end
let(:context) { TestApp::Views::Context.new }
let(:root) { make_tmp_directory }
before do
with_directory(root) do
write "config/app.rb", <<~RUBY
module TestApp
class App < Hanami::App
config.logger.stream = StringIO.new
end
end
RUBY
write "app/views/context.rb", <<~RUBY
# auto_register: false
require "hanami/view/context"
module TestApp
module Views
class Context < Hanami::View::Context
end
end
end
RUBY
write "app/assets/js/app.ts", <<~JS
import "../css/app.css";
console.log("Hello from index.ts");
JS
write "app/assets/css/app.css", <<~CSS
.btn {
background: #f00;
}
CSS
stub_assets("feature-a.js")
require "hanami/setup"
before_prepare if respond_to?(:before_prepare)
require "hanami/prepare"
end
end
it "returns an instance of SafeString" do
actual = javascript_tag("feature-a")
expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString)
end
it "is aliased as #javascript_tag" do
expect(subject.javascript_tag("feature-a")).to eq javascript_tag("feature-a")
end
it "renders ))
end
xit "renders ))
end
it "renders ))
end
it "renders ))
end
it "renders ))
end
it "ignores src passed as an option" do
actual = javascript_tag("feature-a", src: "wrong")
expect(actual).to eq(%())
end
describe "async option" do
it "renders ))
end
it "renders ))
end
end
describe "subresource_integrity mode" do
def before_prepare
Hanami.app.config.assets.subresource_integrity = [:sha384]
end
before { compile_assets! }
it "includes subresource_integrity and crossorigin attributes" do
actual = javascript_tag("app")
expect(actual).to match(%r{})
end
end
describe "cdn mode" do
let(:base_url) { "https://hanami.test" }
def before_prepare
Hanami.app.config.assets.base_url = "https://hanami.test"
end
it "returns absolute url for src attribute" do
actual = javascript_tag("feature-a")
expect(actual).to eq(%())
end
end
end