-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup a proxy object that wraps CableReady::Channels (#382)
Setup a proxy object that wraps CableReady::Channels This improves developer ergonimics for emitting CR broadcasts on the SR channel within a reflex.
- Loading branch information
Showing
14 changed files
with
220 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module StimulusReflex | ||
class CableReadyChannels | ||
def initialize(stream_name) | ||
@cable_ready_channels = CableReady::Channels.instance | ||
@stimulus_reflex_channel = @cable_ready_channels[stream_name] | ||
end | ||
|
||
def method_missing(name, *args) | ||
return @stimulus_reflex_channel.send(name, *args) if @stimulus_reflex_channel.respond_to?(name) | ||
@cable_ready_channels.send(name, *args) | ||
end | ||
|
||
def respond_to_missing?(name, include_all) | ||
@stimulus_reflex_channel.respond_to?(name, include_all) || | ||
@cable_ready_channels.respond_to?(name, include_all) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require_relative "../test_helper" | ||
|
||
class StimulusReflex::BroadcasterTestCase < ActionCable::Channel::TestCase | ||
tests StimulusReflex::Channel | ||
|
||
setup do | ||
stub_connection(session_id: SecureRandom.uuid) | ||
def connection.env | ||
@env ||= {} | ||
end | ||
@reflex = StimulusReflex::Reflex.new(subscribe, url: "https://test.stimulusreflex.com") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
require_relative "../test_helper" | ||
|
||
class StimulusReflex::NothingBroadcasterTest < ActiveSupport::TestCase | ||
setup do | ||
@reflex = Minitest::Mock.new | ||
@reflex.expect :stream_name, "TestStream" | ||
end | ||
require_relative "broadcaster_test_case" | ||
|
||
class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCase | ||
test "broadcasts a server message when called" do | ||
broadcaster = StimulusReflex::NothingBroadcaster.new(@reflex) | ||
|
||
cable_ready_channels = Minitest::Mock.new | ||
cable_ready_channel = Minitest::Mock.new | ||
CableReady::Channels.stub :instance, cable_ready_channels do | ||
cable_ready_channel.expect(:dispatch_event, nil, [{name: "stimulus-reflex:server-message", | ||
detail: { | ||
reflexId: nil, | ||
stimulus_reflex: { | ||
some: :data, | ||
morph: :nothing, | ||
server_message: { | ||
subject: "nothing", body: nil | ||
} | ||
} | ||
}}]) | ||
cable_ready_channels.expect(:[], cable_ready_channel, ["TestStream"]) | ||
cable_ready_channels.expect(:broadcast, nil) | ||
broadcaster.broadcast(nil, {some: :data}) | ||
end | ||
expected = { | ||
"cableReady" => true, | ||
"operations" => { | ||
"dispatchEvent" => [ | ||
{ | ||
"name" => "stimulus-reflex:server-message", | ||
"detail" => { | ||
"reflexId" => nil, | ||
"stimulusReflex" => { | ||
"some" => :data, | ||
"morph" => :nothing, | ||
"serverMessage" => { | ||
"subject" => "nothing", | ||
"body" => nil | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert_mock cable_ready_channels | ||
assert_mock cable_ready_channel | ||
assert_broadcast_on @reflex.stream_name, expected do | ||
broadcaster.broadcast nil, some: :data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,73 @@ | ||
require_relative "../test_helper" | ||
|
||
class StimulusReflex::PageBroadcasterTest < ActiveSupport::TestCase | ||
setup do | ||
@reflex = Minitest::Mock.new | ||
@reflex.expect :params, {action: "show"} | ||
@reflex.expect :stream_name, "TestStream" | ||
@reflex.expect :permanent_attribute_name, "some-attribute" | ||
end | ||
require_relative "broadcaster_test_case" | ||
|
||
class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase | ||
test "returns if the response html is empty" do | ||
controller = Minitest::Mock.new | ||
controller.expect(:process, nil, ["show"]) | ||
@reflex.expect :controller, controller | ||
@reflex.expect :controller, controller | ||
broadcaster = StimulusReflex::PageBroadcaster.new(@reflex) | ||
broadcaster.broadcast(["#foo"], {some: :data}) | ||
# TODO: figure out how to refute_broadcast_on | ||
end | ||
|
||
# stub the controller response with a struct responding to :body | ||
controller.expect(:response, Struct.new(:body).new(nil)) | ||
test "performs a page morph on body" do | ||
class << @reflex.controller.response | ||
def body | ||
"<html><head></head><body>New Content</body></html>" | ||
end | ||
end | ||
|
||
broadcaster = StimulusReflex::PageBroadcaster.new(@reflex) | ||
|
||
cable_ready_channels = Minitest::Mock.new | ||
cable_ready_channels.expect(:broadcast, nil) | ||
|
||
broadcaster.broadcast(["#foo"], {some: :data}) | ||
expected = { | ||
"cableReady" => true, | ||
"operations" => { | ||
"morph" => [ | ||
{ | ||
"selector" => "body", | ||
"html" => "New Content", | ||
"childrenOnly" => true, | ||
"permanentAttributeName" => nil, | ||
"stimulusReflex" => { | ||
"some" => :data, | ||
"morph" => :page | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert_raises { cable_ready_channels.verify } | ||
assert_broadcast_on @reflex.stream_name, expected do | ||
broadcaster.broadcast(["body"], {some: :data}) | ||
end | ||
end | ||
|
||
test "performs a page morph given an array of reflex root selectors" do | ||
controller = Minitest::Mock.new | ||
controller.expect(:process, nil, ["show"]) | ||
@reflex.expect :controller, controller | ||
@reflex.expect :controller, controller | ||
|
||
# stub the controller response with a struct responding to :body | ||
controller.expect(:response, Struct.new(:body).new("<html></html>")) | ||
class << @reflex.controller.response | ||
def body | ||
"<html><head></head><body><div id=\"foo\">New Content</div></body></html>" | ||
end | ||
end | ||
|
||
broadcaster = StimulusReflex::PageBroadcaster.new(@reflex) | ||
|
||
cable_ready_channels = Minitest::Mock.new | ||
cable_ready_channel = Minitest::Mock.new | ||
document = Minitest::Mock.new | ||
Nokogiri::HTML.stub :parse, document do | ||
document.expect(:css, "something that is present", ["#foo"]) | ||
document.expect(:css, Struct.new(:inner_html).new("<span>bar</span>"), ["#foo"]) | ||
|
||
CableReady::Channels.stub :instance, cable_ready_channels do | ||
cable_ready_channel.expect(:morph, nil, [{ | ||
selector: "#foo", | ||
html: "<span>bar</span>", | ||
children_only: true, | ||
permanent_attribute_name: "some-attribute", | ||
stimulus_reflex: { | ||
some: :data, | ||
morph: :page | ||
expected = { | ||
"cableReady" => true, | ||
"operations" => { | ||
"morph" => [ | ||
{ | ||
"selector" => "#foo", | ||
"html" => "New Content", | ||
"childrenOnly" => true, | ||
"permanentAttributeName" => nil, | ||
"stimulusReflex" => { | ||
"some" => :data, | ||
"morph" => :page | ||
} | ||
} | ||
}]) | ||
cable_ready_channels.expect(:[], cable_ready_channel, ["TestStream"]) | ||
cable_ready_channels.expect(:broadcast, nil) | ||
] | ||
} | ||
} | ||
|
||
broadcaster.broadcast(["#foo"], {some: :data}) | ||
end | ||
assert_broadcast_on @reflex.stream_name, expected do | ||
broadcaster.broadcast(["#foo"], {some: :data}) | ||
end | ||
|
||
assert_mock cable_ready_channels | ||
assert_mock cable_ready_channel | ||
end | ||
end |
Oops, something went wrong.