Skip to content

Commit

Permalink
Cleanup...
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Feb 21, 2024
1 parent cffd1b1 commit 9f5f9a3
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 116 deletions.
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: ['3.0', '3.1', '3.2', '3.3']
ruby-version: ['3.3']

steps:
- uses: actions/checkout@v3
Expand Down
11 changes: 0 additions & 11 deletions lib/turbo_boost/commands/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,6 @@ def dom_id_selector(...)
css_id_selector dom_id(...)
end

def render_response(html:, status:)
@rendered_response = html
@rendered_status = status
end

attr_reader :rendered_response, :rendered_status

def rendered_response?
rendered_response.present?
end

# Same method signature as ActionView::Rendering#render (i.e. controller.view_context.render)
# Great for rendering partials with short-hand syntax sugar → `render "/path/to/partial"`
def render(options = {}, locals = {}, &block)
Expand Down
6 changes: 1 addition & 5 deletions lib/turbo_boost/commands/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ def update_response
end

def render_response(html: "", status: nil, status_header: nil)
if command_instance.rendered_response? && html.blank?
html = command_instance.rendered_response
status = command_instance.rendered_status
end
controller.render html: html, layout: false, status: status || response_status unless controller.performed?
controller.render html: html, layout: false, status: status || response_status # unless controller.performed?
append_to_response_headers status_header
end

Expand Down
24 changes: 20 additions & 4 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def needs_server?

Capybara.register_driver(:null) { CapybaraNullDriver.new }
Capybara.default_driver = :null
Capybara.default_max_wait_time = 12
Capybara.default_max_wait_time = 8
Capybara.default_normalize_ws = true
Capybara.save_path = "tmp/capybara"
Capybara.configure do |config|
Expand Down Expand Up @@ -46,6 +46,22 @@ def after_teardown
playwright_browser&.close
end

def with_retries(max = 3)
count = 0
while count < max
begin
count += 1
return yield
rescue Playwright::Error, Minitest::Assertion => error
puts "RETRY #{count}: #{self.class.name}##{name}#{error.message}"
sleep 1
Capybara.reset_sessions!
page.reload waitUntil: "load"
raise if count >= max
end
end
end

# Waits for a promise to resolve on the client
def wait_for_promise(delay: 0)
page.evaluate("new Promise(resolve => setTimeout(resolve, #{delay}))")
Expand All @@ -65,10 +81,10 @@ def wait_for_next_tick
# NOTE: This isn't as reliable as I had hoped
def wait_for_detach(element)
# 1. STRATEGY: Wait for detach/hidden state + next tick
# element.wait_for_element_state "hidden"
# wait_for_next_tick
element.wait_for_element_state "hidden"
wait_for_next_tick

# 2. STRATEGY: Wait for 100ms
wait_for_promise delay: 100
# wait_for_promise delay: 100
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class PreventControllerActionCommand < ApplicationCommand
prevent_controller_action

def perform
Current.template = "tests/drivers/_form.html.erb"
Current.template = "tests/drivers/_form.turbo_stream.erb"
count = state[self.class.name].to_i + 1
state[self.class.name] = count

