Skip to content

Commit

Permalink
Prevent government_frontend test failure if meta tag key doesn't exist
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AshGDS committed Dec 1, 2023
1 parent 7ff9203 commit d430dad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 5 additions & 6 deletions lib/govuk_publishing_components/presenters/meta_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
55 changes: 33 additions & 22 deletions spec/components/meta_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit d430dad

Please sign in to comment.