Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Wait for the scripts and the page change in the mobile ticket spec

Guido Boyke038a5049c396
diff --git a/spec/support/stimulus_helpers.rb b/spec/support/stimulus_helpers.rb
new file mode 100644
index 0000000..daf6695
--- /dev/null
+++ b/spec/support/stimulus_helpers.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# A page shows before its scripts listen. Stimulus loads the controllers as
+# modules after the HTML (controllers/index.js, eagerLoadControllersFrom), so
+# a click right after a visit can land on a button whose controller is not
+# connected yet and do nothing. On a cold browser (the first example of a run)
+# that window is long enough to fail most runs.
+module StimulusHelpers
+ # Waits until the controller `identifier` is connected on the first element
+ # matching `selector`.
+ def wait_for_stimulus(selector, identifier)
+ script = <<~JS
+ (() => {
+ const element = document.querySelector(#{selector.to_json})
+ return Boolean(element && window.Stimulus &&
+ window.Stimulus.getControllerForElementAndIdentifier(element, #{identifier.to_json}))
+ })()
+ JS
+ page.document.synchronize do
+ raise Capybara::ExpectationNotMet, "#{identifier} is not connected yet" unless page.evaluate_script(script)
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.include StimulusHelpers, type: :system
+end
diff --git a/spec/system/mobile_ticket_navigation_spec.rb b/spec/system/mobile_ticket_navigation_spec.rb
index 6ae617c..245ee97 100644
--- a/spec/system/mobile_ticket_navigation_spec.rb
+++ b/spec/system/mobile_ticket_navigation_spec.rb
@@ -17,6 +17,8 @@ RSpec.describe 'Mobile ticket navigation', type: :system do
page.driver.browser.execute_cdp('Emulation.setDeviceMetricsOverride', width: 390, height: 844, deviceScaleFactor: 1, mobile: true)
visit organisation_project_tickets_path(organisation, project, viewport: 'mobile')
expect(page).to have_css('.mobile-ticket-title', count: 2)
+ # The list is server-rendered; its buttons do nothing until mobile-shell listens.
+ wait_for_stimulus('.mobile-page-head', 'mobile-shell')
end
after do
@@ -82,8 +84,16 @@ RSpec.describe 'Mobile ticket navigation', type: :system do
search: 'Navigation', search_mode: 'title_only',
filter: { tag: [tag.name] }, sort: { column: 'title', direction: 'desc' })
expect(page).to have_css('.mobile-ticket-title', count: 1, text: 'Navigation B')
+ # The list frame advances (data-turbo-action): Turbo writes the address, renders
+ # page 2, and only then starts a visit of its own for the snapshot, which cancels
+ # any visit already running. A click on a row between the render and that visit
+ # is lost. The page change is done when that visit ends with turbo:load.
+ page.execute_script("window.__pageLoaded = false; document.addEventListener('turbo:load', () => { window.__pageLoaded = true }, { once: true })")
within('.mobile-pagination') { click_link I18n.t('mobile.next') }
expect(page).to have_css('.mobile-ticket-title', count: 1, text: 'Navigation A')
+ page.document.synchronize do
+ raise Capybara::ExpectationNotMet, 'the page change is not done yet' unless page.evaluate_script('window.__pageLoaded')
+ end
click_link 'Navigation A'
expect(page).to have_css('.mobile-ticket-detail')
click_link I18n.t('mobile.back_tickets')