From e9449b63b1433366319ad33eab537772f7c5f0a3 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:45:07 +0000 Subject: [PATCH] Fix issues with tests by not merging Values that existed in the content_item were not being overwritten in our merge, so forcefully override values instead. This surfaced some issues with the tests overall. --- .../presenters/meta_tags.rb | 10 ++-- spec/components/meta_tags_spec.rb | 46 ++++++++++--------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/govuk_publishing_components/presenters/meta_tags.rb b/lib/govuk_publishing_components/presenters/meta_tags.rb index f7ea97584e..a2eb5d4b0a 100644 --- a/lib/govuk_publishing_components/presenters/meta_tags.rb +++ b/lib/govuk_publishing_components/presenters/meta_tags.rb @@ -92,14 +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] current_government = government.dig(:details, :current) - meta_tags["govuk:ga4-political-status"] = current_government ? "political" : "historic" - - government_title = government[:title] - if government_title && !current_government - meta_tags["govuk:ga4-publishing-government"] = government_title + 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 f843a8f977..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,12 +454,10 @@ 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 @@ -474,35 +472,39 @@ def example_document_for(schema_name, example_name) 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