Skip to content

Commit

Permalink
Add tests to check for logs and alerts on abort and error
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Mar 7, 2024
1 parent ba32c86 commit c2a6f74
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
32 changes: 32 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,36 @@ def assert_console_messages(min: 1, type: nil, pattern: nil, timeout: 5.seconds,
end
assert messages.size >= min, "Expected at least #{min} console message(s), but got #{messages.size}"
end

def assert_alert(match: nil, timeout: 5.seconds, interval: 0.1)
message = nil

# suppress invalid playwright warnings about unexpected dialogs
suppress_stdout do
page.on "dialog", ->(dialog) {
message = dialog.message
dialog.accept
}
yield if block_given?
begin
Timeout.timeout timeout.to_i do
sleep interval.to_f until message
end
rescue Timeout::Error
sleep 0.5 # last ditch effort to avoid flakiness
end
end

match ?
assert_match(match, message, "Expected alert dialog to match #{match.inspect}") :
assert(message, "Expected alert dialog to be shown")
end

def suppress_stdout
stdout = $stdout
$stdout = StringIO.new
yield
ensure
$stdout = stdout
end
end
4 changes: 2 additions & 2 deletions test/dummy/app/views/tests/drivers/_window.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
</section>

<section>
<button type="submit" data-testid="window_driver_allow_abort" data-turbo-command="Drivers::Window::AllowControllerActionCommand#perform_with_abort">
<button type="submit" data-testid="window_driver_allow_with_abort" data-turbo-command="Drivers::Window::AllowControllerActionCommand#perform_with_abort">
<article>Click to Invoke ➜ <code>Drivers::Window::<ins><u>Allow</u></ins>ControllerActionCommand#perform_with_abort</code></article>
</button>
</section>

<section>
<button type="submit" data-testid="window_driver_abort" data-turbo-command="Drivers::Window::AllowControllerActionCommand#perform_with_error">
<button type="submit" data-testid="window_driver_allow_with_error" data-turbo-command="Drivers::Window::AllowControllerActionCommand#perform_with_error">
<article>Click to Invoke ➜ <code>Drivers::Window::<ins><u>Allow</u></ins>ControllerActionCommand#perform_with_error</code></article>
</button>
</section>
Expand Down
22 changes: 14 additions & 8 deletions test/system/tests/drivers/window_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ class DriversWindowTest < ApplicationSystemTestCase
TurboBoost::Commands.config.alert_on_abort = true
page.goto tests_url
element(:window_driver).click

alerted = false
page.on "dialog", ->(dialog) { alerted = true }

assert_console_messages min: 1, type: "warning", pattern: "turbo-boost:command:.*abort" do
element(:window_driver_allow_abort).click
assert_alert match: /HTTP 285 Abort/i do
assert_console_messages min: 1, type: "warning", pattern: "turbo-boost:command:.*abort" do
element(:window_driver_allow_with_abort).click
end
end
end

sleep 0.1 # wait for alert to be shown
assert alerted, "Expected alert dialog to be shown"
test "command that ALLOWS the rails controller action to perform handles error" do
TurboBoost::Commands.config.alert_on_error = true
page.goto tests_url
element(:window_driver).click
assert_alert match: /HTTP 500 Internal Server Error/i do
assert_console_messages min: 1, type: "error", pattern: "turbo-boost:command:.*error" do
element(:window_driver_allow_with_error).click
end
end
end
end

0 comments on commit c2a6f74

Please sign in to comment.