|
2 | 2 |
|
3 | 3 | require "test_helper" |
4 | 4 |
|
5 | | -require "open3" |
6 | | - |
7 | 5 | class EngineTest < Minitest::Test |
8 | 6 | def test_set_no_duplicate_autoload_paths |
9 | 7 | test_path = "#{Rails.root}/my/components/previews" # set in sandbox/config/application.rb |
10 | 8 | assert_equal 1, ActiveSupport::Dependencies.autoload_paths.count(test_path) |
11 | 9 | end |
12 | 10 |
|
13 | 11 | # Non-main Ractors can't read ActionView::Resolver's class variable on Ruby 4.0, |
14 | | - # so the executor must not read it on every request when templates are cached. |
15 | | - def test_executor_does_not_read_cache_template_loading_when_templates_are_cached |
16 | | - reads, errors, status = Open3.capture3({"RAILS_ENV" => "production"}, RbConfig.ruby, "-rbundler/setup", "-e", <<~RUBY) |
17 | | - require "rails" |
18 | | - require "action_controller/railtie" |
19 | | - require "view_component" |
20 | | - |
21 | | - class CachedTemplatesApp < Rails::Application |
22 | | - config.eager_load = true |
23 | | - config.enable_reloading = false |
24 | | - config.secret_key_base = "test" |
25 | | - config.logger = Logger.new(nil) |
26 | | - end |
27 | | - |
28 | | - CachedTemplatesApp.initialize! |
29 | | - |
30 | | - reads = 0 |
31 | | - ActionView::Resolver.singleton_class.prepend(Module.new { define_method(:caching?) { reads += 1; super() } }) |
32 | | - CachedTemplatesApp.executor.wrap {} |
33 | | - print reads |
34 | | - RUBY |
35 | | - |
36 | | - assert status.success?, errors |
37 | | - assert_equal "0", reads |
| 12 | + # so no per-request hook may read it when templates are cached. |
| 13 | + def test_watch_templates_registers_no_executor_hook_when_templates_are_cached |
| 14 | + app = app_with_new_executor |
| 15 | + |
| 16 | + with_template_caching { ViewComponent::CompileCache.watch_templates(app) } |
| 17 | + |
| 18 | + assert_empty app.executor._run_callbacks |
| 19 | + end |
| 20 | + |
| 21 | + def test_watch_templates_registers_an_executor_hook_when_templates_are_not_cached |
| 22 | + app = app_with_new_executor |
| 23 | + |
| 24 | + with_new_cache { ViewComponent::CompileCache.watch_templates(app) } |
| 25 | + |
| 26 | + assert_equal 1, app.executor._run_callbacks.count |
| 27 | + end |
| 28 | + |
| 29 | + private |
| 30 | + |
| 31 | + def app_with_new_executor |
| 32 | + Struct.new(:executor, :config).new(Class.new(ActiveSupport::Executor), Rails.application.config) |
38 | 33 | end |
39 | 34 | end |
0 commit comments