Skip to content

Commit

Permalink
Focus editor after calling fill_in_rich_text_area
Browse files Browse the repository at this point in the history
When System Tests call `fill_in_rich_text_area`, they interact with
`<trix-editor>` elements by changing the contents programmatically.

This is unlike how end-users will interact with the element. Overhauling
the test helper to more accurately reflect Real World usage would
require a sizable effort.

With that being said, leaving the `<trix-editor>` with focus after
populating its contents is a minor change that makes it a more genuine
recreation.
  • Loading branch information
seanpdoyle committed Dec 9, 2022
1 parent 59fe981 commit 67a4ac6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actiontext/CHANGELOG.md
@@ -1,3 +1,7 @@
* Focus rich-text editor after calling `fill_in_rich_text_area`

*Sean Doyle*

* Support `strict_loading:` option for `has_rich_text` declaration

*Sean Doyle*
Expand Down
6 changes: 5 additions & 1 deletion actiontext/lib/action_text/system_test_helper.rb
Expand Up @@ -30,7 +30,11 @@ module SystemTestHelper
# # <trix-editor input="trix_input_1"></trix-editor>
# fill_in_rich_text_area "message[content]", with: "Hello <em>world!</em>"
def fill_in_rich_text_area(locator = nil, with:)
find(:rich_text_area, locator).execute_script("this.editor.loadHTML(arguments[0])", with.to_s)
javascript = <<~JS
this.editor.loadHTML(arguments[0])
this.editor.focus()
JS
find(:rich_text_area, locator).execute_script(javascript, with.to_s)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions actiontext/test/system/system_test_helper_test.rb
Expand Up @@ -3,6 +3,14 @@
require "application_system_test_case"

class ActionText::SystemTestHelperTest < ApplicationSystemTestCase
test "filling in a rich-text area focuses the trix-editor" do
visit new_message_url

fill_in_rich_text_area "message_content", with: "Hello world!"

assert_selector(:rich_text_area, "message[content]", &:focused?)
end

test "filling in a rich-text area by ID" do
visit new_message_url
assert_selector "trix-editor#message_content"
Expand Down

0 comments on commit 67a4ac6

Please sign in to comment.