Skip to content

Commit e21827f

Browse files
committed
Fix Tom Select timing issues in meeting_spec.rb
The meeting edit page wasn't initializing Tom Select automatically. Added a helper method to manually initialize TomSelect if it hasn't been initialized within 2 seconds of page load. Also fixed: - Longer wait times for TomSelect initialization (10s) - JavaScript click for dropdown options to avoid element interception - Explicit waits for items to be present before trying to remove them
1 parent fd1a03b commit e21827f

1 file changed

Lines changed: 52 additions & 3 deletions

File tree

spec/support/select_from_tom_select.rb

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4089
end
4190

4291
RSpec.configure do |config|

0 commit comments

Comments
 (0)