|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "bundler/setup" |
| 4 | +require "minitest/autorun" |
| 5 | +require "minitest/mock" |
| 6 | +require "tmpdir" |
| 7 | +require "rails" |
| 8 | +require "action_controller/railtie" |
| 9 | +require "active_support/testing/isolation" |
| 10 | +require "view_component" |
| 11 | + |
| 12 | +# Boots a fresh application per test in a forked process, so tests can choose |
| 13 | +# their own environment. Runs separately from the sandbox, which boots in test. |
| 14 | +class TemplateCachingTest < ActiveSupport::TestCase |
| 15 | + include ActiveSupport::Testing::Isolation |
| 16 | + |
| 17 | + def test_executor_checks_for_template_changes_when_templates_are_not_cached |
| 18 | + app = boot_app("development") |
| 19 | + refute ActionView::Base.cache_template_loading |
| 20 | + |
| 21 | + watcher = Minitest::Mock.new |
| 22 | + watcher.expect(:execute_if_updated, nil) |
| 23 | + ViewComponent::CompileCache.stub(:template_watcher, watcher) { app.executor.wrap {} } |
| 24 | + |
| 25 | + watcher.verify |
| 26 | + end |
| 27 | + |
| 28 | + # Non-main Ractors can't read ActionView::Resolver's class variable on Ruby 4.0, |
| 29 | + # so the executor must not read it on every request when templates are cached. |
| 30 | + def test_executor_does_nothing_when_templates_are_cached |
| 31 | + app = boot_app("production") |
| 32 | + assert ActionView::Base.cache_template_loading |
| 33 | + |
| 34 | + reads = 0 |
| 35 | + ActionView::Resolver.singleton_class.prepend(Module.new do |
| 36 | + define_method(:caching?) do |
| 37 | + reads += 1 |
| 38 | + super() |
| 39 | + end |
| 40 | + end) |
| 41 | + ViewComponent::CompileCache.stub(:template_watcher, ->(*) { flunk "templates were watched" }) { app.executor.wrap {} } |
| 42 | + |
| 43 | + assert_equal 0, reads |
| 44 | + end |
| 45 | + |
| 46 | + def test_template_changes_are_not_reflected_on_new_request_when_templates_are_cached |
| 47 | + Dir.mktmpdir do |root| |
| 48 | + write_file(root, "app/components/greeting_component.rb", "class GreetingComponent < ViewComponent::Base; end") |
| 49 | + write_file(root, "app/components/greeting_component.html.erb", "<div>Hello</div>") |
| 50 | + |
| 51 | + app = boot_app("production", root: root) |
| 52 | + app.routes.draw do |
| 53 | + get "/", to: ->(_env) { [200, {"content-type" => "text/html"}, [ActionController::Base.render(GreetingComponent.new)]] } |
| 54 | + end |
| 55 | + request = Rack::MockRequest.new(app) |
| 56 | + |
| 57 | + assert_equal "<div>Hello</div>", request.get("/").body.strip |
| 58 | + |
| 59 | + write_file(root, "app/components/greeting_component.html.erb", "<div>Goodbye</div>") |
| 60 | + |
| 61 | + assert_equal "<div>Hello</div>", request.get("/").body.strip |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + |
| 67 | + def boot_app(env, root: __dir__) |
| 68 | + Rails.env = env |
| 69 | + |
| 70 | + Class.new(Rails::Application) do |
| 71 | + config.root = root |
| 72 | + config.eager_load = env == "production" |
| 73 | + config.enable_reloading = env == "development" |
| 74 | + config.secret_key_base = "test" |
| 75 | + config.logger = Logger.new(nil) |
| 76 | + end.tap(&:initialize!) |
| 77 | + end |
| 78 | + |
| 79 | + def write_file(root, path, content) |
| 80 | + FileUtils.mkdir_p(File.dirname(File.join(root, path))) |
| 81 | + File.write(File.join(root, path), content) |
| 82 | + end |
| 83 | +end |
0 commit comments