Skip to content

Commit

Permalink
convert symbol keys, ensure fragment html exists (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
leastbad authored May 11, 2021
1 parent 630b1b8 commit 80d4d83
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/stimulus_reflex/broadcasters/selector_broadcaster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def broadcast(_, data = {})
updates.each do |key, value|
html = reflex.render(key) if key.is_a?(ActiveRecord::Base) && value.nil?
html = reflex.render_collection(key) if key.is_a?(ActiveRecord::Relation) && value.nil?
fragment = Nokogiri::HTML.fragment(html&.to_s || "")

selector = key.is_a?(ActiveRecord::Base) || key.is_a?(ActiveRecord::Relation) ? reflex.dom_id(key) : key
html ||= value
fragment = Nokogiri::HTML.fragment(html.to_s)
selector = key.is_a?(ActiveRecord::Base) || key.is_a?(ActiveRecord::Relation) ? reflex.dom_id(key) : key.to_s
match = fragment.at_css(selector)
if match.present?
operations << [selector, :morph]
Expand Down
110 changes: 110 additions & 0 deletions test/broadcasters/selector_broadcaster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,115 @@ class SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
broadcaster.broadcast nil, some: :data
end
end

test "morphs the contents of an element to an empty string if no content specified" do
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
broadcaster.append_morph("#foo", nil)

expected = {
"cableReady" => true,
"operations" => {
"innerHtml" => [
{
"selector" => "#foo",
"html" => "",
"payload" => {},
"stimulusReflex" => {
"some" => "data",
"morph" => "selector"
},
"reflexId" => "666"
}
]
}
}

assert_broadcast_on @reflex.stream_name, expected do
broadcaster.broadcast nil, some: :data
end
end

test "morphs the contents of an element to an empty string if empty specified" do
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
broadcaster.append_morph("#foo", "")

expected = {
"cableReady" => true,
"operations" => {
"innerHtml" => [
{
"selector" => "#foo",
"html" => "",
"payload" => {},
"stimulusReflex" => {
"some" => "data",
"morph" => "selector"
},
"reflexId" => "666"
}
]
}
}

assert_broadcast_on @reflex.stream_name, expected do
broadcaster.broadcast nil, some: :data
end
end

test "morphs the contents of an element to an empty string if no content specified, hash form" do
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
broadcaster.append_morph({"#foo": nil}, nil)

expected = {
"cableReady" => true,
"operations" => {
"innerHtml" => [
{
"selector" => "#foo",
"html" => "",
"payload" => {},
"stimulusReflex" => {
"some" => "data",
"morph" => "selector"
},
"reflexId" => "666"
}
]
}
}

assert_broadcast_on @reflex.stream_name, expected do
broadcaster.broadcast nil, some: :data
end
end

test "morphs the contents of an element to specified HTML, hash form" do
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
broadcaster.append_morph({"#foo": '<div id="foo"><div>bar</div><div>baz</div></div>'}, nil)

expected = {
"cableReady" => true,
"operations" => {
"morph" => [
{
"selector" => "#foo",
"html" => "<div>bar</div><div>baz</div>",
"payload" => {},
"childrenOnly" => true,
"permanentAttributeName" => nil,
"stimulusReflex" => {
"some" => "data",
"morph" => "selector"
},
"reflexId" => "666"
}
]
}
}

assert_broadcast_on @reflex.stream_name, expected do
broadcaster.broadcast nil, some: :data
end
end
end
end

0 comments on commit 80d4d83

Please sign in to comment.