Skip to content

Commit 3d8e7b2

Browse files
joelhawksleyCopilot
andcommitted
Move cached-template integration test to isolated tests
Booting a production app tests template caching as decided at boot, so the sandbox no longer needs to fake it by skipping the watcher hook. Remove the now-unused with_template_caching helper. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f2e356ff-16cd-4517-8e63-a8600747d3fa
1 parent b9efa46 commit 3d8e7b2

3 files changed

Lines changed: 27 additions & 38 deletions

File tree

‎test/isolated/template_caching_test.rb‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "bundler/setup"
44
require "minitest/autorun"
55
require "minitest/mock"
6+
require "tmpdir"
67
require "rails"
78
require "action_controller/railtie"
89
require "active_support/testing/isolation"
@@ -42,17 +43,41 @@ def test_executor_does_nothing_when_templates_are_cached
4243
assert_equal 0, reads
4344
end
4445

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+
4565
private
4666

47-
def boot_app(env)
67+
def boot_app(env, root: __dir__)
4868
Rails.env = env
4969

5070
Class.new(Rails::Application) do
51-
config.root = __dir__
71+
config.root = root
5272
config.eager_load = env == "production"
5373
config.enable_reloading = env == "development"
5474
config.secret_key_base = "test"
5575
config.logger = Logger.new(nil)
5676
end.tap(&:initialize!)
5777
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
5883
end

‎test/sandbox/test/integration_test.rb‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,6 @@ def test_rendering_component_in_a_controller_with_block
5050
assert_response :success
5151
end
5252

53-
def test_template_changes_are_not_reflected_on_new_request_when_cache_template_loading_is_true
54-
with_template_caching do
55-
get "/controller_inline"
56-
assert_select("div", "bar")
57-
assert_response :success
58-
59-
modify_file "app/components/controller_inline_component.html.erb", "<div>Goodbye world!</div>" do
60-
get "/controller_inline"
61-
assert_select("div", "bar")
62-
assert_response :success
63-
end
64-
65-
get "/controller_inline"
66-
assert_select("div", "bar")
67-
assert_response :success
68-
end
69-
end
70-
7153
def test_template_changes_are_reflected_on_new_request_when_cache_template_loading_is_false
7254
with_new_cache do
7355
get "/controller_inline"

‎test/test_helper.rb‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,6 @@ def with_generate_sidecar(enabled, &block)
128128
with_generate_option(:sidecar, enabled, &block)
129129
end
130130

131-
# Template caching is decided at boot, so also skip the template watcher that
132-
# the sandbox registered because it boots without template caching.
133-
def with_template_caching
134-
executor = Rails.application.executor
135-
watcher_hook = executor._run_callbacks.find do |callback|
136-
callback.filter.is_a?(Proc) && callback.filter.source_location&.first&.end_with?("view_component/engine.rb")
137-
end&.filter
138-
executor.skip_callback(:run, :before, watcher_hook) if watcher_hook
139-
140-
old_cache_template_loading = ActionView::Base.cache_template_loading
141-
ActionView::Base.cache_template_loading = true
142-
143-
yield
144-
ensure
145-
ActionView::Base.cache_template_loading = old_cache_template_loading
146-
executor.set_callback(:run, :before, watcher_hook) if watcher_hook
147-
end
148-
149131
def with_new_cache
150132
old_cache = ViewComponent::CompileCache.cache
151133
ViewComponent::CompileCache.cache = Set.new

0 commit comments

Comments
 (0)