Add scripts component in order to enable/disable vendor scripts

This commit is contained in:
taitus
2024-12-09 16:34:27 +01:00
parent 7407c386a6
commit 5ffaf7a80e
6 changed files with 87 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ linters:
enabled: true
exclude:
- app/components/budgets/investments/content_blocks_component.html.erb
- app/components/layout/cookies_consent/vendors/scripts_component.html.erb
- app/components/layout/footer_component.html.erb
- app/components/layout/social_component.html.erb
- app/components/layout/subnavigation_component.html.erb

View File

@@ -0,0 +1,16 @@
<script type="text/javascript">
<% vendors.each do |vendor| %>
<% vendor_method_name = "#{vendor.cookie}_script" %>
function <%= vendor_method_name %>() {
<%= sanitize vendor.script %>
}
<% initialize_vendor_method_name = "initialize_#{vendor.cookie}_script" %>
function <%= initialize_vendor_method_name %>() {
if (App.Cookies.getCookie("<%= vendor.cookie %>") === "true") {
<%= vendor_method_name %>();
}
}
$(document).on("turbolinks:load", <%= initialize_vendor_method_name %>);
<% end %>
</script>

View File

@@ -0,0 +1,5 @@
class Layout::CookiesConsent::Vendors::ScriptsComponent < Layout::CookiesConsent::BaseComponent
def render?
super && vendors.any?
end
end

View File

@@ -11,6 +11,7 @@
<%= content_for :social_media_meta_tags %>
<%= raw setting["html.per_page_code_head"] %>
<%= render Layout::CookiesConsent::Vendors::ScriptsComponent.new %>
</head>
<body class="<%= yield(:body_class) %> public">
<%= render Layout::SkipToMainContentComponent.new %>

View File

@@ -0,0 +1,32 @@
require "rails_helper"
describe Layout::CookiesConsent::Vendors::ScriptsComponent do
before do
Setting["feature.cookies_consent"] = true
end
it "is not rendered when there are no vendors" do
render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new
expect(page).not_to be_rendered
end
it "is not rendered when cookies consent feature is not enabled" do
Setting["feature.cookies_consent"] = false
create(:cookies_vendor, name: "Third party", cookie: "third_party", script: "alert('Welcome!')")
render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new
expect(page).not_to be_rendered
end
it "renders script for vendor" do
create(:cookies_vendor, name: "Third party", cookie: "third_party", script: "alert('Welcome!')")
render_inline Layout::CookiesConsent::Vendors::ScriptsComponent.new
expect(page).to have_content "function third_party_script()"
expect(page).to have_content "alert('Welcome!')"
expect(page).to have_css "script", visible: :none
end
end

View File

@@ -3,7 +3,8 @@ require "rails_helper"
describe "Cookies consent" do
before do
Setting["feature.cookies_consent"] = true
create(:cookies_vendor, name: "Third party", cookie: "third_party" )
script = "$('body').append('Running third party script');"
create(:cookies_vendor, name: "Third party", cookie: "third_party", script: script)
end
context "Banner consent" do
@@ -12,6 +13,7 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")).to be nil
expect(cookie_by_name("third_party")).to be nil
expect(page).not_to have_content "Running third party script"
within ".cookies-consent-banner" do
click_button "Accept essential cookies"
@@ -20,10 +22,12 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")[:value]).to eq "essential"
expect(cookie_by_name("third_party")[:value]).to eq "false"
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Running third party script"
refresh
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Running third party script"
end
scenario "Hides the banner when accepting all cookies and for consecutive visits" do
@@ -31,6 +35,7 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")).to be nil
expect(cookie_by_name("third_party")).to be nil
expect(page).not_to have_content "Running third party script"
within ".cookies-consent-banner" do
click_button "Accept all"
@@ -39,10 +44,12 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")[:value]).to eq "all"
expect(cookie_by_name("third_party")[:value]).to eq "true"
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Running third party script"
refresh
expect(page).not_to have_content "Cookies policy"
expect(page).to have_content "Running third party script", count: 1
end
end
@@ -52,6 +59,7 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")).to be nil
expect(cookie_by_name("third_party")).to be nil
expect(page).not_to have_content "Running third party script"
within ".cookies-consent-banner" do
click_button "Manage cookies"
@@ -65,10 +73,12 @@ describe "Cookies consent" do
expect(cookie_by_name("third_party")[:value]).to eq "false"
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Cookies management"
expect(page).not_to have_content "Running third party script"
refresh
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Running third party script"
end
scenario "Allow users to accept all cookies from the cookies management modal" do
@@ -76,6 +86,7 @@ describe "Cookies consent" do
expect(cookie_by_name("cookies_consent")).to be nil
expect(cookie_by_name("third_party")).to be nil
expect(page).not_to have_content "Running third party script"
within ".cookies-consent-banner" do
click_button "Manage cookies"
@@ -89,10 +100,12 @@ describe "Cookies consent" do
expect(cookie_by_name("third_party")[:value]).to eq "true"
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Cookies management"
expect(page).not_to have_content "Running third party script"
refresh
expect(page).not_to have_content "Cookies policy"
expect(page).to have_content "Running third party script", count: 1
end
scenario "Allow users to accept custom cookies from the cookies management modal" do
@@ -117,10 +130,28 @@ describe "Cookies consent" do
expect(cookie_by_name("third_party")[:value]).to eq "true"
expect(page).not_to have_content "Cookies policy"
expect(page).not_to have_content "Cookies management"
expect(page).not_to have_content "Running third party script"
refresh
expect(page).to have_content "Running third party script", count: 1
expect(page).not_to have_content "Cookies policy"
click_link "Manage cookies"
within ".cookies-consent-management" do
expect(page).to have_checked_field "third_party", visible: :none
find("label[for='third_party']").click
expect(page).to have_unchecked_field "third_party", visible: :none
click_button "Save preferences"
end
expect(page).to have_content "Running third party script", count: 1
refresh
expect(page).not_to have_content "Running third party script"
end
end
end