haya_select_helpers 0.0.6 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a2a0808c53f9adebdb42fa32100fbb8152a3b701fbca486cb3d116849306f1e
4
- data.tar.gz: 2e0cd08fc49c9105a0c84d8469e6dd1f5eea928261b3bed299d8bb3848f6d70b
3
+ metadata.gz: 94027197c1829e876fbe519781f094cfcb7db0db47d8eba3c4f851adf1f66017
4
+ data.tar.gz: 211c7411bd07813f3ca1d0cac83f5c3ed1e42c367ec6e1560223b36153ebd783
5
5
  SHA512:
6
- metadata.gz: '08e3d969f7b4a1c460c43c152a9d28f9457bc494cae12d6a966dfe6a36db48494f5b3315d1fb2b39d2fcbb37b2b68163c5cb1d9d7ae50efdaca06a061d4f24a8'
7
- data.tar.gz: 5350a18c9c7eeb346ef151f2002583be8ce5f680b76b0c11854398bce565995886f73b8f573622d9bb16c88895cda5538cbfd34607800344d32fe4e971058fcc
6
+ metadata.gz: 38030c78ac06ea02e773157fefe7f379b780bd20bc471f7fc425a956d8f0a2a8fd8ee981c2637b3824e05979db3279d3f146a47747cefbf36c6b3f9243415268
7
+ data.tar.gz: aa42a5467142a1cd4f893ce1d5f3498240f8082c576fb045e928fc00e9734cd3528894bf4d8734e349416682fc62088867c944a76b5b8eadce1ab371ee116b6e
data/lib/haya_select.rb CHANGED
@@ -1,7 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/ClassLength, Style/Documentation
1
4
  class HayaSelect
2
- attr_reader :base_selector, :not_opened_current_selected_selector, :opened_current_selected_selector, :options_selector, :scope
5
+ attr_reader :base_selector,
6
+ :not_opened_current_selected_selector,
7
+ :opened_current_selected_selector,
8
+ :options_selector,
9
+ :scope
3
10
 
4
- delegate :all, :expect, :eq, :pretty_html, :wait_for_and_find, :wait_for_expect, :wait_for_no_selector, :wait_for_selector, to: :scope
11
+ delegate :all,
12
+ :expect,
13
+ :eq,
14
+ :pretty_html,
15
+ :wait_for_and_find,
16
+ :wait_for_browser,
17
+ :wait_for_expect,
18
+ :wait_for_no_selector,
19
+ :wait_for_selector,
20
+ to: :scope
5
21
 
6
22
  def initialize(id:, scope:)
7
23
  @base_selector = "[data-component='haya-select'][data-id='#{id}']"
@@ -38,7 +54,7 @@ class HayaSelect
38
54
  option_elements.map do |option_element|
39
55
  {
40
56
  label: option_element.text,
41
- value: option_element["data-value"]
57
+ value: option_element['data-value']
42
58
  }
43
59
  end
44
60
  rescue Selenium::WebDriver::Error::StaleElementReferenceError
@@ -62,21 +78,19 @@ class HayaSelect
62
78
  def select(label = nil, value: nil)
63
79
  open
64
80
  select_option(label:, value:)
65
- wait_for_selector not_opened_current_selected_selector
66
- wait_for_no_selector options_selector
81
+ wait_for_selected_value_or_label(label, value)
82
+ close_if_open
67
83
  self
68
84
  end
69
85
 
70
86
  def select_option(label: nil, value: nil)
71
87
  raise "No 'label' or 'value' given" if label.nil? && value.nil?
72
88
 
73
- selector = "#{options_selector} [data-testid='option-presentation']"
74
- selector << "[data-text='#{label}']" unless label.nil?
75
- selector << "[data-value='#{value}']" unless value.nil?
76
-
89
+ selector = select_option_selector(label: label, value: value)
90
+ wait_for_option(selector, label)
77
91
  option = wait_for_and_find(selector)
78
92
 
79
- raise "The '#{label}'-option is disabled" if option["data-disabled"] == "true"
93
+ raise "The '#{label}'-option is disabled" if option['data-disabled'] == 'true'
80
94
 
81
95
  option.click
82
96
  self
@@ -91,16 +105,19 @@ class HayaSelect
91
105
  end
92
106
 
93
107
  def wait_for_label(expected_label)
94
- wait_for_selector "#{base_selector} [data-class='current-selected'] [data-class='current-option']", exact_text: expected_label
108
+ wait_for_selector(
109
+ "#{base_selector} [data-class='current-selected'] [data-class='current-option']",
110
+ exact_text: expected_label
111
+ )
95
112
  self
96
113
  end
97
114
 
98
115
  def toggles
99
116
  all("#{base_selector} [data-testid='option-presentation']").map do |element|
100
117
  {
101
- toggle_icon: element["data-toggle-icon"],
102
- toggle_value: element["data-toggle-value"],
103
- value: element["data-value"]
118
+ toggle_icon: element['data-toggle-icon'],
119
+ toggle_value: element['data-toggle-value'],
120
+ value: element['data-value']
104
121
  }
