Skip to content

Commit

Permalink
Merge pull request #3361 from alphagov/display-content-lists-on-publi…
Browse files Browse the repository at this point in the history
…cations-irrespective-off-h2-count

Display content lists on publications irrespective off h2 count
  • Loading branch information
CodeSonia authored Nov 5, 2024
2 parents c97011f + 0887aee commit 0aee348
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 121 deletions.
3 changes: 1 addition & 2 deletions app/presenters/content_item/contents_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def contents_items
end

def show_contents_list?
return false if contents_items.count < 2
return true if contents_items.count > 2
return true if contents_items.present?
return false if no_first_item?

first_item_size_requirements_met?(character_count, table_row_count)
Expand Down
13 changes: 0 additions & 13 deletions test/integration/corporate_information_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ class CorporateInformationPageTest < ActionDispatch::IntegrationTest
])
end

test "renders without contents list if it has fewer than 3 items" do
item = get_content_example("corporate_information_page")
item["details"]["body"] = "<div class='govspeak'>
<h2>Item one</h2><p>Content about item one</p>
<h2>Item two</h2><p>Content about item two</p>
</div>"

stub_content_store_has_item(item["base_path"], item.to_json)
visit_with_cachebust(item["base_path"])

assert_not page.has_css?(".gem-c-contents-list")
end

test "renders corporate information with body when present" do
setup_and_visit_content_item("corporate_information_page")

Expand Down
5 changes: 0 additions & 5 deletions test/integration/detailed_guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ class DetailedGuideTest < ActionDispatch::IntegrationTest
assert page.has_css?(".gem-c-contents-list")
end

test "renders without contents list if it has fewer than 3 items" do
setup_and_visit_content_item("national_applicability_alternative_url_detailed_guide")
assert_not page.has_css?(".gem-c-contents-list")
end

test "conditionally renders a logo" do
setup_and_visit_content_item("england-2014-to-2020-european-structural-and-investment-funds")

Expand Down
59 changes: 40 additions & 19 deletions test/integration/document_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ class DocumentCollectionTest < ActionDispatch::IntegrationTest
setup_and_visit_content_item("document_collection")
assert_has_component_title(@content_item["title"])
assert page.has_text?(@content_item["description"])
assert page.has_css?(".gem-c-contents-list")
end

test "document collection with no body and 2 collection groups where 1st group has long body" do
content_item = get_content_example("document_collection")
content_item["details"]["collection_groups"][0]["body"] = Faker::Lorem.characters(number: 416)
stub_content_store_has_item(content_item["base_path"], content_item.to_json)
visit(content_item["base_path"])

assert page.has_css?(".gem-c-contents-list")
end

test "renders metadata and document footer" do
Expand All @@ -34,25 +24,56 @@ class DocumentCollectionTest < ActionDispatch::IntegrationTest
assert page.has_text?("Each regime page provides a current list of asset freeze targets designated by the United Nations (UN), European Union and United Kingdom, under legislation relating to current financial sanctions regimes.")
end

test "renders contents with link to each collection group" do
test "adds a contents list, with one list item per document collection group, if the group contains documents" do
setup_and_visit_content_item("document_collection")
assert_equal 6, @content_item["details"]["collection_groups"].size

@content_item["details"]["collection_groups"].each do |group|
assert page.has_css?("nav a", text: group["title"])
end
end

