stimulus_reflex 3.4.2 → 3.5.0.pre0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +587 -495
  3. data/Gemfile.lock +120 -172
  4. data/LATEST +1 -0
  5. data/README.md +10 -10
  6. data/app/channels/stimulus_reflex/channel.rb +40 -62
  7. data/lib/generators/USAGE +1 -1
  8. data/lib/generators/stimulus_reflex/{config_generator.rb → initializer_generator.rb} +3 -3
  9. data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +3 -2
  10. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +1 -1
  11. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +6 -1
  12. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +7 -4
  13. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +2 -2
  14. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +12 -5
  15. data/lib/stimulus_reflex/cable_ready_channels.rb +6 -2
  16. data/lib/stimulus_reflex/callbacks.rb +55 -5
  17. data/lib/stimulus_reflex/concern_enhancer.rb +37 -0
  18. data/lib/stimulus_reflex/configuration.rb +2 -1
  19. data/lib/stimulus_reflex/element.rb +31 -7
  20. data/lib/stimulus_reflex/policies/reflex_invocation_policy.rb +28 -0
  21. data/lib/stimulus_reflex/reflex.rb +35 -20
  22. data/lib/stimulus_reflex/reflex_data.rb +79 -0
  23. data/lib/stimulus_reflex/reflex_factory.rb +23 -54
  24. data/lib/stimulus_reflex/request_parameters.rb +19 -0
  25. data/lib/stimulus_reflex/{logger.rb → utils/logger.rb} +0 -2
  26. data/lib/stimulus_reflex/{sanity_checker.rb → utils/sanity_checker.rb} +58 -10
  27. data/lib/stimulus_reflex/version.rb +1 -1
  28. data/lib/stimulus_reflex.rb +8 -2
  29. data/lib/tasks/stimulus_reflex/install.rake +6 -4
  30. data/package.json +6 -5
  31. data/stimulus_reflex.gemspec +5 -5
  32. data/test/broadcasters/broadcaster_test_case.rb +1 -1
  33. data/test/broadcasters/nothing_broadcaster_test.rb +5 -3
  34. data/test/broadcasters/page_broadcaster_test.rb +8 -4
  35. data/test/broadcasters/selector_broadcaster_test.rb +171 -55
  36. data/test/callbacks_test.rb +652 -0
  37. data/test/concern_enhancer_test.rb +54 -0
  38. data/test/element_test.rb +181 -0
  39. data/test/reflex_test.rb +1 -1
  40. data/test/test_helper.rb +4 -34
  41. data/test/tmp/app/reflexes/application_reflex.rb +12 -0
  42. data/test/tmp/app/reflexes/user_reflex.rb +44 -0
  43. data/yarn.lock +1138 -919
  44. metadata +36 -23
  45. data/test/reflex_factory_test.rb +0 -79
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StimulusReflex::SanityChecker
4
+ LATEST_VERSION_FORMAT = /^(\d+\.\d+\.\d+)$/
4
5
  NODE_VERSION_FORMAT = /(\d+\.\d+\.\d+.*):/
5
6
  JSON_VERSION_FORMAT = /(\d+\.\d+\.\d+.*)"/
6
7
 
@@ -13,6 +14,8 @@ class StimulusReflex::SanityChecker
13
14
  instance = new
14
15
  instance.check_caching_enabled
15
16
  instance.check_javascript_package_version
17
+ instance.check_default_url_config
18
+ instance.check_new_version_available
16
19
  end
17
20
 
18
21
  private
@@ -24,44 +27,80 @@ class StimulusReflex::SanityChecker
24
27
  end
25
28
 
26
29
  def called_by_generate_config?
27
- ARGV.include? "stimulus_reflex:config"
30
+ ARGV.include? "stimulus_reflex:initializer"
28
31
  end
29
32
  end
30
33
 
31
34
  def check_caching_enabled
32
35
  unless caching_enabled?
33
36
  warn_and_exit <<~WARN
