From 881ef618b04fc5cac78c85abdc8cc1ae78159b8a Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 1 Nov 2024 10:05:27 -0400 Subject: [PATCH] Add feature-test coverage for Rich Text Field Follow-up to [#2411][] Add Capybara-driven feature test coverage for filling in and rendering the contents of a `` element. [#2411]: https://github.com/thoughtbot/administrate/pull/2411 --- spec/features/products_form_spec.rb | 26 +++++++++++++++++++++++--- spec/support/features/action_text.rb | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 spec/support/features/action_text.rb diff --git a/spec/features/products_form_spec.rb b/spec/features/products_form_spec.rb index 4eddddfb48..469cff2fbd 100644 --- a/spec/features/products_form_spec.rb +++ b/spec/features/products_form_spec.rb @@ -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" @@ -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 +
A banner with a link.
+ 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") @@ -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) +
A banner with a link.
+ 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 +
A changed banner without a link.
+ 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") diff --git a/spec/support/features/action_text.rb b/spec/support/features/action_text.rb new file mode 100644 index 0000000000..7ea51195f0 --- /dev/null +++ b/spec/support/features/action_text.rb @@ -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