Skip to content

Commit

Permalink
Tests: Use render when applicable
Browse files Browse the repository at this point in the history
Changes several tests to call `render` with either `partial:` and
`locals:` options, or by passing a model instance directly.

This provides the opportunity to change the underlying partial without
needing to make changes in numerous places in the suite.
  • Loading branch information
seanpdoyle committed Oct 9, 2021
1 parent d4e5c44 commit f4c86a4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 67 deletions.
30 changes: 15 additions & 15 deletions test/streams/broadcastable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,85 +19,85 @@ class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
end

test "broadcasting replace to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: render(@message)) do
@message.broadcast_replace_to "stream"
end
end

test "broadcasting replace now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("replace", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("replace", target: "message_1", template: render(@message)) do
@message.broadcast_replace
end
end

test "broadcasting update to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: render(@message)) do
@message.broadcast_update_to "stream"
end
end

test "broadcasting update now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("update", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("update", target: "message_1", template: render(@message)) do
@message.broadcast_update
end
end

test "broadcasting before to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("before", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("before", target: "message_1", template: render(@message)) do
@message.broadcast_before_to "stream", target: "message_1"
end
end

test "broadcasting after to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("after", target: "message_1", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("after", target: "message_1", template: render(@message)) do
@message.broadcast_after_to "stream", target: "message_1"
end
end

test "broadcasting append to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: render(@message)) do
@message.broadcast_append_to "stream"
end
end

test "broadcasting append to stream with custom target now" do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "board_messages", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "board_messages", template: render(@message)) do
@message.broadcast_append_to "stream", target: "board_messages"
end
end

test "broadcasting append now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("append", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("append", target: "messages", template: render(@message)) do
@message.broadcast_append
end
end

test "broadcasting prepend to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(@message)) do
@message.broadcast_prepend_to "stream"
end
end

test "broadcasting prepend to stream with custom target now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "board_messages", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "board_messages", template: render(@message)) do
@message.broadcast_prepend_to "stream", target: "board_messages"
end
end

test "broadcasting prepend now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("prepend", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("prepend", target: "messages", template: render(@message)) do
@message.broadcast_prepend
end
end

test "broadcasting action to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(@message)) do
@message.broadcast_action_to "stream", action: "prepend"
end
end

test "broadcasting action now" do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("prepend", target: "messages", template: "<p>Hello!</p>") do
assert_broadcast_on @message.to_gid_param, turbo_stream_action_tag("prepend", target: "messages", template: render(@message)) do
@message.broadcast_action "prepend"
end
end
Expand All @@ -111,7 +111,7 @@ class Turbo::BroadcastableTest < ActionCable::Channel::TestCase

test "local variables don't get overwritten if they collide with the template name" do
@profile = Users::Profile.new(id: 1, name: "Ryan")
assert_broadcast_on @profile.to_param, turbo_stream_action_tag("replace", target: "users_profile_1", template: "<p>Hello!</p>") do
assert_broadcast_on @profile.to_param, turbo_stream_action_tag("replace", target: "users_profile_1", template: render(@message)) do
@profile.broadcast_replace partial: 'messages/message', locals: { message: @message }
end
end
Expand Down
100 changes: 60 additions & 40 deletions test/streams/streams_channel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,38 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
end

test "broadcasting replace now" do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_replace_to "stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: render(options)) do
Turbo::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **options
end

assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_replace_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: render(options)) do
Turbo::StreamsChannel.broadcast_replace_to "stream", targets: ".message", **options
end
end

test "broadcasting update now" do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_update_to "stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: render(options)) do
Turbo::StreamsChannel.broadcast_update_to "stream", target: "message_1", **options
end

assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_update_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: render(options)) do
Turbo::StreamsChannel.broadcast_update_to "stream", targets: ".message", **options
end
end

test "broadcasting append now" do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_append_to "stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: render(options)) do
Turbo::StreamsChannel.broadcast_append_to "stream", target: "messages", **options
end

assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_append_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: render(options)) do
Turbo::StreamsChannel.broadcast_append_to "stream", targets: ".message", **options
end
end

Expand All @@ -62,102 +68,116 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
end

test "broadcasting prepend now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_prepend_to "stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(options)) do
Turbo::StreamsChannel.broadcast_prepend_to "stream", target: "messages", **options
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_prepend_to "stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: render(options)) do
Turbo::StreamsChannel.broadcast_prepend_to "stream", targets: ".message", **options
end
end

test "broadcasting action now" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", partial: "messages/message", locals: { message: "hello!" }
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(options)) do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", **options
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: render(options)) do
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", **options
end
end

test "broadcasting replace later" do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: "<p>hello!</p>") do
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("replace", target: "message_1", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_replace_later_to \
"stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
"stream", target: "message_1", **options
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: "<p>hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("replace", targets: ".message", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_replace_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
"stream", targets: ".message", **options
end
end
end

test "broadcasting update later" do
assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: "<p>hello!</p>") do
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("update", target: "message_1", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_update_later_to \
"stream", target: "message_1", partial: "messages/message", locals: { message: "hello!" }
"stream", target: "message_1", **options
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: "<p>hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("update", targets: ".message", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_update_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
"stream", targets: ".message", **options
end
end
end

test "broadcasting append later" do
assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: "<p>hello!</p>") do
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("append", target: "messages", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_append_later_to \
"stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
"stream", target: "messages", **options
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: "<p>hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("append", targets: ".message", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_append_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
"stream", targets: ".message", **options
end
end
end

test "broadcasting prepend later" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_prepend_later_to \
"stream", target: "messages", partial: "messages/message", locals: { message: "hello!" }
"stream", target: "messages", **options
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_prepend_later_to \
"stream", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
"stream", targets: ".message", **options
end
end

end

test "broadcasting action later" do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: "<p>hello!</p>") do
options = { partial: "messages/message", locals: { message: "hello!" } }

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", target: "messages", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_action_later_to \
"stream", action: "prepend", target: "messages", partial: "messages/message", locals: { message: "hello!" }
"stream", action: "prepend", target: "messages", **options
end
end

assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<p>hello!</p>") do
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: render(options)) do
perform_enqueued_jobs do
Turbo::StreamsChannel.broadcast_action_later_to \
"stream", action: "prepend", targets: ".message", partial: "messages/message", locals: { message: "hello!" }
"stream", action: "prepend", targets: ".message", **options
end
end
end
Expand Down

0 comments on commit f4c86a4

Please sign in to comment.