Skip to content

Commit

Permalink
Merge pull request #160 from stimulusreflex/fix-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
julianrubisch authored Sep 9, 2023
2 parents 45ddffa + f05a8af commit 5af940e
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/standardrb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Set up Ruby 2.6
- name: Set up Ruby 3
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [2.7.2, '3.0.2', '3.1.0']
ruby-version: ['3.0.2', '3.1.4', '3.2.2']
rails-version: ['6_0', '6_1', '7_0']
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails-version }}.gemfile
Expand Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ GEM
activesupport (>= 5.2)
railties (>= 5.2)
thread-local (>= 1.1.0)
coderay (1.1.3)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
Expand Down Expand Up @@ -121,12 +120,11 @@ GEM
nio4r (2.5.9)
nokogiri (1.14.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.2-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
racc (1.6.2)
rack (2.2.6.4)
rack-test (2.1.0)
Expand Down Expand Up @@ -178,6 +176,7 @@ GEM
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.13.0)
sqlite3 (1.6.1-arm64-darwin)
sqlite3 (1.6.1-x86_64-linux)
standard (1.25.3)
language_server-protocol (~> 3.17.0.2)
rubocop (~> 1.48.1)
Expand All @@ -197,13 +196,14 @@ GEM

PLATFORMS
arm64-darwin-21
arm64-darwin-22
x86_64-linux

DEPENDENCIES
appraisal
bundler (~> 2.0)
futurism!
nokogiri
pry (~> 0.12.2)
rake (~> 13.0)
sqlite3
standardrb
Expand Down
3 changes: 0 additions & 3 deletions futurism.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ Gem::Specification.new do |spec|
"[A-Z]*"
]

spec.test_files = Dir["test/**/*.rb"]

spec.add_development_dependency "appraisal"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "pry", "~> 0.12.2"
spec.add_development_dependency "nokogiri"
spec.add_development_dependency "standardrb"
spec.add_development_dependency "sqlite3"
Expand Down
8 changes: 4 additions & 4 deletions lib/futurism/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def futurize(records_or_string = nil, extends: :div, **options, &block)
end
end

options[:eager] = true unless block_given?
options[:eager] = true unless block

# cannot serialize a proc
options.delete(:cached) if options[:cached].is_a?(Proc)
Expand All @@ -27,7 +27,7 @@ def futurize(records_or_string = nil, extends: :div, **options, &block)
def futurize_with_options(extends:, **options, &block)
collection = options.delete(:collection)
if collection.nil?
placeholder = capture(&block) if block_given?
placeholder = capture(&block) if block

WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options).render
else
Expand All @@ -36,7 +36,7 @@ def futurize_with_options(extends:, **options, &block)
broadcast_each = options.delete(:broadcast_each) || false

