Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Oct 7, 2023
1 parent 1809799 commit 753f64b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/turbo/broadcastable/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ def capture_turbo_stream_broadcasts(stream_name_or_object, &block)
document.at("body").element_children
end
end

def assert_broadcastable_render(&render)
test_case = self
job_class = Class.new(ActiveJob::Base)

forbidden_controller_methods = %i[session turbo_native_app?]
forbidden_view_methods = %i[cookies request]

forbidden_controller_methods.each do |method_name|
controller.singleton_class.define_method method_name do
test_case.flunk "Cannot access ##{method_name} during broadcasts"
end
end

forbidden_view_methods.each do |method_name|
view.singleton_class.define_method method_name do
test_case.flunk "Cannot access ##{method_name} during broadcasts"
end
end

test_case.singleton_class.define_method :render do |*args, **options, &block|
raise ArgumentError, <<~ERROR if args.any?
Do not call render with positional arguments. Instead, call render with `partial:`
ERROR

super(*args, **options, &block)
end

job_class.define_method :perform do
test_case.with_options(formats: [:turbo_stream], &render)
end

job_class.perform_now
end
end
end
end
74 changes: 74 additions & 0 deletions test/broadcastable/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,77 @@ class Turbo::Broadcastable::TestHelper::AssertNoTurboStreamBroadcastsTest < Acti
end
end
end

class Turbo::Broadcastable::TestHelper::ViewTest < ActionView::TestCase
include Turbo::Broadcastable::TestHelper

test "#assert_broadcastable_render raises when render called with positional arguments" do
error = assert_raises ArgumentError do
assert_broadcastable_render do |broadcastable|
broadcastable.render "turbo_broadcastable"
end
end

assert_includes error.message, <<~ERROR
Do not call render with positional arguments. Instead, call render with `partial:`
ERROR
end

test "#assert_broadcastable_render does not leak positional argument change across tests" do
render "turbo_broadcastable"

assert_includes rendered, "Broadcastable"
end

test "#assert_broadcastable_render passes for a view partial" do
assert_broadcastable_render do |broadcastable|
broadcastable.render partial: "turbo_broadcastable", locals: { text: "text" }
end

assert_includes rendered, "Broadcastable text"
end

test "#assert_broadcastable_render flunks for a view partial with #current_user" do
assert_raises_forbidden_method_error :session do
assert_broadcastable_render do |broadcastable|
broadcastable.render partial: "not_turbo_broadcastable"
end
end
end

test "#assert_broadcastable_render flunks for a view partial with #session" do
assert_raises_forbidden_method_error :request do
assert_broadcastable_render do |broadcastable|
broadcastable.render inline: <<~ERB
<%= request.path %>
ERB
end
end
end

test "#assert_broadcastable_render flunks for a view partial with #turbo_native_app?" do
assert_raises_forbidden_method_error :turbo_native_app? do
assert_broadcastable_render do |broadcastable|
broadcastable.render inline: <<~ERB
<%= turbo_native_app? %>
ERB
end
end
end

test "#assert_broadcastable_render flunks for a view partial with #cookies" do
assert_raises_forbidden_method_error :cookies do
assert_broadcastable_render do |broadcastable|
broadcastable.render inline: <<~ERB
<%= cookies %>
ERB
end
end
end

def assert_raises_forbidden_method_error(method_name, &block)
error = assert_raises(Minitest::Assertion, &block)

assert_includes error.message, "Cannot access ##{method_name} during broadcasts"
end
end
5 changes: 5 additions & 0 deletions test/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
module ApplicationHelper
def current_user
if session.present?
"user@example.com"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= current_user %>
1 change: 1 addition & 0 deletions test/dummy/app/views/_turbo_broadcastable.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Broadcastable <%= local_assigns[:text] %>

0 comments on commit 753f64b

Please sign in to comment.