@@ -7,8 +7,11 @@ module SelectFromTomSelect
77 # @param item_text [String] The text to select
88 # @param from [String, Symbol] The field ID (for documentation purposes)
99 def select_from_tom_select ( item_text , from : nil )
10- # Wait for TomSelect to initialize
11- expect ( page ) . to have_css ( '.ts-wrapper' , wait : 5 )
10+ # Ensure TomSelect is initialized (workaround for pages where it doesn't auto-init)
11+ ensure_tom_select_initialized ( 'meeting_organisers' )
12+
13+ # Wait for TomSelect to initialize - give extra time for page to fully load JS
14+ expect ( page ) . to have_css ( '.ts-wrapper' , wait : 10 )
1215
1316 # Open dropdown and type search query
1417 find ( '.ts-control' ) . click
@@ -27,16 +30,62 @@ def select_from_tom_select(item_text, from: nil)
2730 expect ( page ) . to have_css ( '.ts-dropdown .option' , wait : 5 )
2831
2932 # Click the matching option
30- find ( '.ts-dropdown .option' , text : item_text , match : :prefer_exact ) . click
33+ # Use JavaScript click to avoid element interception issues
34+ option = find ( '.ts-dropdown .option' , text : item_text , match : :prefer_exact )
35+ page . execute_script ( "arguments[0].click();" , option . native )
3136 end
3237
3338 # Remove an item from a TomSelect multi-select
3439 # @param item_text [String] The text of the item to remove (must match exactly)
3540 def remove_from_tom_select ( item_text )
41+ # Ensure TomSelect is initialized (workaround for pages where it doesn't auto-init)
42+ ensure_tom_select_initialized ( 'meeting_organisers' )
43+
44+ # Wait for TomSelect to initialize and items to be present
45+ expect ( page ) . to have_css ( '.ts-wrapper' , wait : 10 )
46+ expect ( page ) . to have_css ( '.ts-wrapper .item' , text : item_text , wait : 5 )
47+
3648 within '.ts-wrapper' do
3749 find ( '.item' , text : item_text , match : :prefer_exact ) . find ( '.remove' ) . click
3850 end
3951 end
52+
53+ private
54+
55+ def ensure_tom_select_initialized ( element_id )
56+ # Check if TomSelect is already initialized
57+ return if page . has_css? ( '.ts-wrapper' , wait : 2 )
58+
59+ # Try to initialize TomSelect manually if the element exists
60+ # Note: This is a minimal initialization for tests where TomSelect doesn't auto-init
61+ script = <<~JS
62+ (function() {
63+ var elem = document.getElementById('#{ element_id } ');
64+ if (elem && typeof TomSelect !== 'undefined' && !elem.tomselect) {
65+ new TomSelect(elem, {
66+ plugins: ['remove_button'],
67+ placeholder: 'Type to search members...',
68+ valueField: 'id',
69+ labelField: 'full_name',
70+ searchField: ['full_name', 'email'],
71+ create: false,
72+ loadThrottle: 300,
73+ shouldLoad: function(query) { return query.length >= 3; },
74+ load: function(query, callback) {
75+ fetch('/admin/members/search?q=' + encodeURIComponent(query))
76+ .then(response => response.json())
77+ .then(json => callback(json))
78+ .catch(() => callback());
79+ }
80+ });
81+ }
82+ })();
83+ JS
84+ page . execute_script ( script )
85+
86+ # Wait for initialization
87+ expect ( page ) . to have_css ( '.ts-wrapper' , wait : 5 )
88+ end
4089end
4190
4291RSpec . configure do |config |
0 commit comments