test "renders without contents list if it has fewer than 3 items" do
item = get_content_example("document_collection")
item["details"]["collection_groups"] = [
test "ignores document collection groups that have no documents when presenting the contents list" do
setup_and_visit_content_item("document_collection")
@content_item["details"]["collection_groups"] << { "title" => "Empty Group", "documents" => [] }
assert_equal 7, @content_item["details"]["collection_groups"].size

content_list_items = all("nav.gem-c-contents-list .gem-c-contents-list__list-item")
assert_equal 6, content_list_items.size

@content_item["details"]["collection_groups"].each do |group|
next if group["documents"].empty?

assert page.has_css?("nav a", text: group["title"])
end

assert page.has_css?(".gem-c-contents-list", text: "Contents")
end

test "renders no contents list if body has multiple h2s and is long, but collection groups are empty" do
content_item = get_content_example("document_collection")

content_item["details"]["body"] = <<~HTML
<div class="empty group">
<h2 id="one">One</h2>
<p>#{Faker::Lorem.characters(number: 200)}</p>
<h2 id="two">Two</h2>
<p>#{Faker::Lorem.characters(number: 200)}</p>
<h2 id="three">Three</h2>
<p>#{Faker::Lorem.characters(number: 200)}</p>
</div>
HTML

content_item["details"]["collection_groups"] = [
{
"title" => "Item one",
"body" => "<p>Content about item one</p>",
"documents" => %w[a-content-id],
"body" => "<div class=\"empty group\">\n</div>",
"documents" => [],
"title" => "Empty Group",
},
]
stub_content_store_has_item(item["base_path"], item.to_json)
visit_with_cachebust(item["base_path"])

stub_content_store_has_item(content_item["base_path"], content_item.to_json)
visit(content_item["base_path"])
assert_not page.has_css?(".gem-c-contents-list")
end

Expand Down
5 changes: 2 additions & 3 deletions test/integration/specialist_document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ def assert_nested_content_item(heading)
assert page.has_content?("on the Big Issue Invest website")
end

test "does not render a contents list if there are fewer than three items in the contents list" do
test "renders a content list" do
setup_and_visit_content_item("aaib-reports")

assert_not page.has_css?("#contents .gem-c-contents-list")
assert page.has_css?(".gem-c-contents-list", text: "Contents")
end

test "renders a link to statutory instruments finder" do
Expand Down
13 changes: 0 additions & 13 deletions test/integration/statistical_data_set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ class StatisticalDataSetTest < ActionDispatch::IntegrationTest
])
end

test "renders without contents list if it has fewer than 3 items" do
item = get_content_example("statistical_data_set")
item["details"]["body"] = "<div class='govspeak'>
<h2>Item one</h2><p>Content about item one</p>
<h2>Item two</h2><p>Content about item two</p>
</div>"

stub_content_store_has_item(item["base_path"], item.to_json)
visit_with_cachebust(item["base_path"])

assert_not page.has_css?(".gem-c-contents-list")
end

test "does not render with the single page notification button" do
setup_and_visit_content_item("statistical_data_set")
assert_not page.has_css?(".gem-c-single-page-notification-button")
Expand Down
11 changes: 3 additions & 8 deletions test/integration/topical_event_about_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ class TopicalEventAboutPageTest < ActionDispatch::IntegrationTest
assert_not page.has_css?(".contents-list.contents-list-dashed")
end

test "contents list not displayed when fewer than three items" do
@content_item = get_content_example("topical_event_about_page")
@content_item["details"]["body"] = body_with_two_contents_list_items

stub_content_store_has_item(@content_item["base_path"], @content_item.to_json)

visit_with_cachebust @content_item["base_path"]
assert_not page.has_css?(".gem-c-contents-list")
test "renders a content list" do
setup_and_visit_content_item("topical_event_about_page")
assert page.has_css?(".gem-c-contents-list", text: "Contents")
end

test "contents list displayed when fewer than three items and first item word count is greater than 100" do
Expand Down
14 changes: 3 additions & 11 deletions test/integration/working_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@ class WorkingGroupTest < ActionDispatch::IntegrationTest
assert page.has_text?("Some content")
end

test "renders without contents list if it has fewer than 3 items" do
item = get_content_example("short")
item["details"]["body"] = "<div class='govspeak'>
<h2>Item one</h2><p>Content about item one</p>
<h2>Item two</h2><p>Content about item two</p>
</div>"

stub_content_store_has_item(item["base_path"], item.to_json)
visit_with_cachebust(item["base_path"])

assert_not page.has_css?(".gem-c-contents-list")
test "renders a content list" do
setup_and_visit_content_item("with_policies")
assert page.has_css?(".gem-c-contents-list", text: "Contents")
end

test "does not render with the single page notification button" do
Expand Down
74 changes: 27 additions & 47 deletions test/presenters/content_item/contents_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,91 +84,82 @@ def body
@contents_list.contents_items
end

