# frozen_string_literal: true require "ostruct" # rubocop:disable Style/OpenStructUse RSpec.describe "App view / Helpers / User-defined helpers / Scope helpers", :app_integration do before do with_directory(make_tmp_directory) do write "config/app.rb", <<~RUBY module TestApp class App < Hanami::App end end RUBY write "app/view.rb", <<~RUBY # auto_register: false require "hanami/view" module TestApp class View < Hanami::View config.layout = nil end end RUBY write "app/views/helpers.rb", <<~'RUBY' # auto_register: false module TestApp module Views module Helpers def exclaim_from_app(str) tag.h1("#{str}! (app #{_context.inflector.pluralize('helper')})") end end end end RUBY before_prepare if respond_to?(:before_prepare) require "hanami/prepare" end end describe "app view and parts" do def before_prepare write "app/views/posts/show.rb", <<~RUBY module TestApp module Views module Posts class Show < TestApp::View expose :post end end end end RUBY write "app/views/parts/post.rb", <<~RUBY module TestApp module Views module Parts class Post < TestApp::Views::Part def title helpers.exclaim_from_app(value.title) end end end end end RUBY write "app/templates/posts/show.html.erb", <<~ERB <%= post.title %> ERB end it "makes user-defined helpers available in parts via a `helpers` object" do post = OpenStruct.new(title: "Hello world") output = TestApp::App["views.posts.show"].call(post: post).to_s.strip expect(output).to eq "