collection.each_with_index.map { |record, index|
placeholder = capture(record, index, &block) if block_given?
placeholder = capture(record, index, &block) if block

WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options.deep_merge(
broadcast_each: broadcast_each,
Expand All @@ -48,7 +48,7 @@ def futurize_with_options(extends:, **options, &block)

def futurize_active_record(records, extends:, **options, &block)
Array(records).map.with_index { |record, index|
placeholder = capture(record, index, &block) if block_given?
placeholder = capture(record, index, &block) if block

WrappingFuturismElement.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
}.join.html_safe
Expand Down
2 changes: 1 addition & 1 deletion lib/futurism/options_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def dump_options(options)
def load_options(options)
require_relative "shims/deep_transform_values" unless options.respond_to? :deep_transform_values

options.deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
options.deep_transform_values { |value| (value.is_a?(String) && value.start_with?("gid://")) ? GlobalID::Locator.locate(value) : value }
end
end
end
6 changes: 3 additions & 3 deletions lib/futurism/resolver/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def render?

def renderer_for(resource_definition:)
Resolver::Controller::Renderer.for(controller: resource_definition.controller,
connection: @connection,
url: resource_definition.url,
params: @params)
connection: @connection,
url: resource_definition.url,
params: @params)
end

def resolved_models
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/futurism_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ namespace :futurism do
subscribe_line = lines.find { |line| line.start_with?("Futurism.createSubscription") }
lines << "Futurism.createSubscription(consumer)\n" unless subscribe_line

File.open(filepath, "w") { |f| f.write lines.join }
File.write(filepath, lines.join)
end
end
42 changes: 22 additions & 20 deletions test/cable/channel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ def with_mocked_renderer
end
end

def with_mocked_cable_ready
cable_ready_mock = MiniTest::Mock.new
cable_ready_channel = MiniTest::Mock.new
cable_ready_channel.expect(:outer_html, nil, [Hash])
cable_ready_channel.expect(:outer_html, nil, [Hash])
def with_mocked_cable_ready(signed_params:, html:)
cable_ready_mock = Minitest::Mock.new
cable_ready_channel = Minitest::Mock.new

cable_ready_channel.expect(:outer_html, nil, [], selector: "[data-signed-params='#{signed_params.first}']", html: html.first)
cable_ready_channel.expect(:outer_html, nil, [], selector: "[data-signed-params='#{signed_params.second}']", html: html.second)

cable_ready_mock.expect(:[], cable_ready_channel, ["1"])
cable_ready_mock.expect(:[], cable_ready_channel, ["1"])
Expand Down Expand Up @@ -195,16 +196,17 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase
end

test "broadcasts elements of a collection immediately" do
with_mocked_cable_ready do |cable_ready_mock|
Post.create title: "Lorem"
Post.create title: "Ipsum"
fragment = Nokogiri::HTML.fragment(futurize(partial: "posts/card", collection: Post.all, broadcast_each: true, locals: {important_local: "needed to render"}) {})
subscribe
Post.create title: "Lorem"
Post.create title: "Ipsum"
fragment = Nokogiri::HTML.fragment(futurize(partial: "posts/card", collection: Post.all, broadcast_each: true, locals: {important_local: "needed to render"}) {})
subscribe

signed_params_1 = fragment.children.first["data-signed-params"]
broadcast_each_1 = fragment.children.first["data-broadcast-each"]
signed_params_2 = fragment.children.last["data-signed-params"]
broadcast_each_2 = fragment.children.last["data-broadcast-each"]

signed_params_1 = fragment.children.first["data-signed-params"]
broadcast_each_1 = fragment.children.first["data-broadcast-each"]
signed_params_2 = fragment.children.last["data-signed-params"]
broadcast_each_2 = fragment.children.last["data-broadcast-each"]
with_mocked_cable_ready(signed_params: [signed_params_1, signed_params_2], html: ["<div class=\"card\">\n Lorem\n <a href=\"/posts/1/edit\">Edit</a>\n</div>\n", "<div class=\"card\">\n Ipsum\n <a href=\"/posts/2/edit\">Edit</a>\n</div>\n"]) do |cable_ready_mock|
perform :receive, {"signed_params" => [signed_params_1, signed_params_2], "broadcast_each" => [broadcast_each_1, broadcast_each_2]}

assert_mock cable_ready_mock
Expand All @@ -228,8 +230,8 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase
subscribe(channel: "Futurism::Channel")

assert_cable_ready_operation_on("Futurism::Channel:1", operation: "outerHtml",
selector: "[data-signed-params='#{signed_params}']",
html: "<div class=\"card\">\n Lorem\n <a href=\"/posts/1/edit\">Edit</a>\n</div>\n") do
selector: "[data-signed-params='#{signed_params}']",
html: "<div class=\"card\">\n Lorem\n <a href=\"/posts/1/edit\">Edit</a>\n</div>\n") do
perform :receive, {"signed_params" => [signed_params]}
end
end
Expand Down Expand Up @@ -257,8 +259,8 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase
subscribe(channel: "Futurism::Channel")

assert_cable_ready_operation_on("Futurism::Channel:1", operation: "outerHtml",
selector: "[data-signed-params='#{signed_params}']",
html: /Missing partial INVALID\/_PARTIAL/) do
selector: "[data-signed-params='#{signed_params}']",
html: /Missing partial INVALID\/_PARTIAL/) do
perform :receive, {"signed_params" => [signed_params]}
end
end
Expand All @@ -270,8 +272,8 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase
subscribe(channel: "Futurism::Channel")

