-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
5 changed files
with
117 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters