# frozen_string_literal: true RSpec.describe Hanami::Helpers::AssetsHelper, "#image", :app_integration do subject(:obj) { helpers = described_class Class.new { include helpers attr_reader :_context def initialize(context) @_context = context end }.new(context) } def image(...) subject.image(...) end let(:root) { make_tmp_directory } let(:context) { TestApp::Views::Context.new } 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 stub_assets("application.jpg") require "hanami/setup" before_prepare if respond_to?(:before_prepare) require "hanami/prepare" end end it "returns an instance of HtmlBuilder" do actual = image("application.jpg") expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString) end it "renders an tag" do actual = image("application.jpg").to_s expect(actual).to eq(%(Application)) end it "is aliased as #image_tag" do expect(subject.image_tag("application.jpg")).to eq image("application.jpg") end it "custom alt" do actual = image("application.jpg", alt: "My Alt").to_s expect(actual).to eq(%(My Alt)) end it "custom data attribute" do actual = image("application.jpg", "data-user-id" => 5).to_s expect(actual).to eq(%(Application)) end it "ignores src passed as an option" do actual = image("application.jpg", src: "wrong").to_s expect(actual).to eq(%(Application)) 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 = image("application.jpg").to_s expect(actual).to eq(%(Application)) end end end