Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open external links in product description in a new page #11761

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions spec/system/consumer/shopping/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@

visit shop_path
expect(page).to have_content product.name

# It truncates a long product description.
within_product_description(product) do
expect(html).to include product.description
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I brought this line back but it's actually wrong because we want to see only part of the description with ... at the end. It'll be fixed in a later commit.

expect(page).to have_content "..."
end
expect_product_description_html_to_be_displayed(product, product.description, nil,
truncate: true, href: true)
href: true)
end

it "does not show unsecure HTML" do
Expand All @@ -94,8 +100,12 @@
visit shop_path
expect(page).to have_content product.name

within_product_description(product) do
expect(html).to include "<p>Safe</p>"
expect(html).not_to include "<script>alert('Dangerous!');</script>"
end
expect_product_description_html_to_be_displayed(
product, "<p>Safe</p>", "<script>alert('Dangerous!');</script>", truncate: false
product, "<p>Safe</p>", "<script>alert('Dangerous!');</script>"
)
end
end
Expand Down Expand Up @@ -144,13 +154,7 @@
end

def expect_product_description_html_to_be_displayed(product, html, not_include = nil,
truncate: false, href: false)
# check inside list of products
within "#product-#{product.id} .product-description" do
expect(html).not_to include(not_include) if not_include
expect(page).to have_content "..." if truncate # it truncates a long product description
end

href: false)
# check in product description modal
click_link product.name
modal_should_be_open_for product
Expand All @@ -160,4 +164,8 @@ def expect_product_description_html_to_be_displayed(product, html, not_include =
expect(html).not_to include(not_include) if not_include
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not split this up? There are two distinct chunks of tests here. One is about the product list and the other about the modal. Boolean flags are often a code smell as well. There are some strange things in this method. This line is self-fulfilling:

      expect(html).to include(html)

This line also seems unnecessary:

    expect(page).to have_selector '.reveal-modal'

The next line should test the same thing.

Copy link
Contributor Author

@binarygit binarygit Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed those redundant assertions.

I tried to make the other assertions better but have not found a clear way to do so. I think the intention is to check whether in the rendered HTML the html passed into the method exists but I couldn't come up with a solution that can make the assertion.
Also the spec right after this,

     it "does not show unsecure HTML" do
        product.description = "<script>alert('Dangerous!');</script><p>Safe</p>"
        product.save!

        visit shop_path
        expect(page).to have_content product.name

        expect_product_description_html_to_be_displayed(
          product, "<p>Safe</p>", "<script>alert('Dangerous!');</script>", truncate: false
        )
     end

says that unsecure html should not be displayed but is calling the same method at the end, I don't know what the actual behaviour should be but what's currently happening is that the unsecure HTML does show up (as text) in the page.

end

def within_product_description(product, &)
within("#product-#{product.id} .product-description", &)
end
end