From d430dad8433364b86c6667f541b3c83d4894ce2c Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:46:20 +0000 Subject: [PATCH] Prevent government_frontend test failure if meta tag key doesn't exist This change also fixes issues with tests by overriding/deleting content item values instead of trying to merge hashes together. Values that existed in the content_item were not being overwritten in our merge. This surfaced some issues with the tests overall. Fixing this led to cleaner/simpler code. --- CHANGELOG.md | 4 ++ .../presenters/meta_tags.rb | 11 ++-- spec/components/meta_tags_spec.rb | 55 +++++++++++-------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9834f2314e..c6f3037111 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +## Unreleased + +* Prevent government_frontend test failure if meta tag key doesn't exist ([PR #3741](https://github.com/alphagov/govuk_publishing_components/pull/3741)) + ## 36.0.1 * Use component wrapper on option select ([PR #3738](https://github.com/alphagov/govuk_publishing_components/pull/3738)) diff --git a/lib/govuk_publishing_components/presenters/meta_tags.rb b/lib/govuk_publishing_components/presenters/meta_tags.rb index fd7911f17e..a2eb5d4b0a 100644 --- a/lib/govuk_publishing_components/presenters/meta_tags.rb +++ b/lib/govuk_publishing_components/presenters/meta_tags.rb @@ -92,13 +92,12 @@ def add_political_tags(meta_tags) def add_ga4_political_tags(meta_tags) government = content_item.dig(:links, :government, 0) - # political: true/false is in a different place to current: true/false, which is why we have 'details' and 'government[:details]' + # political: true/false is in a different place to current: true/false, which is why we have 'details' and 'government.dig(:details)' if government && details[:political] - meta_tags["govuk:ga4-political-status"] = government[:details][:current] ? "political" : "historic" - - government_title = government[:title] - if government_title && !government[:details][:current] - meta_tags["govuk:ga4-publishing-government"] = government_title + current_government = government.dig(:details, :current) + unless current_government + meta_tags["govuk:ga4-political-status"] = "historic" + meta_tags["govuk:ga4-publishing-government"] = government[:title] if government[:title] end end diff --git a/spec/components/meta_tags_spec.rb b/spec/components/meta_tags_spec.rb index 79d90cf444..20c31db630 100644 --- a/spec/components/meta_tags_spec.rb +++ b/spec/components/meta_tags_spec.rb @@ -440,10 +440,10 @@ def example_document_for(schema_name, example_name) end it "doesn't render govuk:ga4-browse-topic if the dig doesn't return anything" do - content_item = { - "links": nil, - } - render_component(content_item: example_document_for("transaction", "transaction").merge(content_item)) + content_item = example_document_for("html_publication", "published_with_history_mode") + content_item.delete("links") + + render_component(content_item:) assert_no_meta_tag("govuk:ga4-browse-topic") end @@ -454,46 +454,57 @@ def example_document_for(schema_name, example_name) end it "doesn't render GA4 political tags if the government object doesn't exist in the content item" do - content_item = { - "links": { - "government": nil, - }, - } - render_component(content_item: example_document_for("html_publication", "published_with_history_mode").merge(content_item)) + content_item = example_document_for("html_publication", "published_with_history_mode") + content_item["links"].delete("government") + + render_component(content_item:) assert_no_meta_tag("govuk:ga4-publishing-government") assert_no_meta_tag("govuk:ga4-political-status") end + it "doesn't crash generating GA4 political tags if details/current does not exist" do + content_item = example_document_for("html_publication", "published_with_history_mode") + content_item["links"]["government"][0].delete("details") + + render_component(content_item:) + assert_meta_tag("govuk:ga4-publishing-government", "2010 to 2015 Conservative and Liberal Democrat coalition government") + assert_meta_tag("govuk:ga4-political-status", "historic") + end + it "doesn't render GA4 political tags if the government object exists in the content item, but political is false" do - content_item = { - "political": false, - "links": { - "government": { + content_item = example_document_for("html_publication", "published_with_history_mode") + content_item["details"]["political"] = false + content_item["links"] = { + "government": [ + { "details": { "current": false, }, "title": "2005 to 2010 Labour government", }, - }, + ], } - render_component(content_item: example_document_for("html_publication", "published_with_history_mode").merge(content_item)) + + render_component(content_item:) assert_no_meta_tag("govuk:ga4-publishing-government") assert_no_meta_tag("govuk:ga4-political-status") end it "doesn't render GA4 political tags if the government object exists in the content item, but it refers to the current government" do - content_item = { - "political": true, - "links": { - "government": { + content_item = example_document_for("html_publication", "published_with_history_mode") + content_item["details"]["political"] = true + content_item["links"] = { + "government": [ + { "details": { "current": true, }, "title": "2015 Conservative government", }, - }, + ], } - render_component(content_item: example_document_for("html_publication", "published_with_history_mode").merge(content_item)) + + render_component(content_item:) assert_no_meta_tag("govuk:ga4-publishing-government") assert_no_meta_tag("govuk:ga4-political-status") end