render_response(
html: render(partial: "/tests/drivers/form", assigns: {message: "#{self.class.name.demodulize} invoked #{count} times"}),
status: :multiple_choices
streams << render(
partial: "/tests/drivers/form",
formats: [:turbo_stream],
assigns: {message: "#{self.class.name.demodulize} invoked #{count} times"}
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def perform
Current.template = "tests/drivers/frame/_turbo_stream.html.erb"
count = state[self.class.name].to_i + 1
state[self.class.name] = count

streams << render(
partial: "/tests/drivers/frame",
formats: [:turbo_stream],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ def perform
count = state[self.class.name].to_i + 1
state[self.class.name] = count

render_response(
html: render(partial: "/tests/drivers/method", assigns: {message: "#{self.class.name.demodulize} invoked #{count} times"}),
status: :multiple_choices
streams << render(
partial: "/tests/drivers/method",
formats: [:turbo_stream],
assigns: {message: "#{self.class.name.demodulize} invoked #{count} times"}
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/app/views/tests/drivers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= form_with url: tests_path, data: { turbo_command: "Drivers::Form::PreventControllerActionCommand" } do |form| %>
<button type="submit" data-test="prevent">
<article>Click to Invoke → <code>Drivers::Form::<ins><u>Prevent</u></ins>ControllerActionCommand</code></article>
<small>Uses explicit <u>Rails/Hotwire</u> rendering mechanics</small>
<small>Uses the <u>Append</u> rendering strategy</small>
</button>
<% end %>
</section>
Expand All @@ -19,7 +19,7 @@
<%= form_with url: tests_path, data: { turbo_command: "Drivers::Form::AllowControllerActionCommand" } do |form| %>
<button type="submit" data-test="allow">
<article>Click to Invoke → <code>Drivers::Form::<ins><u>Allow</u></ins>ControllerActionCommand</code></article>
<small>Uses explicit <u>Rails/Hotwire</u> rendering mechanics</small>
<small>Uses the <u>Append</u> rendering strategy</small>
</button>
<% end %>
</section>
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/app/views/tests/drivers/_form.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= turbo_stream.replace "drivers-frame" do %>
<%= render partial: "/tests/drivers/frame", formats: [:html] %>
<%= turbo_stream.replace "drivers-form" do %>
<%= render partial: "/tests/drivers/form", formats: [:html] %>
<% end %>
4 changes: 2 additions & 2 deletions test/dummy/app/views/tests/drivers/_method.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<%= link_to test_path(:method), role: "button", style: "width:100%",
data: { test: "prevent", turbo_method: "delete", turbo_command: "Drivers::Method::PreventControllerActionCommand" } do %>
<article>Click to Invoke ➜ <code>Drivers::Method::<ins><u>Prevent</u></ins>ControllerActionCommand</code></article>
<small>Uses explicit <u>Rails/Hotwire</u> rendering mechanics</small>
<small>Uses the <u>Append</u> rendering strategy</small>
<% end %>
</section>

<section>
<%= link_to test_path(:method), role: "button", style: "width:100%",
data: { test: "allow", turbo_method: "delete", turbo_command: "Drivers::Method::AllowControllerActionCommand" } do %>
<article>Click to Invoke ➜ <code>Drivers::Method::<ins><u>Allow</u></ins>ControllerActionCommand</code></article>
<small>Uses explicit <u>Rails/Hotwire</u> rendering mechanics</small>
<small>Uses the <u>Append</u> rendering strategy</small>
<% end %>
</section>

Expand Down
48 changes: 26 additions & 22 deletions test/system/basic_commands/turbo_boost_setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@

class TurboBoostSetupTest < ApplicationSystemTestCase
test "turbo boost commands loaded and configured" do
page.goto basic_command_url
with_retries do
page.goto basic_command_url

assert_equal "object", js("typeof TurboBoost")
assert_equal "object", js("typeof TurboBoost.Commands")
assert_equal "object", js("typeof TurboBoost.Commands.schema")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'change').selectors)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'click').selectors)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'submit').selectors)")
assert_equal "function", js("typeof TurboBoost.Commands.registerEventDelegate")
assert_equal "object", js("typeof TurboBoost.Commands.events")
assert_equal "object", js("typeof TurboBoost.State")
assert_equal "object", js("typeof TurboBoost.Commands.logger")
assert_equal "debug", js("TurboBoost.Commands.logger.level")
assert_equal TurboBoost::Commands::VERSION, js("TurboBoost.Commands.VERSION")
assert_equal "object", js("typeof TurboBoost")
assert_equal "object", js("typeof TurboBoost.Commands")
assert_equal "object", js("typeof TurboBoost.Commands.schema")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'change').selectors)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'click').selectors)")
assert js("Array.isArray(TurboBoost.Commands.eventDelegates.find(e => e.name === 'submit').selectors)")
assert_equal "function", js("typeof TurboBoost.Commands.registerEventDelegate")
assert_equal "object", js("typeof TurboBoost.Commands.events")
assert_equal "object", js("typeof TurboBoost.State")
assert_equal "object", js("typeof TurboBoost.Commands.logger")
assert_equal "debug", js("TurboBoost.Commands.logger.level")
assert_equal TurboBoost::Commands::VERSION, js("TurboBoost.Commands.VERSION")
end
end

test "turbo boost client state" do
page.goto basic_command_url
with_retries do
page.goto basic_command_url

assert js("TurboBoost.State.signed.length > 0")
assert_not_nil js("TurboBoost.State.current.command_token") # used for forgery protection
assert js("TurboBoost.State.signed.length > 0")
assert_not_nil js("TurboBoost.State.current.command_token") # used for forgery protection

js("TurboBoost.State.current.test = true")
js("TurboBoost.State.current.example = 'value'")
assert js("TurboBoost.State.current.test")
assert_equal "value", js("TurboBoost.State.current.example")
assert_equal "value", js("TurboBoost.State.changed.example")
js("TurboBoost.State.current.test = true")
js("TurboBoost.State.current.example = 'value'")
assert js("TurboBoost.State.current.test")
assert_equal "value", js("TurboBoost.State.current.example")
assert_equal "value", js("TurboBoost.State.changed.example")
end
end
end
34 changes: 19 additions & 15 deletions test/system/tests/drivers/form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ def setup
end

def test_prevent_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end

def test_allow_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end

private

def details_element
page.wait_for_selector "#drivers-form"
page.wait_for_selector "#drivers-form details"
end

def message_element
Expand Down
34 changes: 19 additions & 15 deletions test/system/tests/drivers/frame_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ def setup
end

def test_prevent_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end

def test_allow_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end

private

def details_element
page.wait_for_selector "#drivers-frame"
page.wait_for_selector "#drivers-frame details"
end

def message_element
Expand Down
34 changes: 19 additions & 15 deletions test/system/tests/drivers/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ def setup
end

def test_prevent_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
prevent_element.click
wait_for_detach prevent_element
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "PreventControllerActionCommand invoked #{count} times", message_element.inner_text
end

def test_allow_controller_action_command
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
with_retries do
count = 10
details_element.click
assert_equal "...", message_element.inner_text
count.times do
allow_element.click
wait_for_detach allow_element
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end
assert_equal "AllowControllerActionCommand invoked #{count} times", message_element.inner_text
end

private

def details_element
page.wait_for_selector "#drivers-method"
page.wait_for_selector "#drivers-method details"
end

def message_element
Expand Down
Loading

0 comments on commit 9f5f9a3

Please sign in to comment.