After migrating from jquery_ujs
to rails-ujs
, certain JS tests are failing and proving very difficult to debug.
The problem appears to be that Jquery objects are only initialized for the first test in a sequence. Everything works fine in my development environment.
After upgrading to Rails 5.1 everything was working with jquery_ujs
. I then made only one change in application.js
.
#added this
//= require rails-ujs
# removed this
//= require jquery_ujs
Here's an example. Two almost identical system tests for interaction with a select2 field.
it "selects recognized search strings", :js do
expect(MyRemoteSelectBuilder).to receive(:search_query).
with("recognized_string").once.
and_return(
[
id: 1,
name: "Recognized Name"
]
)
visit new_my_object_path
# triggers the select2
is_expected.to have_selector('.select2-container', visible: false)
first('.select2-container', minimum: 1).click
# do some more stuff
end
it "does not select unrecognized search strings", :js do
expect(MyRemoteSelectBuilder).to receive(:search_query).
with("unrecognized_string").once.
and_return(
[ ]
)
visit new_my_object_path
# triggers the select2
is_expected.to have_selector('.select2-container', visible: false)
first('.select2-container', minimum: 1).click
# do some more stuff
end
When run together, one of these tests will fail with
expected to find css ".select2-container" but there were no matches
If I focus and run only one or other of these tests, they will pass.
I considered whether this might be a turbolinks issue (I don't think it is as the tests pass sometimes). Nonetheless I have tried various ways to initialize Select2 with Turbolinks but without success.
(($) ->
$(document).on 'turbolinks:load', ->
InititializeSelect2.init()
return
return
) jQuery
# or
$ ->
InititializeSelect2.init()
# etc
Is there a fundamental difference between rails-ujs
or jquery_ujs
that could affect jquery initialization between tests? Have I overlooked some configuration? What would be logical steps to debug this issue?