test "#show_contents_list? returns true if number of contents items is less than 3 and the first item's character count is above 415" do
test "displays content list if there is an H2" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<p>#{Faker::Lorem.characters(number: 220)}</p>
<p>#{Faker::Lorem.characters(number: 196)}</p>
<h2 id='two'>Two</h2>
<p>#{Faker::Lorem.sentence}</p>"
"<h2 id='one'>One</h2>"
end
end
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if number of contents items is 2 and the first item's character count is above 415 including a list" do
test "displays no contents list if there is no H2 and first item is less than 415" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<p>#{Faker::Lorem.characters(number: 40)}</p>
<ul>
<li>#{Faker::Lorem.characters(number: 100)}</li>
<li>#{Faker::Lorem.characters(number: 100)}</li>
<li>#{Faker::Lorem.characters(number: 200)}</li>
</ul>
<p>#{Faker::Lorem.characters(number: 40)}</p>
<h2 id='two'>Two</h2>
<p>#{Faker::Lorem.sentence}</p>"
"<div>
<p>#{Faker::Lorem.characters(number: 400)}</p>
</div>
"
end
end
assert @contents_list.show_contents_list?
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if number of contents items is 3 or more" do
test "displays contents list if the first item's character count is above 415, including a list" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<p>#{Faker::Lorem.sentence}</p>
<p>#{Faker::Lorem.characters(number: 40)}</p>
<ul>
<li>#{Faker::Lorem.characters(number: 100)}</li>
<li>#{Faker::Lorem.characters(number: 100)}</li>
<li>#{Faker::Lorem.characters(number: 200)}</li>
</ul>
<p>#{Faker::Lorem.characters(number: 40)}</p>
<h2 id='two'>Two</h2>
<p>#{Faker::Lorem.sentence}</p>
<h2 id='three'>Three</h2>
<h3>Pi</h3>
<h2 id='four'>#{Faker::Lorem.sentence}</h2>"
<p>#{Faker::Lorem.sentence}</p>"
end
end
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns false if number of contents times is less than 3 and first item's character count is less than 415" do
test "does not display contents list if the first item's character count is less than 415" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
<p>#{Faker::Lorem.characters(number: 10)}</p>
<p>#{Faker::Lorem.characters(number: 10)}</p>
<h2 id='two'>Two</h2>
"<p>#{Faker::Lorem.characters(number: 20)}</p>
<p>#{Faker::Lorem.characters(number: 20)}</p>
<p>#{Faker::Lorem.sentence}</p>"
end
end
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if number of table rows in the first item is more than 13" do
test "displays contents list if number of table rows in the first item is more than 13" do
class << @contents_list
def body
base = "<h2 id='one'>One</h2><table>\n<tbody>\n"
14.times do
base += "<tr>\n<td>#{Faker::Lorem.word}</td>\n<td>#{Faker::Lorem.word}/td>\n</tr>\n"
base += "<tr>\n<td>#{Faker::Lorem.word}</td>\n<td>#{Faker::Lorem.word}</td>\n</tr>\n"
end
base += "</tbody>\n</table><h2 id='two'>Two</h2>"
end
end
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns false if number of table rows in the first item is less than 13" do
test "does not display contents list if number of table rows in the first item is less than 13" do
class << @contents_list
def body
"<h2 id='one'>One</h2><table>\n<tbody>\n
"<table>\n<tbody>\n
<tr>\n<td>#{Faker::Lorem.word}</td>\n<td>#{Faker::Lorem.word}/td>\n</tr>\n
<tr>\n<td>#{Faker::Lorem.word}</td>\n<td>#{Faker::Lorem.word}/td>\n</tr>\n
</tbody>\n</table><h2 id='two'>Two</h2>"
</tbody>\n</table>"
end
end
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if image and over 224 characters are present in the first item" do
test "displays contents list if image is present and the first_item's character count is over 224" do
class << @contents_list
def body
"<h2 id='one'>One</h2>
Expand All @@ -181,7 +172,7 @@ def body
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if image and table over 6 rows are present in the first item" do
test "displays contents list if an image and a table with more than 6 rows are present in the first item" do
class << @contents_list
def body
base = "<h2 id='one'>One</h2><div class='img'>
Expand All @@ -195,17 +186,6 @@ def body
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if body is over 415 characters and has no h2s present" do
class << @contents_list
def body
"<div><p>#{Faker::Lorem.characters(number: 416)}</p></div>"
end
end
@contents_list.stubs(:contents_items).returns(["item 1", "item2"])
@contents_list.stubs(:first_item).returns(first_element)
assert @contents_list.show_contents_list?
end

def first_element
Nokogiri::HTML(@contents_list.body).css("div").first.first_element_child
end
Expand Down

0 comments on commit 0aee348

Please sign in to comment.