# encoding: UTF-8 require 'test_helper' class GovspeakContactsTest < Minitest::Test def build_contact(attrs={}) { id: attrs.fetch(:id, 123456), content_id: attrs.fetch(:content_id, "4f3383e4-48a2-4461-a41d-f85ea8b89ba0"), title: attrs.fetch(:title, "Government Digital Service"), recipient: attrs.fetch(:recipient, ""), street_address: attrs.fetch(:street_address, "125 Kingsway"), postal_code: attrs.fetch(:postal_code, "WC2B 6NH"), locality: attrs.fetch(:locality, "Holborn"), region: attrs.fetch(:region, "London"), country_code: attrs.fetch(:country_code, "gb"), email: attrs.fetch(:email, "people@digital.cabinet-office.gov.uk"), contact_form_url: attrs.fetch(:contact_form_url, ""), contact_numbers: attrs.fetch(:contact_numbers, [{ label: "helpdesk", number: "+4412345 67890" }]), comments: attrs.fetch(:comments, ""), worldwide_organisation_path: attrs.fetch(:worldwide_organisation_path, nil), } end def compress_html(html) html.gsub(/[\n\r]+[\s]*/,'') end test "contact is rendered when present in options[:contacts]" do contact = build_contact govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]" rendered = Govspeak::Document.new(govspeak, { contacts: [contact] }).to_html expected_html_output = %{

Government Digital Service

125 Kingsway
Holborn
London
WC2B 6NH

} assert_equal(compress_html(expected_html_output), compress_html(rendered)) end test "no contact is rendered when contact not present in options[:contacts]" do contact = build_contact(content_id: "19f06142-1b4a-47ce-b257-964badd0a5e2") govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]" rendered = Govspeak::Document.new(govspeak, { contacts: [contact]}).to_html assert_match("", rendered) end test "no contact is rendered when no contacts are supplied" do rendered = Govspeak::Document.new("[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]").to_html assert_match("", rendered) end test "contact with no postal address omits the address info" do contact = build_contact( recipient: nil, street_address: nil, locality: nil, region: nil, postal_code: nil, ) govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]" rendered = Govspeak::Document.new(govspeak, { contacts: [contact] }).to_html expected_html_output = %{

Government Digital Service

} assert_equal(compress_html(expected_html_output), compress_html(rendered)) end test "worldwide office contact renders worldwide organisation link" do contact = build_contact(worldwide_organisation_path: "/government/world/organisations/british-antarctic-territory") govspeak = "[Contact:4f3383e4-48a2-4461-a41d-f85ea8b89ba0]" rendered = Govspeak::Document.new(govspeak, { contacts: [contact] }).to_html organisation_link = %Q(Access and opening times) assert_match(organisation_link, rendered) end end