Skip to content

Commit

Permalink
Convert answer integration test
Browse files Browse the repository at this point in the history
- These integration tests can be made into system tests.
- We create a helper for system tests using content items and begin moving the methods in from the old test_helper as they're used.
- for the moment these tests aren't including JS, so we can drive them with rack_test.
  • Loading branch information
KludgeKML committed Jul 3, 2024
1 parent 1c8ae5b commit 609dc37
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 102 deletions.
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@

config.include GovukContentSchemaExamples, type: :request

config.include ContentItemSystemHelpers, type: :system
config.include GovukContentSchemaExamples, type: :system

config.include ComponentHelpers, type: :view

config.before(:each, type: :system) do
driven_by :rack_test
end
end
72 changes: 72 additions & 0 deletions spec/support/content_item_system_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "gds_api/test_helpers/content_store"

module ContentItemSystemHelpers
include GdsApi::TestHelpers::ContentStore

# System tests that rely on the methods in this module must be named
# after the schema type they represent (in such a way that they'll
# be translated into lower snake case, ie Answer -> answer,
# TravelAdvice -> travel_advice)
def schema_type
self.class.description.to_s.underscore
end

def setup_and_visit_content_item(name, overrides = {}, parameter_string = "")
get_content_example(name).tap do |item|
content_item = item.deep_merge(overrides)
setup_and_visit_content_item_by_example(content_item, parameter_string)
end
end

def setup_and_visit_content_item_by_example(content_item, parameter_string = "")
stub_content_store_has_item(content_item["base_path"], content_item.to_json)
visit_with_cachebust("#{content_item['base_path']}#{parameter_string}")
end

def setup_and_visit_random_content_item(document_type: nil)
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: schema_type) do |payload|
payload.merge!("document_type" => document_type) unless document_type.nil?
payload
end

path = content_item["base_path"]

if schema_type == "html_publication"
parent = content_item.dig("links", "parent")&.first
if parent
parent_path = parent["base_path"]
stub_request(:get, %r{#{parent_path}})
.to_return(status: 200, body: content_item.to_json, headers: {})
end
end

stub_request(:get, %r{#{path}})
.to_return(status: 200, body: content_item.to_json, headers: {})

visit path

content_item
end

def visit_with_cachebust(visit_uri)
uri = Addressable::URI.parse(visit_uri)
uri.query_values = uri.query_values.yield_self { |values| (values || {}).merge(cachebust: rand) }

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

def get_content_example(name)
get_content_example_by_schema_and_name(schema_type, name)
end

def get_content_example_by_schema_and_name(schema_type, name)
GovukSchemas::Example.find(schema_type, example_name: name)
end
end
37 changes: 37 additions & 0 deletions spec/system/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe("Answer", type: :system) do
it "does not error with a random but valid item" do
content_item = setup_and_visit_random_content_item

expect(page).to have_css("meta[name='govuk:content-id'][content='#{content_item['content_id']}']", visible: false)
end

it "renders title and body" do
content_item = setup_and_visit_content_item("answer")

expect(page).to have_text(content_item["title"])
expect(page).to have_text("Bydd angen cod cychwyn arnoch i ddechrau defnyddio\u2019r holl wasanaethau hyn, ac eithrio TAW. Anfonir hwn atoch cyn pen saith diwrnod gwaith ar \u00F4l i chi gofrestru. Os ydych chi\u2019n byw dramor, gall gymryd hyd at 21 diwrnod i gyrraedd.")
end

it "renders related links" do
content_item = setup_and_visit_content_item("answer")
first_related_link = content_item["details"]["external_related_links"].first

within(".gem-c-related-navigation") do
expect(page).to have_css(".gem-c-related-navigation__section-link--other[href=\"#{first_related_link['url']}\"]", text: first_related_link["title"])
end
end

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

expect(content_item["title"]).to eq(faq_schema["name"])
expect([]).not_to eq(faq_schema["mainEntity"])
end

it "does not render with the single page notification button" do
setup_and_visit_content_item("answer")

expect(page).not_to have_css(".gem-c-single-page-notification-button")
end
end
36 changes: 0 additions & 36 deletions test/integration/answer_test.rb

This file was deleted.

67 changes: 1 addition & 66 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,11 @@ def assert_footer_has_published_dates(first_published = nil, last_updated = nil,
assert_has_published_dates(first_published, last_updated, history_link:)
end

def setup_and_visit_content_item(name, overrides = {}, parameter_string = "")
@content_item = get_content_example(name).tap do |item|
content_item = item.deep_merge(overrides)
setup_and_visit_content_item_by_example(content_item, parameter_string)
end
end

def setup_and_visit_content_item_with_params(name, parameter_string = "")
@content_item = get_content_example(name)
setup_and_visit_content_item_by_example(@content_item, parameter_string)
end

def setup_and_visit_content_item_by_example(content_item, parameter_string = "")
stub_content_store_has_item(content_item["base_path"], content_item.to_json)
visit_with_cachebust("#{content_item['base_path']}#{parameter_string}")
end

def setup_and_visit_html_publication(name, parameter_string = "")
@content_item = get_content_example(name).tap do |item|
parent = item["links"]["parent"][0]
Expand Down Expand Up @@ -215,64 +203,11 @@ def setup_and_visit_a_page_with_specific_base_path(name, base_path, content_id =
end
end

def setup_and_visit_random_content_item(document_type: nil)
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: schema_type) do |payload|
payload.merge!("document_type" => document_type) unless document_type.nil?
payload
end

content_id = content_item["content_id"]
path = content_item["base_path"]

if schema_type == "html_publication"
parent = content_item.dig("links", "parent")&.first
if parent
parent_path = parent["base_path"]
stub_request(:get, %r{#{parent_path}})
.to_return(status: 200, body: content_item.to_json, headers: {})
end
end

stub_request(:get, %r{#{path}})
.to_return(status: 200, body: content_item.to_json, headers: {})

visit path

assert_selector %(meta[name="govuk:content-id"][content="#{content_id}"]), visible: false
end

def get_content_example(name)
get_content_example_by_schema_and_name(schema_type, name)
end

def get_content_example_by_schema_and_name(schema_type, name)
GovukSchemas::Example.find(schema_type, example_name: name)
end

# Override this method if your test file doesn't match the convention
def schema_type
self.class.to_s.gsub("Test", "").underscore
end

def visit_with_cachebust(visit_uri)
uri = Addressable::URI.parse(visit_uri)
uri.query_values = uri.query_values.yield_self { |values| (values || {}).merge(cachebust: rand) }

visit(uri)
end

def assert_has_structured_data(page, schema_name)
assert find_structured_data(page, schema_name).present?
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

def single_page_notification_button_ga4_tracking(index_link, section)
def single_page_notification_button_ga_tracking(index_link, section)
{
"event_name" => "navigation",
"type" => "subscribe",
Expand Down

0 comments on commit 609dc37

Please sign in to comment.