105
122
  end
106
123
  rescue Selenium::WebDriver::Error::StaleElementReferenceError
@@ -108,7 +125,9 @@ class HayaSelect
108
125
  end
109
126
 
110
127
  def selected_option_values
111
- all("[data-class='select-option'][data-selected='true']").map { |select_option_element| select_option_element["data-value"] }
128
+ all("[data-class='select-option'][data-selected='true']").map do |select_option_element|
129
+ select_option_element['data-value']
130
+ end
112
131
  end
113
132
 
114
133
  def wait_for_selected_option_values(values)
@@ -125,7 +144,118 @@ class HayaSelect
125
144
  end
126
145
 
127
146
  def wait_for_value(expected_value)
128
- wait_for_selector "#{base_selector} [data-class='current-selected'] input[type='hidden'][value='#{expected_value}']", visible: false
147
+ wait_for_selector(
148
+ "#{base_selector} [data-class='current-selected'] input[type='hidden'][value='#{expected_value}']",
149
+ visible: false
150
+ )
129
151
  self
130
152
  end
153
+
154
+ private
155
+
156
+ def select_option_selector(label:, value:)
157
+ selector = "#{options_selector} [data-testid='option-presentation']"
158
+ selector << "[data-text='#{label}']" unless label.nil?
159
+ selector << "[data-value='#{value}']" unless value.nil?
160
+ selector
161
+ end
162
+
163
+ def wait_for_option(selector, label)
164
+ return wait_for_browser { scope.page.has_selector?(selector) } unless label
165
+
166
+ option_found = false
167
+
168
+ search_terms_for(label).each do |search_term|
169
+ current_options_text = options_container_text
170
+ search_for_option(search_term)
171
+
172
+ wait_for_browser do
173
+ scope.page.has_selector?(selector) || options_container_updated?(search_term, current_options_text)
174
+ end
175
+
176
+ if scope.page.has_selector?(selector)
177
+ option_found = true
178
+ break
179
+ end
180
+ end
181
+
182
+ return if option_found
183
+
184
+ wait_for_browser do
185
+ scope.page.has_selector?(selector)
186
+ end
187
+ end
188
+
189
+ def wait_for_selected_value_or_label(label, value)
190
+ if value
191
+ wait_for_value(value)
192
+ elsif label
193
+ wait_for_label(label)
194
+ end
195
+ end
196
+
197
+ def search_for_option(label)
198
+ return unless scope.page.has_selector?(search_input_selector)
199
+
200
+ search(label)
201
+ end
202
+
203
+ def options_container_updated?(search_term, previous_text)
204
+ return false unless scope.page.has_selector?(no_options_selector)
205
+ return false unless search_input_value == search_term
206
+ return false if previous_text.nil?
207
+
208
+ options_container_text != previous_text
209
+ end
210
+
211
+ def options_container_text
212
+ scope.page.find(options_selector).text
213
+ rescue Capybara::ElementNotFound
214
+ nil
215
+ end
216
+
217
+ def search_input_value
218
+ scope.page.find(search_input_selector).value
219
+ rescue Capybara::ElementNotFound
220
+ nil
221
+ end
222
+
223
+ def search_terms_for(label)
224
+ terms = [label]
225
+ terms << label.split(" (", 2).first if label.include?(" (")
226
+ terms.uniq
227
+ end
228
+
229
+ def close_if_open
230
+ return if scope.page.has_no_selector?(options_selector)
231
+
232
+ close_attempts = 0
233
+
234
+ while scope.page.has_selector?(options_selector) && close_attempts < 3
235
+ if scope.page.has_selector?(select_container_selector)
236
+ wait_for_and_find(select_container_selector).click
237
+ else
238
+ wait_for_and_find("body").click
239
+ end
240
+
241
+ close_attempts += 1
242
+ end
243
+
244
+ wait_for_no_selector options_selector
245
+ wait_for_selector not_opened_current_selected_selector
246
+ end
247
+
248
+ def search_input_selector
249
+ "#{base_selector} [data-class='search-text-input']"
250
+ end
251
+
252
+ def no_options_selector
253
+ "#{options_selector} [data-class='no-options-container']"
254
+ end
255
+
256
+ def select_container_selector
257
+ "#{base_selector} [data-class='select-container']"
258
+ end
259
+
260
+ # rubocop:enable Metrics/ClassLength, Style/Documentation
131
261
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HayaSelectHelpers
2
- VERSION = "0.0.6"
4
+ VERSION = '0.0.9'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haya_select_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-01-01 00:00:00.000000000 Z
11
+ date: 2026-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -26,7 +26,7 @@ dependencies:
26
26
  version: 7.0.3
27
27
  description: RSpec helpers for HayaSelect.
28
28
  email:
29
- - k@spernj.org
29
+ - kasper@diestoeckels.de
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []