debian-mirror-gitlab/spec/support/helpers/select2_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2019-03-02 22:35:43 +05:30
require_relative 'wait_for_requests'
2014-09-02 18:07:02 +05:30
# Select2 ajax programmatic helper
# It allows you to select value from select2
#
# Params
# value - real value of selected item
# opts - options containing css selector
#
# Usage:
#
# select2(2, from: '#user_ids')
#
module Select2Helper
2019-03-02 22:35:43 +05:30
include WaitForRequests
2016-09-13 17:45:13 +05:30
def select2(value, options = {})
2017-08-17 22:00:37 +05:30
raise ArgumentError, 'options must be a Hash' unless options.is_a?(Hash)
2014-09-02 18:07:02 +05:30
2019-03-02 22:35:43 +05:30
wait_for_requests unless options[:async]
2015-09-11 14:41:01 +05:30
selector = options.fetch(:from)
2014-09-02 18:07:02 +05:30
2019-12-21 20:55:43 +05:30
ensure_select2_loaded(selector)
2018-03-17 18:26:18 +05:30
2014-09-02 18:07:02 +05:30
if options[:multiple]
2017-08-17 22:00:37 +05:30
execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
2014-09-02 18:07:02 +05:30
else
2017-08-17 22:00:37 +05:30
execute_script("$('#{selector}').select2('val', '#{value}').trigger('change');")
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
def open_select2(selector)
2019-12-21 20:55:43 +05:30
ensure_select2_loaded(selector)
2017-08-17 22:00:37 +05:30
execute_script("$('#{selector}').select2('open');")
end
2019-07-31 22:56:46 +05:30
def close_select2(selector)
2019-12-21 20:55:43 +05:30
ensure_select2_loaded(selector)
2019-07-31 22:56:46 +05:30
execute_script("$('#{selector}').select2('close');")
end
2017-08-17 22:00:37 +05:30
def scroll_select2_to_bottom(selector)
evaluate_script "$('#{selector}').scrollTop($('#{selector}')[0].scrollHeight); $('#{selector}');"
end
2019-12-21 20:55:43 +05:30
private
def ensure_select2_loaded(selector)
first(selector, visible: :all).sibling('.select2-container')
end
2014-09-02 18:07:02 +05:30
end