How do I make assertions consistently wait until a turbo frame has been updated?
My system tests are experiencing a race condition leading to inconsistent test results. Sometimes the turbo frame is updated before my assertion (test passes) and sometimes afterward (test fails).
View:
<div data-controller="filter">
<%= form_with url: root_path, method: :get, data: { turbo_frame: "intakes", filter_target: "form", action: "change->filter#submit" } do %>
<%= select_tag "country", options_for_select(@countries) %>
<% end %>
<%= turbo_frame_tag "intakes" do %>
<table>
<% @family_intakes.each do |family_intake| %>
<tr>
<td><%= family_intake.full_name %></td>
</tr>
<% end %>
</table>
<% end %>
</div>
This Stimulus controller submits my form on change event (ultimately updating the turbo frame):
export default class extends Controller {
static targets = ["form"];
submit(event) {
this.formTarget.requestSubmit();
}
}
Test:
class FamilyIntakesTest < ApplicationSystemTestCase
test "Only intakes from selected country are displayed" do
login
select "Afghanistan", from: "country"
assert_selector "td", text: family_intakes(:manizha).first
assert_selector "td", text: family_intakes(:sayed).first开发者_开发知识库
refute_selector "td", text: family_intakes(:mohammad).first
end
end
Error:
Failure:
FamilyIntakesTest#test_Only_intakes_from_selected_country_are_displayed [/home/eric/<redacted>/test/system/family_intakes_test.rb:31]:
expected not to find visible css "td" with text "Mohammad", found 1 match: "Mohammad Ahmad". Also found "Manizha Ahmadi", "Sayed Shinwari", which matched the selector but not all filters.
精彩评论