Skip to content

Commit

Permalink
Add feature-test coverage for Rich Text Field
Browse files Browse the repository at this point in the history
Follow-up to [#2411][]

Add Capybara-driven feature test coverage for filling in and rendering
the contents of a `<trix-editor>` element.

[#2411]: #2411
  • Loading branch information
seanpdoyle committed Nov 1, 2024
1 parent 010ea1c commit 881ef61
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
26 changes: 23 additions & 3 deletions spec/features/products_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "product form has_one relationship" do
include ActiveSupport::Testing::TimeHelpers

it "saves product and meta tag data correctly" do
it "saves product and meta tag data correctly", js: true do
visit new_admin_product_path

fill_in "Name", with: "Example"
Expand All @@ -12,11 +12,17 @@
fill_in "Image url", with: "http://imageurlthatdoesnotexist"
fill_in "Meta title", with: "Example meta title"
fill_in "Meta description", with: "Example meta description"
fill_in_rich_text_area "Banner", with: <<~HTML
<div>A banner with a <a href="https://example.com">link</a>.</div>
HTML

expect(page).to have_css("legend", text: "Product Meta Tag")

click_on "Create Product"

within(".trix-content div", text: "A banner with a link") do
expect(page).to have_link("link", href: "https://example.com")
end
expect(page).to have_link("Example meta title")
expect(page).to have_flash(
t("administrate.controller.create.success", resource: "Product")
Expand Down Expand Up @@ -53,13 +59,27 @@
ProductDashboard::ATTRIBUTE_TYPES[:release_year] = old_release_year
end

it "edits product and meta tag data correctly" do
product = create(:product)
it "edits product and meta tag data correctly", js: true do
product = create(:product, banner: <<~HTML)
<div>A banner with a <a href="https://example.com">link</a>.</div>
HTML

visit edit_admin_product_path(product)

within(:rich_text_area, "Banner") do
within("div", text: "A banner with a link") do
expect(page).to have_link("link", href: "https://example.com")
end
end

fill_in_rich_text_area "Banner", with: <<~HTML
<div>A changed banner without a link.</div>
HTML
click_on "Update Product"

within(".trix-content div", text: "A changed banner without a link.") do
expect(page).to have_no_link(href: "https://example.com")
end
expect(page).to have_link(product.product_meta_tag.meta_title.to_s)
expect(page).to have_flash(
t("administrate.controller.update.success", resource: "Product")
Expand Down
23 changes: 23 additions & 0 deletions spec/support/features/action_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Features
if Rails.version >= "6.1"
require "action_text/system_test_helper"

include ActionText::SystemTestHelper
else
def fill_in_rich_text_area(locator = nil, with:)
find(:rich_text_area, locator).execute_script("this.editor.loadHTML(arguments[0])", with.to_s)
end

Capybara.add_selector :rich_text_area do
xpath do |label|
trix_editor = XPath.descendant(:"trix-editor")

if label.nil?
trix_editor
else
trix_editor.where XPath.attr(:"aria-label").equals(label)
end
end
end
end
end

0 comments on commit 881ef61

Please sign in to comment.