34
- Stimulus Reflex requires caching to be enabled. Caching allows the session to be modified during ActionCable requests.
37
+ StimulusReflex requires caching to be enabled. Caching allows the session to be modified during ActionCable requests.
35
38
  To enable caching in development, run:
36
- rails dev:cache
39
+ rails dev:cache
37
40
  WARN
38
41
  end
39
42
 
40
43
  unless not_null_store?
41
44
  warn_and_exit <<~WARN
42
- Stimulus Reflex requires caching to be enabled. Caching allows the session to be modified during ActionCable requests.
45
+ StimulusReflex requires caching to be enabled. Caching allows the session to be modified during ActionCable requests.
43
46
  But your config.cache_store is set to :null_store, so it won't work.
44
47
  WARN
45
48
  end
46
49
  end
47
50
 
51
+ def check_default_url_config
52
+ unless default_url_config_set?
53
+ warn_and_exit <<~WARN
54
+ StimulusReflex strongly suggests that you set default_url_options in your environment files.
55
+ Otherwise, ActionController and ActionMailer will default to example.com when rendering route helpers.
56
+ You can set your URL options in config/environments/#{Rails.env}.rb
57
+ config.action_controller.default_url_options = {host: "localhost", port: 3000}
58
+ config.action_mailer.default_url_options = {host: "localhost", port: 3000}
59
+ Please update every environment with the appropriate URL. Typically, no port is necessary in production.
60
+ WARN
61
+ end
62
+ end
63
+
48
64
  def check_javascript_package_version
49
65
  if javascript_package_version.nil?
50
66
  warn_and_exit <<~WARN
51
- Can't locate the stimulus_reflex NPM package.
67
+ Can't locate the stimulus_reflex npm package.
52
68
  Either add it to your package.json as a dependency or use "yarn link stimulus_reflex" if you are doing development.
53
69
  WARN
54
70
  end
55
71
 
56
72
  unless javascript_version_matches?
57
73
  warn_and_exit <<~WARN
