Skip to content

Commit

Permalink
Rename richtext controller and write specs
Browse files Browse the repository at this point in the history
  • Loading branch information
binarygit committed Nov 9, 2023
1 parent afd7dd7 commit fdc4546
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/templates/product_modal.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
%filter-selector{ 'selector-set' => "productPropertySelectors", objects: "[product] | propertiesWithValuesOf" }
.product-description{"ng-if" => "product.description_html"}
%p.text-small{"ng-bind-html" => "::product.description_html", "data-controller" => "richtext"}
%p.text-small{"ng-bind-html" => "::product.description_html", "data-controller" => "add-blank-to-link"}
.columns.small-12.medium-6.large-6.product-img
%img{"ng-src" => "{{::product.largeImage}}", "ng-if" => "::product.largeImage"}
Expand Down
24 changes: 24 additions & 0 deletions spec/javascripts/stimulus/add_blank_to_link_controller_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @jest-environment jsdom
*/

import { Application } from "stimulus";
import add_blank_to_link_controller from "../../../app/webpacker/controllers/add_blank_to_link_controller";

describe("AddBlankToLink", () => {
beforeAll(() => {
const application = Application.start();
application.register("add-blank-to-link", add_blank_to_link_controller);
});

describe("#add-blank-to-link", () => {
beforeEach(() => {
document.body.innerHTML = `<div data-controller="add-blank-to-link"><a href="www.ofn.com">www.ofn.com</a></div>`;
});

it("adds target='_blank' to anchor tags", () => {
const anchorTag = document.querySelector('a')
expect(anchorTag.getAttribute('target')).toBe("_blank");
});
});
});
5 changes: 3 additions & 2 deletions spec/system/consumer/shopping/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
visit shop_path
expect(page).to have_content product.name
expect_product_description_html_to_be_displayed(product, product.description, nil,
truncate: true)
truncate: true, href: true)
end

it "does not show unsecure HTML" do
Expand Down Expand Up @@ -144,7 +144,7 @@
end

def expect_product_description_html_to_be_displayed(product, html, not_include = nil,

Check warning on line 146 in spec/system/consumer/shopping/products_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Assignment Branch Condition size for expect_product_description_html_to_be_displayed is too high. [<0, 30, 4> 30.27/30] Raw Output: spec/system/consumer/shopping/products_spec.rb:146:3: C: Metrics/AbcSize: Assignment Branch Condition size for expect_product_description_html_to_be_displayed is too high. [<0, 30, 4> 30.27/30]
truncate: false)
truncate: false, href: false)
# check inside list of products
within "#product-#{product.id} .product-description" do
expect(html).to include(html)
Expand All @@ -157,6 +157,7 @@ def expect_product_description_html_to_be_displayed(product, html, not_include =
expect(page).to have_selector '.reveal-modal'
modal_should_be_open_for product
within(".reveal-modal") do
expect(html.match?('_blank')).to be(true) if href
expect(html).to include(html)
expect(html).not_to include(not_include) if not_include
end
Expand Down

0 comments on commit fdc4546

Please sign in to comment.