From 609dc376609297ca1645746d77bbc78190f92fe9 Mon Sep 17 00:00:00 2001 From: Keith Lawrence Date: Tue, 2 Jul 2024 15:16:19 +0100 Subject: [PATCH] Convert answer integration test - 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. --- spec/spec_helper.rb | 7 ++ spec/support/content_item_system_helpers.rb | 72 +++++++++++++++++++++ spec/system/answer_spec.rb | 37 +++++++++++ test/integration/answer_test.rb | 36 ----------- test/test_helper.rb | 67 +------------------ 5 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 spec/support/content_item_system_helpers.rb create mode 100644 spec/system/answer_spec.rb delete mode 100644 test/integration/answer_test.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 72e5fb6c15..672a1d9a86 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/support/content_item_system_helpers.rb b/spec/support/content_item_system_helpers.rb new file mode 100644 index 0000000000..744848f3c2 --- /dev/null +++ b/spec/support/content_item_system_helpers.rb @@ -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 diff --git a/spec/system/answer_spec.rb b/spec/system/answer_spec.rb new file mode 100644 index 0000000000..95fd3fb372 --- /dev/null +++ b/spec/system/answer_spec.rb @@ -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 diff --git a/test/integration/answer_test.rb b/test/integration/answer_test.rb deleted file mode 100644 index e7c650b917..0000000000 --- a/test/integration/answer_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require "test_helper" - -class AnswerTest < ActionDispatch::IntegrationTest - test "random but valid items do not error" do - setup_and_visit_random_content_item - end - - test "renders title and body" do - setup_and_visit_content_item("answer") - assert page.has_text?(@content_item["title"]) - assert page.has_text?("Bydd angen cod cychwyn arnoch i ddechrau defnyddio’r holl wasanaethau hyn, ac eithrio TAW. Anfonir hwn atoch cyn pen saith diwrnod gwaith ar ôl i chi gofrestru. Os ydych chi’n byw dramor, gall gymryd hyd at 21 diwrnod i gyrraedd.") - end - - test "related links are rendered" do - setup_and_visit_content_item("answer") - - first_related_link = @content_item["details"]["external_related_links"].first - - within(".gem-c-related-navigation") do - assert page.has_css?(".gem-c-related-navigation__section-link--other[href=\"#{first_related_link['url']}\"]", text: first_related_link["title"]) - end - end - - test "renders FAQ structured data" do - setup_and_visit_content_item("answer") - faq_schema = find_structured_data(page, "FAQPage") - - assert_equal faq_schema["name"], @content_item["title"] - assert_not_equal faq_schema["mainEntity"], [] - end - - test "does not render with the single page notification button" do - setup_and_visit_content_item("answer") - assert_not page.has_css?(".gem-c-single-page-notification-button") - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8bffc5940f..ab1d0fc5c2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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] @@ -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",