58
- The Stimulus Reflex javascript package version (#{javascript_package_version}) does not match the Rubygem version (#{gem_version}).
59
- To update the Stimulus Reflex npm package:
60
- yarn upgrade stimulus_reflex@#{gem_version}
74
+ The stimulus_reflex npm package version (#{javascript_package_version}) does not match the Rubygem version (#{gem_version}).
75
+ To update the stimulus_reflex npm package:
76
+ yarn upgrade stimulus_reflex@#{gem_version}
61
77
  WARN
62
78
  end
63
79
  end
64
80
 
81
+ def check_new_version_available
82
+ return unless Rails.env.development?
83
+ return if StimulusReflex.config.on_new_version_available == :ignore
84
+ return unless using_stable_release
85
+ begin
86
+ latest_version = URI.open("https://raw.githubusercontent.com/stimulusreflex/stimulus_reflex/master/LATEST", open_timeout: 1, read_timeout: 1).read.strip
87
+ if latest_version != StimulusReflex::VERSION
88
+ puts <<~WARN
89
+
90
+ There is a new version of StimulusReflex available!
91
+ Current: #{StimulusReflex::VERSION} Latest: #{latest_version}
92
+
93
+ If you upgrade, it is very important that you update BOTH Gemfile and package.json
94
+ Then, run `bundle install && yarn install` to update to #{latest_version}.
95
+
96
+ WARN
97
+ exit if StimulusReflex.config.on_new_version_available == :exit
98
+ end
99
+ rescue
100
+ puts "StimulusReflex #{StimulusReflex::VERSION} update check skipped: connection timeout"
101
+ end
102
+ end
103
+
65
104
  private
66
105
 
67
106
  def caching_enabled?
@@ -72,10 +111,20 @@ class StimulusReflex::SanityChecker
72
111
  Rails.application.config.cache_store != :null_store
73
112
  end
74
113
 
114
+ def default_url_config_set?
115
+ Rails.application.config.action_controller.default_url_options
116
+ end
117
+
75
118
  def javascript_version_matches?
76
119
  javascript_package_version == gem_version
77
120
  end
78
121
 
122
+ def using_stable_release
123
+ stable = StimulusReflex::VERSION.match?(LATEST_VERSION_FORMAT)
124
+ puts "StimulusReflex #{StimulusReflex::VERSION} update check skipped: pre-release build" unless stable
125
+ stable
126
+ end
127
+
79
128
  def gem_version
80
129
  @_gem_version ||= StimulusReflex::VERSION.gsub(".pre", "-pre")
81
130
  end
@@ -118,7 +167,6 @@ class StimulusReflex::SanityChecker
118
167
  def exit_with_info
119
168
  puts
120
169
 
121
- # bundle exec rails generate stimulus_reflex:config
122
170
  if File.exist?(initializer_path)
123
171
  puts <<~INFO
124
172
  If you know what you are doing and you want to start the application anyway,
@@ -139,7 +187,7 @@ class StimulusReflex::SanityChecker
139
187
 
140
188
  Then open your initializer at
141
189
 
142
- <RAILS_ROOT>/config/initializers/stimulus_reflex.rb
190
+ #{initializer_path}
143
191
 
144
192
  and then add the following directive:
145
193
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusReflex
4
- VERSION = "3.4.2"
4
+ VERSION = "3.5.0.pre0"
5
5
  end
@@ -1,27 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "uri"
4
+ require "open-uri"
4
5
  require "rack"
5
6
  require "rails/engine"
6
7
  require "active_support/all"
7
8
  require "action_dispatch"
8
9
  require "action_cable"
10
+ require "action_view"
9
11
  require "nokogiri"
10
12
  require "cable_ready"
11
13
  require "stimulus_reflex/version"
12
14
  require "stimulus_reflex/cable_ready_channels"
15
+ require "stimulus_reflex/concern_enhancer"
13
16
  require "stimulus_reflex/configuration"
14
17
  require "stimulus_reflex/callbacks"
18
+ require "stimulus_reflex/request_parameters"
15
19
  require "stimulus_reflex/reflex"
20
+ require "stimulus_reflex/reflex_data"
16
21
  require "stimulus_reflex/reflex_factory"
17
22
  require "stimulus_reflex/element"
18
- require "stimulus_reflex/sanity_checker"
19
23
  require "stimulus_reflex/broadcasters/broadcaster"
20
24
  require "stimulus_reflex/broadcasters/nothing_broadcaster"
21
25
  require "stimulus_reflex/broadcasters/page_broadcaster"
22
26
  require "stimulus_reflex/broadcasters/selector_broadcaster"
27
+ require "stimulus_reflex/policies/reflex_invocation_policy"
23
28
  require "stimulus_reflex/utils/colorize"
24
- require "stimulus_reflex/logger"
29
+ require "stimulus_reflex/utils/logger"
30
+ require "stimulus_reflex/utils/sanity_checker"
25
31
 
26
32
  module StimulusReflex
27
33
  class Engine < Rails::Engine
@@ -44,14 +44,16 @@ namespace :stimulus_reflex do
44
44
  end
45
45
 
46
46
  initialize_line = lines.find { |line| line.start_with?("StimulusReflex.initialize") }
47
- lines << "StimulusReflex.initialize(application, { consumer, controller, isolate: true })\n" unless initialize_line
47
+ lines << "application.consumer = consumer\n"
48
+ lines << "StimulusReflex.initialize(application, { controller, isolate: true })\n" unless initialize_line
48
49
  lines << "StimulusReflex.debug = process.env.RAILS_ENV === 'development'\n" unless initialize_line
49
50
  File.open(filepath, "w") { |f| f.write lines.join }
50
51
 
51
52
  filepath = Rails.root.join("config/environments/development.rb")
52
53
  lines = File.open(filepath, "r") { |f| f.readlines }
53
54
  unless lines.find { |line| line.include?("config.session_store") }
54
- lines.insert 3, " config.session_store :cache_store\n\n"
55
+ matches = lines.select { |line| line =~ /\A(Rails.application.configure do)/ }
56
+ lines.insert lines.index(matches.last).to_i + 1, " config.session_store :cache_store\n\n"
55
57
  File.open(filepath, "w") { |f| f.write lines.join }
56
58
  end
57
59
 
@@ -61,7 +63,7 @@ namespace :stimulus_reflex do
61
63
  lines.delete_at 1
62
64
  lines.insert 1, " adapter: redis\n"
63
65
  lines.insert 2, " url: <%= ENV.fetch(\"REDIS_URL\") { \"redis://localhost:6379/1\" } %>\n"
64
- lines.insert 3, " channel_prefix: " + File.basename(Rails.root.to_s).underscore + "_development\n"
66
+ lines.insert 3, " channel_prefix: " + File.basename(Rails.root.to_s).tr("\\", "").tr("-. ", "_").underscore + "_development\n"
65
67
  File.open(filepath, "w") { |f| f.write lines.join }
66
68
  end
67
69
 
@@ -71,7 +73,7 @@ namespace :stimulus_reflex do
71
73
 
72
74
  puts
73
75
  puts "StimulusReflex and CableReady have been successfully installed!"
74
- puts "Go to https://docs.stimulusreflex.com/quickstart if you need help getting started."
76
+ puts "Go to https://docs.stimulusreflex.com/hello-world/quickstart if you need help getting started."
75
77
  puts
76
78
  end
77
79
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stimulus_reflex",
3
- "version": "3.4.2",
3
+ "version": "3.4.1",
4
4
  "description": "Build reactive applications with the Rails tooling you already know and love.",
5
5
  "keywords": [
6
6
  "ruby",
@@ -20,11 +20,11 @@
20
20
  ],
21
21
  "homepage": "https://docs.stimulusreflex.com/",
22
22
  "bugs": {
23
- "url": "https://github.com/hopsoft/stimulus_reflex/issues"
23
+ "url": "https://github.com/stimulusreflex/stimulus_reflex/issues"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
27
- "url": "git+https://github.com:hopsoft/stimulus_reflex.git"
27
+ "url": "git+https://github.com:stimulusreflex/stimulus_reflex.git"
28
28
  },
29
29
  "license": "MIT",
30
30
  "author": "Nathan Hopkins <natehop@gmail.com>",
@@ -34,14 +34,14 @@
34
34
  "postinstall": "node ./javascript/scripts/post_install.js",
35
35
  "prettier-standard:check": "yarn run prettier-standard --check ./javascript/*.js ./javascript/**/*.js",
36
36
  "prettier-standard:format": "yarn run prettier-standard ./javascript/*.js ./javascript/**/*.js",
37
- "test": "yarn run mocha --require @babel/register --require esm ./javascript/test"
37
+ "test": "yarn run mocha --require @babel/register --require jsdom-global/register --require esm ./javascript/test"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "stimulus": ">= 1.1"
41
41
  },
42
42
  "dependencies": {
43
43
  "@rails/actioncable": ">= 6.0",
44
- "cable_ready": "^4.5.0"
44
+ "cable_ready": "5.0.0-pre0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@babel/core": "^7.6.2",
@@ -50,6 +50,7 @@
50
50
  "assert": "^2.0.0",
51
51
  "esm": "^3.2.25",
52
52
  "jsdom": "^16.0.1",
53
+ "jsdom-global": "^3.0.2",
53
54
  "mocha": "^8.0.1",
54
55
  "prettier-standard": "^16.1.0",
55
56
  "stimulus": ">= 1.1"
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.version = StimulusReflex::VERSION
9
9
  gem.authors = ["Nathan Hopkins"]
10
10
  gem.email = ["natehop@gmail.com"]
11
- gem.homepage = "https://github.com/hopsoft/stimulus_reflex"
11
+ gem.homepage = "https://github.com/stimulusreflex/stimulus_reflex"
12
12
  gem.summary = "Build reactive applications with the Rails tooling you already know and love."
13
13
  gem.post_install_message = <<~MESSAGE
14
14
  Friendly reminder: When updating the stimulus_reflex gem,
@@ -18,8 +18,8 @@ Gem::Specification.new do |gem|
18
18
  MESSAGE
19
19
 
20
20
  gem.metadata = {
21
- "bug_tracker_uri" => "https://github.com/hopsoft/stimulus_reflex/issues",
22
- "changelog_uri" => "https://github.com/hopsoft/stimulus_reflex/CHANGELOG.md",
21
+ "bug_tracker_uri" => "https://github.com/stimulusreflex/stimulus_reflex/issues",
22
+ "changelog_uri" => "https://github.com/stimulusreflex/stimulus_reflex/CHANGELOG.md",
23
23
  "documentation_uri" => "https://docs.stimulusreflex.com",
24
24
  "homepage_uri" => gem.homepage,
25
25
  "source_code_uri" => gem.homepage
@@ -32,11 +32,11 @@ Gem::Specification.new do |gem|
32
32
  gem.add_dependency "nokogiri"
33
33
  gem.add_dependency "rails", ">= 5.2"
34
34
  gem.add_dependency "redis"
35
- gem.add_dependency "cable_ready", "~> 4.5"
35
+ gem.add_dependency "cable_ready", "5.0.0.pre0"
36
36
 
37
37
  gem.add_development_dependency "bundler", "~> 2.0"
38
38
  gem.add_development_dependency "pry-nav"
39
39
  gem.add_development_dependency "pry"
40
40
  gem.add_development_dependency "rake"
41
- gem.add_development_dependency "standardrb"
41
+ gem.add_development_dependency "standardrb", "~> 1.0"
42
42
  end
@@ -10,6 +10,6 @@ class StimulusReflex::BroadcasterTestCase < ActionCable::Channel::TestCase
10
10
  def connection.env
11
11
  @env ||= {}
12
12
  end
13
- @reflex = StimulusReflex::Reflex.new(subscribe, url: "https://test.stimulusreflex.com")
13
+ @reflex = StimulusReflex::Reflex.new(subscribe, url: "https://test.stimulusreflex.com", client_attributes: {reflex_id: "666"})
14
14
  end
15
15
  end
@@ -13,7 +13,8 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
13
13
  {
14
14
  "name" => "stimulus-reflex:server-message",
15
15
  "detail" => {
16
- "reflexId" => nil,
16
+ "reflexId" => "666",
17
+ "payload" => {},
17
18
  "stimulusReflex" => {
18
19
  "some" => :data,
19
20
  "morph" => :nothing,
@@ -22,14 +23,15 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
22
23
  "body" => nil
23
24
  }
24
25
  }
25
- }
26
+ },
27
+ "reflexId" => "666"
26
28
  }
27
29
  ]
