Created
February 7, 2024 14:54
-
-
Save Rodrigora/21082ee84d448a2fc245536d2e773c64 to your computer and use it in GitHub Desktop.
Stimulus/Capybara helper to ensure that the stimulus application loaded all controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module StimulusHelper | |
CouldNotLoadStimulus = Class.new(StandardError) do | |
def initialize(missing_controllers) | |
super("Could not load all controllers. missing: #{missing_controllers}") | |
end | |
end | |
# Reads the list of controllers from the filesystem | |
APPLICATION_CONTROLLERS = Dir[Rails.root.join('app/javascript/controllers/*')].map do |path| | |
File.basename(path).gsub('_controller.js', '').gsub('_', '-') | |
end - ['application.js', 'index.js'] | |
def ensure_stimulus_is_ready | |
tries = 0 | |
begin | |
missing_controllers = APPLICATION_CONTROLLERS - loaded_controllers | |
raise CouldNotLoadStimulus, missing_controllers if missing_controllers.any? | |
rescue CouldNotLoadStimulus => e | |
tries += 1 | |
raise e if tries > 3 | |
sleep 0.2 | |
retry | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment