-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean up helper tests * Pull out test helpers * Split up tests
- Loading branch information
1 parent
0fa9a7f
commit 57dea45
Showing
23 changed files
with
1,466 additions
and
1,434 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,4 @@ | ||
class ApplicationViewTestCase < ActionView::TestCase | ||
include ConfigurationHelper | ||
|
||
# `#assert_attrs` expects attrs to appear in the order they are passed. | ||
def assert_attrs(tag, tag_name: :input, **attrs) | ||
assert_match(/<#{tag_name}.* #{escaped_attrs(attrs)}/, tag) | ||
end | ||
|
||
def assert_no_attrs(tag, tag_name: :input, **attrs) | ||
assert_no_match(/<#{tag_name}.* #{escaped_attrs(attrs)}/, tag) | ||
end | ||
|
||
private | ||
def escaped_attrs(attrs) | ||
attrs.map do |k, v| | ||
%Q(#{escape_specials(k)}="#{escape_specials(v)}".*) | ||
end.join(" ").gsub("&", "&") | ||
end | ||
|
||
def escape_specials(value) | ||
special_characters = Regexp.union "[]?".chars | ||
value.to_s.gsub(special_characters) { |char| "\\#{char}" } | ||
end | ||
include AttributeAssertionsHelper | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module AttributeAssertionsHelper | ||
# `#assert_attrs` expects attrs to appear in the order they are passed. | ||
def assert_attrs(tag, tag_name: :input, **attrs) | ||
assert_match(/<#{tag_name}.* #{escaped_attrs(attrs)}/, tag) | ||
end | ||
|
||
def assert_no_attrs(tag, tag_name: :input, **attrs) | ||
assert_no_match(/<#{tag_name}.* #{escaped_attrs(attrs)}/, tag) | ||
end | ||
|
||
private | ||
def escaped_attrs(attrs) | ||
attrs.map do |k, v| | ||
%Q(#{escape_specials(k)}="#{escape_specials(v)}".*) | ||
end.join(" ").gsub("&", "&") | ||
end | ||
|
||
def escape_specials(value) | ||
special_characters = Regexp.union "[]?".chars | ||
value.to_s.gsub(special_characters) { |char| "\\#{char}" } | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module ComboboxActionsHelper | ||
def open_combobox(selector) | ||
find(selector).click | ||
end | ||
|
||
def type_in_combobox(selector, *text) | ||
find(selector).send_keys(*text) | ||
end | ||
|
||
def delete_from_combobox(selector, text, original:) | ||
find(selector).then do |input| | ||
original.chars.each { input.send_keys(:arrow_right) } | ||
text.chars.each { input.send_keys(:backspace) } | ||
end | ||
end | ||
|
||
def click_on_option(text) | ||
find("li[role=option]", exact_text: text).click | ||
end | ||
|
||
def clear_autocompleted_portion(selector) | ||
type_in_combobox selector, :backspace | ||
end | ||
|
||
def remove_chip(text) | ||
find("[aria-label='Remove #{text}']").click | ||
end | ||
|
||
def on_small_screen | ||
@on_small_screen = true | ||
original_size = page.current_window.size | ||
page.current_window.resize_to 375, 667 | ||
yield | ||
ensure | ||
@on_small_screen = false | ||
page.current_window.resize_to(*original_size) | ||
end | ||
|
||
def on_slow_device(delay:) | ||
@on_slow_device = true | ||
page.execute_script "window.HOTWIRE_COMBOBOX_STREAM_DELAY = #{delay * 1000}" | ||
yield | ||
ensure | ||
@on_slow_device = false | ||
page.execute_script "window.HOTWIRE_COMBOBOX_STREAM_DELAY = 0" | ||
end | ||
|
||
def tab_away | ||
find("body").send_keys(:tab, :tab) | ||
assert_closed_combobox | ||
end | ||
|
||
def click_away | ||
if @on_small_screen | ||
click_on_top_left_corner | ||
else | ||
find("#clickable").click | ||
end | ||
|
||
assert_closed_combobox | ||
end | ||
|
||
def click_on_top_left_corner | ||
page.execute_script "document.elementFromPoint(0, 0).click()" | ||
end | ||
|
||
def current_selection_contents | ||
page.evaluate_script "document.getSelection().toString()" | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module ComboboxAssertionsHelper | ||
def assert_combobox | ||
assert_selector "input[role=combobox]" | ||
end | ||
|
||
def assert_closed_combobox | ||
assert_selector "input[aria-expanded=false]" | ||
end | ||
|
||
def assert_open_combobox | ||
assert_selector "input[aria-expanded=true]" | ||
end | ||
|
||
def assert_no_listbox | ||
assert_no_selector "ul[role=listbox]" | ||
end | ||
|
||
def assert_listbox_with(**kwargs) | ||
assert_selector "ul[role=listbox]", **kwargs | ||
end | ||
alias_method :assert_listbox, :assert_listbox_with | ||
|
||
def assert_no_visible_options_with(**kwargs) | ||
assert_no_selector "li[role=option]", **kwargs | ||
end | ||
alias_method :assert_no_visible_options, :assert_no_visible_options_with | ||
|
||
def assert_option_with(html_markup: "", **kwargs) | ||
assert_selector "li[role=option]#{kwargs.delete(:class)} #{html_markup}".squish, **kwargs | ||
end | ||
alias_method :assert_options_with, :assert_option_with | ||
|
||
def assert_chip_with(html_markup: "", **kwargs) | ||
assert_selector "[data-hw-combobox-chip] #{html_markup}".squish, **kwargs | ||
end | ||
|
||
def assert_combobox_display(selector, text) | ||
if text.is_a? Array | ||
assert_selection_chips(*text) | ||
else | ||
assert_field locator_for(selector), with: text | ||
end | ||
end | ||
|
||
def assert_combobox_value(selector, value) | ||
value = value.join(",") if value.is_a? Array | ||
assert_field "#{locator_for(selector)}-hw-hidden-field", type: "hidden", with: value | ||
end | ||
|
||
def assert_combobox_display_and_value(selector, text, value) | ||
assert_combobox_display selector, text | ||
assert_combobox_value selector, value | ||
end | ||
|
||
def assert_selected_option_with(selector: "", **kwargs) | ||
assert_selector "li[role=option][aria-selected=true]#{selector}".squish, **kwargs | ||
end | ||
|
||
def assert_no_visible_selected_option | ||
assert_no_selector "li[role=option][aria-selected=true]" | ||
end | ||
|
||
def assert_invalid_combobox | ||
assert_selector "input[aria-invalid=true]" | ||
assert_selector "dialog input[aria-invalid=true]", visible: :hidden | ||
end | ||
|
||
def assert_not_invalid_combobox | ||
assert_no_selector "input[aria-invalid=true]" | ||
assert_no_selector "dialog input[aria-invalid=true]", visible: :hidden | ||
end | ||
|
||
def assert_proper_combobox_name_choice(original:, new:, proper:) | ||
if proper == :original | ||
assert_selector "input[name='#{original}']", visible: :hidden | ||
assert_no_selector "input[name='#{new}']", visible: :hidden | ||
else | ||
assert_no_selector "input[name='#{original}']", visible: :hidden | ||
assert_selector "input[name='#{new}']", visible: :hidden | ||
end | ||
end | ||
|
||
def assert_selection_chips(*texts) | ||
texts.each do |text| | ||
assert_selector "[data-hw-combobox-chip]", text: text | ||
end | ||
end | ||
|
||
def assert_focused_combobox(selector) | ||
page.evaluate_script("document.activeElement.id") == locator_for(selector) | ||
end | ||
|
||
def assert_group_with(**kwargs) | ||
assert_selector "ul[role=group] li[role=presentation]", **kwargs | ||
end | ||
|
||
def assert_scrolled(selector) | ||
sleep 0.5 | ||
assert page.evaluate_script("document.querySelector('#{selector}').scrollTop") > 0, | ||
"Expected #{selector} to be scrolled." | ||
end | ||
|
||
def locator_for(selector) | ||
# https://rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Matchers#has_field%3F-instance_method | ||
selector.delete_prefix("#") | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require "system_test_helper" | ||
|
||
class AsyncTest < ApplicationSystemTestCase | ||
[ | ||
{ path: :async_path, visible_options: 10 }, | ||
{ path: :async_html_path, visible_options: 5 } | ||
].each do |test_case| | ||
test "async combobox #{test_case[:path]}" do | ||
visit send(test_case[:path]) | ||
|
||
open_combobox "#movie-field" | ||
|
||
assert_text "12 Angry Men" | ||
type_in_combobox "#movie-field", "wh" | ||
assert_combobox_display_and_value "#movie-field", "Whiplash", movies(:whiplash).id | ||
assert_options_with count: 2 | ||
clear_autocompleted_portion "#movie-field" | ||
delete_from_combobox "#movie-field", "wh", original: "wh" | ||
assert_combobox_display_and_value "#movie-field", "", nil | ||
assert_text "12 Angry Men" | ||
|
||
# pagination | ||
assert_options_with count: test_case[:visible_options] | ||
find("#movie-field-hw-listbox").scroll_to :bottom | ||
assert_options_with count: test_case[:visible_options] + 5 | ||
|
||
type_in_combobox "#movie-field", "a" | ||
assert_combobox_display_and_value "#movie-field", "A Beautiful Mind", movies(:a_beautiful_mind).id | ||
find("#movie-field-hw-listbox").scroll_to :bottom | ||
assert_options_with count: test_case[:visible_options] + 5 | ||
assert_scrolled "#movie-field-hw-listbox" | ||
end | ||
end | ||
|
||
test "async autocomplete selections don't trample over each other" do | ||
visit async_path | ||
|
||
on_slow_device delay: 0.5 do | ||
open_combobox "#movie-field" | ||
type_in_combobox "#movie-field", "a" | ||
sleep 0.3 # less than the delay, more than the debounce | ||
type_in_combobox "#movie-field", "l" | ||
sleep 0.7 # more than the delay | ||
|
||
assert_equal "addin", current_selection_contents | ||
end | ||
end | ||
|
||
test "substring matching in async free-text combobox" do | ||
visit freetext_async_path | ||
|
||
open_combobox "#movie-field" | ||
type_in_combobox "#movie-field", "few" | ||
click_on_option "A Few Good Men" | ||
assert_combobox_display_and_value "#movie-field", "A Few Good Men", movies(:a_few_good_men).id | ||
end | ||
end |
Oops, something went wrong.