Jonathan Bennett

Waiting on Turbo the right way

Quick tip for waiting on <form> submission when using Turbo:

class SomethingTest < ApplicationSystemTestCase
	test "something works" do
		visit something_path
		# this might automatically submit
		fill_in "Name", with: "Jonathan"
		
		sleep 10 # pure magic!
		
		assert_selector "h1", text: "Jonathan"
	end
end

Okay, sure. You could toss a random sleep 10 into your tests and go get coffee. But don’t.

The Right Way

Turbo adds aria-busy="true" to forms while they’re submitting. You can use this to wait for the submission to start and finish:

class SomethingTest < ApplicationSystemTestCase
	test "something works" do
		visit something_path
		# this might automatically submit
		fill_in "Name", with: "Jonathan"
		
		assert_selector "form[aria-busy=true]" # start submission
		refute_selector "form[aria-busy=true]" # end submission
		
		assert_selector "h1", text: "Jonathan"
	end
end

That’s it. Clean. Reliable. No black magic.