28
30
  }
29
31
  }
30
32
 
31
33
  assert_broadcast_on @reflex.stream_name, expected do
32
- broadcaster.broadcast nil, some: :data
34
+ broadcaster.broadcast nil, {:some => :data, "reflexId" => "666"}
33
35
  end
34
36
  end
35
37
  end
@@ -12,7 +12,7 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
12
12
  test "performs a page morph on body" do
13
13
  class << @reflex.controller.response
14
14
  def body
15
- "<html><head></head><body>New Content</body></html>"
15
+ "<html><head></head><body><div>New Content</div><div>Another Content</div></body></html>"
16
16
  end
17
17
  end
18
18
 
@@ -24,13 +24,15 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
24
24
  "morph" => [
25
25
  {
26
26
  "selector" => "body",
27
- "html" => "New Content",
27
+ "html" => "<div>New Content</div><div>Another Content</div>",
28
+ "payload" => {},
28
29
  "childrenOnly" => true,
29
30
  "permanentAttributeName" => nil,
30
31
  "stimulusReflex" => {
31
32
  "some" => :data,
32
33
  "morph" => :page
33
- }
34
+ },
35
+ "reflexId" => "666"
34
36
  }
35
37
  ]
36
38
  }
@@ -57,12 +59,14 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
57
59
  {
58
60
  "selector" => "#foo",
59
61
  "html" => "New Content",
62
+ "payload" => {},
60
63
  "childrenOnly" => true,
61
64
  "permanentAttributeName" => nil,
62
65
  "stimulusReflex" => {
63
66
  "some" => :data,
64
67
  "morph" => :page
65
- }
68
+ },
69
+ "reflexId" => "666"
66
70
  }
