We’re writing some Rails system test (capybara) and one of the problems we’re coming across is intermittent test failures when there’s dynamic HTML injected.
For example, let’s say we fetch the following HTML and inject it into the DOM:
<div data-controller="foo"> <button data-action="foo.perform">Go</button> </div>
The moment it’s injected, the “foo” Stimulus controller gets attached via MutationObserver callback. However, during system tests, there a possibility that
click_button("Go") is performed before the controller’s actions are even registered. This results in the click not performing the action
So, is there a way to know when Stimulus is finished working its magic for newly injected content?