Skip to content

Commit

Permalink
Extract schema finding logic to helper
Browse files Browse the repository at this point in the history
This is repeated a few times now and doesn't add much by being there.
  • Loading branch information
sihugh committed Sep 19, 2019
1 parent db2f596 commit 9456891
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
5 changes: 1 addition & 4 deletions test/integration/answer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ class AnswerTest < ActionDispatch::IntegrationTest

test "renders FAQ structured data" do
setup_and_visit_content_item('answer')
faq_schema = find_structured_data(page, "FAQPage")

schema_sections = page.find_all("script[type='application/ld+json']", visible: false)
schemas = schema_sections.map { |section| JSON.parse(section.text(:all)) }

faq_schema = schemas.detect { |schema| schema["@type"] == "FAQPage" }
assert_equal faq_schema["headline"], @content_item['title']
end
end
10 changes: 2 additions & 8 deletions test/integration/guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,15 @@ class GuideTest < ActionDispatch::IntegrationTest

test "guides show the faq page schema" do
setup_and_visit_content_item('guide')
faq_schema = find_structured_data(page, "FAQPage")

schema_sections = page.find_all("script[type='application/ld+json']", visible: false)
schemas = schema_sections.map { |section| JSON.parse(section.text(:all)) }

faq_schema = schemas.detect { |schema| schema["@type"] == "FAQPage" }
assert_equal faq_schema["headline"], @content_item['title']
end

test "guide chapters show the faq schema" do
setup_and_visit_part_in_guide
faq_schema = find_structured_data(page, "FAQPage")

schema_sections = page.find_all("script[type='application/ld+json']", visible: false)
schemas = schema_sections.map { |section| JSON.parse(section.text(:all)) }

faq_schema = schemas.detect { |schema| schema["@type"] == "FAQPage" }
assert_equal faq_schema["headline"], @content_item['title']
end

Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ def visit_with_cachebust(visit_uri)

visit(uri)
end

def find_structured_data(page, schema_name)
schema_sections = page.find_all("script[type='application/ld+json']", visible: false)
schemas = schema_sections.map { |section| JSON.parse(section.text(:all)) }

schemas.detect { |schema| schema["@type"] == schema_name }
end
end

0 comments on commit 9456891

Please sign in to comment.