67
71
  ]
68
72
  }
@@ -1,57 +1,173 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # require_relative "broadcaster_test_case"
4
-
5
- # class StimulusReflex::SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
6
- # test "morphs the contents of an element if the selector(s) are present in both original and morphed html fragments" do
7
- # broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
8
- # broadcaster.append_morph("#foo", "<div id=\"foo\"><span>bar</span></div>")
9
-
10
- # expected = {
11
- # "cableReady" => true,
12
- # "operations" => {
13
- # "morph" => [
14
- # {
15
- # "selector" => "#foo",
16
- # "html" => "<span>bar</span>",
17
- # "childrenOnly" => true,
18
- # "permanentAttributeName" => nil,
19
- # "stimulusReflex" => {
20
- # "some" => :data,
21
- # "morph" => :selector
22
- # }
23
- # }
24
- # ]
25
- # }
26
- # }
27
-
28
- # assert_broadcast_on @reflex.stream_name, expected do
29
- # broadcaster.broadcast nil, some: :data
30
- # end
31
- # end
32
-
33
- # test "replaces the contents of an element and ignores permanent-attributes if the selector(s) aren't present in the replacing html fragment" do
34
- # broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
35
- # broadcaster.append_morph("#foo", "<div id=\"baz\"><span>bar</span></div>")
36
-
37
- # expected = {
38
- # "cableReady" => true,
39
- # "operations" => {
40
- # "innerHtml" => [
41
- # {
42
- # "selector" => "#foo",
43
- # "html" => "<div id=\"baz\"><span>bar</span></div>",
44
- # "stimulusReflex" => {
45
- # "some" => :data,
46
- # "morph" => :selector
47
- # }
48
- # }
49
- # ]
50
- # }
51
- # }
52
-
53
- # assert_broadcast_on @reflex.stream_name, expected do
54
- # broadcaster.broadcast nil, some: :data
55
- # end
56
- # end
57
- # end
3
+ require_relative "broadcaster_test_case"
4
+
5
+ module StimulusReflex
6
+ class SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
7
+ test "morphs the contents of an element if the selector(s) are present in both original and morphed html fragments" do
8
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
9
+ broadcaster.append_morph("#foo", '<div id="foo"><div>bar</div><div>baz</div></div>')
10
+
11
+ expected = {
12
+ "cableReady" => true,
13
+ "operations" => {
14
+ "morph" => [
15
+ {
16
+ "selector" => "#foo",
17
+ "html" => "<div>bar</div><div>baz</div>",
18
+ "payload" => {},
19
+ "childrenOnly" => true,
20
+ "permanentAttributeName" => nil,
21
+ "stimulusReflex" => {
22
+ "some" => "data",
23
+ "morph" => "selector"
24
+ },
25
+ "reflexId" => "666"
26
+ }
27
+ ]
28
+ }
29
+ }
30
+
31
+ assert_broadcast_on @reflex.stream_name, expected do
32
+ broadcaster.broadcast nil, some: :data
33
+ end
34
+ end
35
+
36
+ test "replaces the contents of an element and ignores permanent-attributes if the selector(s) aren't present in the replacing html fragment" do
37
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
38
+ broadcaster.append_morph("#foo", '<div id="baz"><span>bar</span></div>')
39
+
40
+ expected = {
41
+ "cableReady" => true,
42
+ "operations" => {
43
+ "innerHtml" => [
44
+ {
45
+ "selector" => "#foo",
46
+ "html" => '<div id="baz"><span>bar</span></div>',
47
+ "payload" => {},
48
+ "stimulusReflex" => {
49
+ "some" => "data",
50
+ "morph" => "selector"
51
+ },
52
+ "reflexId" => "666"
53
+ }
54
+ ]
55
+ }
56
+ }
57
+
58
+ assert_broadcast_on @reflex.stream_name, expected do
59
+ broadcaster.broadcast nil, some: :data
60
+ end
61
+ end
62
+
63
+ test "morphs the contents of an element to an empty string if no content specified" do
64
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
65
+ broadcaster.append_morph("#foo", nil)
66
+
67
+ expected = {
68
+ "cableReady" => true,
69
+ "operations" => {
70
+ "innerHtml" => [
71
+ {
72
+ "selector" => "#foo",
73
+ "html" => "",
74
+ "payload" => {},
75
+ "stimulusReflex" => {
76
+ "some" => "data",
77
+ "morph" => "selector"
78
+ },
79
+ "reflexId" => "666"
80
+ }
81
+ ]
82
+ }
83
+ }
84
+
85
+ assert_broadcast_on @reflex.stream_name, expected do
86
+ broadcaster.broadcast nil, some: :data
87
+ end
88
+ end
89
+
90
+ test "morphs the contents of an element to an empty string if empty specified" do
91
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
92
+ broadcaster.append_morph("#foo", "")
93
+
94
+ expected = {
95
+ "cableReady" => true,
96
+ "operations" => {
97
+ "innerHtml" => [
98
+ {
99
+ "selector" => "#foo",
100
+ "html" => "",
101
+ "payload" => {},
102
+ "stimulusReflex" => {
103
+ "some" => "data",
104
+ "morph" => "selector"
105
+ },
106
+ "reflexId" => "666"
107
+ }
108
+ ]
109
+ }
110
+ }
111
+
112
+ assert_broadcast_on @reflex.stream_name, expected do
113
+ broadcaster.broadcast nil, some: :data
114
+ end
115
+ end
116
+
117
+ test "morphs the contents of an element to an empty string if no content specified, hash form" do
118
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
119
+ broadcaster.append_morph({"#foo": nil}, nil)
120
+
121
+ expected = {
122
+ "cableReady" => true,
123
+ "operations" => {
124
+ "innerHtml" => [
125
+ {
126
+ "selector" => "#foo",
127
+ "html" => "",
128
+ "payload" => {},
129
+ "stimulusReflex" => {
130
+ "some" => "data",
131
+ "morph" => "selector"
132
+ },
133
+ "reflexId" => "666"
134
+ }
135
+ ]
136
+ }
137
+ }
138
+
139
+ assert_broadcast_on @reflex.stream_name, expected do
140
+ broadcaster.broadcast nil, some: :data
141
+ end
142
+ end
143
+
144
+ test "morphs the contents of an element to specified HTML, hash form" do
145
+ broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
146
+ broadcaster.append_morph({"#foo": '<div id="foo"><div>bar</div><div>baz</div></div>'}, nil)
147
+
148
+ expected = {
149
+ "cableReady" => true,
150
+ "operations" => {
151
+ "morph" => [
152
+ {
153
+ "selector" => "#foo",
154
+ "html" => "<div>bar</div><div>baz</div>",
155
+ "payload" => {},
156
+ "childrenOnly" => true,
157
+ "permanentAttributeName" => nil,
158
+ "stimulusReflex" => {
159
+ "some" => "data",
160
+ "morph" => "selector"
161
+ },
162
+ "reflexId" => "666"
163
+ }
164
+ ]
165
+ }
166
+ }
167
+
168
+ assert_broadcast_on @reflex.stream_name, expected do
169
+ broadcaster.broadcast nil, some: :data
170
+ end
171
+ end
172
+ end
173
+ end