assert_cable_ready_operation_on("Futurism::Channel:1", operation: "outerHtml",
selector: "[data-signed-params='#{signed_params}']",
html: /undefined local variable or method/) do
selector: "[data-signed-params='#{signed_params}']",
html: /undefined local variable or method/) do
perform :receive, {"signed_params" => [signed_params]}
end
end
Expand Down
48 changes: 24 additions & 24 deletions test/resolver/controller/renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test ".for controller configures renderer" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: ApplicationController,
connection: dummy_connection,
url: "http://www.example.org?someParam=1234",
params: {"SOME" => "SOME_VALUE"})
connection: dummy_connection,
url: "http://www.example.org?someParam=1234",
params: {"SOME" => "SOME_VALUE"})
assert_equal renderer.controller, ApplicationController

assert_equal renderer.render(inline: "<%= request.env['HTTP_VAR'] %>"), "HTTP_VAR_VALUE"
Expand All @@ -40,9 +40,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test ".for controller configures renderer using the passed in controller" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: DummyController,
connection: dummy_connection,
url: "http://www.example.org?someParam=1234",
params: {"SOME" => "SOME_VALUE"})
connection: dummy_connection,
url: "http://www.example.org?someParam=1234",
params: {"SOME" => "SOME_VALUE"})
assert_equal renderer.controller, DummyController

assert_equal renderer.render(inline: "<%= request.env['HTTP_VAR'] %>"), "HTTP_VAR_VALUE"
Expand All @@ -52,9 +52,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test "renderer.render resolves helper methods" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: DummyController,
connection: dummy_connection,
url: "http://www.example.org?name=the%20future",
params: {})
connection: dummy_connection,
url: "http://www.example.org?name=the%20future",
params: {})

rendered_html = renderer.render(inline: "Hi <%= name_helper %>")

Expand All @@ -63,9 +63,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test "renderer.render resolves helper methods that rely on params from controller" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: DummyController,
connection: dummy_connection,
url: "http://www.example.org?name=the%20future!",
params: {})
connection: dummy_connection,
url: "http://www.example.org?name=the%20future!",
params: {})

rendered_html = renderer.render(inline: "Hi <%= controller_and_action_helper %>")

Expand All @@ -74,9 +74,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test "renderer.render resolves helper methods that rely on params from url" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: DummyController,
connection: dummy_connection,
url: "http://www.example.org?name=the%20future!",
params: {})
connection: dummy_connection,
url: "http://www.example.org?name=the%20future!",
params: {})
rendered_html = renderer.render(inline: "Hi <%= name_from_params_helper %>")

assert_equal rendered_html, "Hi the future!"
Expand All @@ -86,9 +86,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase
post = Post.create(title: "Lorem")

renderer = Futurism::Resolver::Controller::Renderer.for(controller: ApplicationController,
connection: dummy_connection,
url: "http://www.example.org",
params: {})
connection: dummy_connection,
url: "http://www.example.org",
params: {})

rendered_html = renderer.render(inline: "<%= link_to post_path(id: #{post.id}) %>")

Expand All @@ -98,9 +98,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase
["get", "put", "patch", "post", "delete"].each do |http_method|
test "renderer.render handles an #{http_method} route" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: ApplicationController,
connection: dummy_connection,
url: "http://example.org/known/#{http_method}",
params: {})
connection: dummy_connection,
url: "http://example.org/known/#{http_method}",
params: {})

rendered_html = renderer.render(inline: "<%= request.url %>")
assert_equal rendered_html, "http://example.org/known/#{http_method}"
Expand All @@ -109,9 +109,9 @@ class Futurism::Resolver::Controller::RendererTest < ActiveSupport::TestCase

test "renderer.render handles an umatchable url" do
renderer = Futurism::Resolver::Controller::Renderer.for(controller: ApplicationController,
connection: dummy_connection,
url: "http://example.org/unknown/place",
params: {})
connection: dummy_connection,
url: "http://example.org/unknown/place",
params: {})

assert_nothing_raised do
renderer.render(inline: "<%= request.url %>")
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "rails/test_help"
require "minitest/mock"
require "nokogiri"
require "pry"

# Filter out the backtrace from minitest while preserving the one from other libraries.
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
Expand Down

0 comments on commit 5af940e

Please sign in to comment.