From 22518c9f4cb4e2709f2c173a938510134ce3bde3 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Mon, 1 Apr 2024 23:13:53 +0100 Subject: [PATCH 1/3] rewrite html fragment tests at the broadcast level --- Gemfile.lock | 3 + test/broadcasters/broadcaster_test_case.rb | 56 +++++ .../selector_broadcaster_examples_test.rb | 116 +++++++++ test/html/document_fragment_test.rb | 233 ------------------ test/html/document_test.rb | 170 ------------- 5 files changed, 175 insertions(+), 403 deletions(-) create mode 100644 test/broadcasters/selector_broadcaster_examples_test.rb delete mode 100644 test/html/document_fragment_test.rb delete mode 100644 test/html/document_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index c82a4fb1..744132f5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -123,6 +123,8 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.8) + nokogiri (1.16.2-arm64-darwin) + racc (~> 1.4) nokogiri (1.16.2-x86_64-darwin) racc (~> 1.4) nokogiri (1.16.2-x86_64-linux) @@ -202,6 +204,7 @@ GEM zeitwerk (2.6.8) PLATFORMS + arm64-darwin-22 x86_64-darwin-19 x86_64-darwin-22 x86_64-darwin-23 diff --git a/test/broadcasters/broadcaster_test_case.rb b/test/broadcasters/broadcaster_test_case.rb index efdca3bd..b8a836b0 100644 --- a/test/broadcasters/broadcaster_test_case.rb +++ b/test/broadcasters/broadcaster_test_case.rb @@ -29,6 +29,62 @@ def assert_broadcast_on(stream, data, &block) assert message, "No messages sent with #{data} to #{stream}" end + def assert_morph(selector:, input_html:, output_html:) + broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex) + broadcaster.append_morph(selector, input_html) + + expected = { + "cableReady" => true, + "operations" => [ + { + "selector" => selector, + "html" => output_html, + "payload" => {}, + "childrenOnly" => true, + "permanentAttributeName" => nil, + "stimulusReflex" => { + "some" => "data", + "morph" => "selector" + }, + "reflexId" => "666", + "operation" => "morph" + } + ], + "version" => CableReady::VERSION + } + + assert_broadcast_on @reflex.stream_name, expected do + broadcaster.broadcast nil, some: :data + end + end + + def assert_inner_html(selector:, input_html:, output_html:) + broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex) + broadcaster.append_morph(selector, input_html) + + expected = { + "cableReady" => true, + "operations" => [ + { + "selector" => selector, + "html" => output_html, + "payload" => {}, + "stimulusReflex" => { + "some" => "data", + "morph" => "selector" + }, + "reflexId" => "666", + "operation" => "innerHtml" + } + ], + "version" => CableReady::VERSION + } + + assert_broadcast_on @reflex.stream_name, expected do + broadcaster.broadcast nil, some: :data + end + end + setup do stub_connection(session_id: SecureRandom.uuid) def connection.env diff --git a/test/broadcasters/selector_broadcaster_examples_test.rb b/test/broadcasters/selector_broadcaster_examples_test.rb new file mode 100644 index 00000000..1caf4c99 --- /dev/null +++ b/test/broadcasters/selector_broadcaster_examples_test.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require_relative "broadcaster_test_case" + +module StimulusReflex + class SelectorBroadcasterExamplesTest < StimulusReflex::BroadcasterTestCase + test "morphs the contents of an element when selected by id" do + assert_morph( + selector: "#container", + input_html: '

Home#index

', + output_html: '

Home#index

' + ) + + assert_morph( + selector: "#title", + input_html: '

Home#index

', + output_html: "Home#index" + ) + end + + test "morphs the contents of an element when selected by tag" do + # extract the body + assert_morph( + selector: "body", + input_html: '

Home#index

Wassup

', + output_html: "

Home#index

Wassup

" + ) + end + + test "should properly handle a tr without the parent table" do + assert_morph( + selector: "#foo", + input_html: "
12
", + output_html: "12" + ) + + assert_inner_html( + selector: "#not-there", + input_html: "12", + output_html: "12" + ) + end + + test "should properly handle a td without the parent table or td" do + assert_inner_html( + selector: "#not-there", + input_html: "1", + output_html: "1" + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: '12', + output_html: '12' + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: "1", + output_html: "1" + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: "1", + output_html: "1" + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: "12", + output_html: "12" + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: "12", + output_html: "12" + ) + end + + test "should properly parse " do + assert_inner_html( + selector: "#not-there", + input_html: "12", + output_html: "12" + ) + end + + test "should properly parse