From cd7bff04b499a2741bccedf98b48b32f0c3c654b Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 31 Oct 2018 11:51:44 +0100 Subject: [PATCH] Refactors social meta tags spec --- db/dev_seeds/settings.rb | 3 +- spec/features/social_media_meta_tags_spec.rb | 39 +++++++++----------- spec/support/matchers/have_meta.rb | 15 ++++++++ spec/support/matchers/have_property.rb | 15 ++++++++ 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 spec/support/matchers/have_meta.rb create mode 100644 spec/support/matchers/have_property.rb diff --git a/db/dev_seeds/settings.rb b/db/dev_seeds/settings.rb index 756ba9785..388742217 100644 --- a/db/dev_seeds/settings.rb +++ b/db/dev_seeds/settings.rb @@ -56,7 +56,8 @@ section "Creating Settings" do Setting.create(key: 'mailer_from_name', value: 'CONSUL') Setting.create(key: 'mailer_from_address', value: 'noreply@consul.dev') Setting.create(key: 'meta_title', value: 'CONSUL') - Setting.create(key: 'meta_description', value: 'Citizen Participation & Open Gov Application') + Setting.create(key: 'meta_description', value: 'Citizen participation tool for an open, '\ + 'transparent and democratic government') Setting.create(key: 'meta_keywords', value: 'citizen participation, open government') Setting.create(key: 'verification_offices_url', value: 'http://oficinas-atencion-ciudadano.url/') Setting.create(key: 'min_age_to_participate', value: '16') diff --git a/spec/features/social_media_meta_tags_spec.rb b/spec/features/social_media_meta_tags_spec.rb index dcaef7aee..0eb1d30f6 100644 --- a/spec/features/social_media_meta_tags_spec.rb +++ b/spec/features/social_media_meta_tags_spec.rb @@ -5,16 +5,9 @@ feature 'Social media meta tags' do context 'Setting social media meta tags' do let(:meta_keywords) { 'citizen, participation, open government' } - let(:meta_title) { 'CONSUL TEST' } - let(:meta_description) do - "

Paragraph

link Lorem ipsum dolr"\ - " sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididt"\ - " ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostud"\ - end - let(:sanitized_truncated_meta_description) do - "Paragraph link Lorem ipsum dolr sit amet, consectetur adipisicing elit,"\ - " sed do eiusmod tempor incididt ut labore et dolore magna aliqua. ..." - end + let(:meta_title) { 'CONSUL' } + let(:meta_description) { 'Citizen participation tool for an open, '\ + 'transparent and democratic government.' } let(:twitter_handle) { '@consul_test' } let(:url) { 'http://consul.dev' } let(:facebook_handle) { 'consultest' } @@ -43,19 +36,21 @@ feature 'Social media meta tags' do scenario 'Social media meta tags partial render settings content' do visit root_path + expect(page).to have_meta "keywords", with: meta_keywords + expect(page).to have_meta "twitter:site", with: twitter_handle + expect(page).to have_meta "twitter:title", with: meta_title + expect(page).to have_meta "twitter:description", with: meta_description + expect(page).to have_meta "twitter:image", + with:'http://www.example.com/social_media_icon_twitter.png' - expect(page).to have_css 'meta[name="keywords"][content="' + meta_keywords + '"]', visible: false - expect(page).to have_css 'meta[name="twitter:site"][content="' + twitter_handle + '"]', visible: false - expect(page).to have_css 'meta[name="twitter:title"][content="' + meta_title + '"]', visible: false - expect(page).to have_css 'meta[name="twitter:description"][content="' + sanitized_truncated_meta_description + '"]', visible: false - expect(page).to have_css 'meta[name="twitter:image"][content="http://www.example.com/social_media_icon_twitter.png"]', visible: false - expect(page).to have_css 'meta[property="og:title"][content="' + meta_title + '"]', visible: false - expect(page).to have_css 'meta[property="article:publisher"][content="' + url + '"]', visible: false - expect(page).to have_css 'meta[property="article:author"][content="https://www.facebook.com/' + facebook_handle + '"]', visible: false - expect(page).to have_css 'meta[property="og:url"][content="http://www.example.com/"]', visible: false - expect(page).to have_css 'meta[property="og:image"][content="http://www.example.com/social_media_icon.png"]', visible: false - expect(page).to have_css 'meta[property="og:site_name"][content="' + org_name + '"]', visible: false - expect(page).to have_css 'meta[property="og:description"][content="' + sanitized_truncated_meta_description + '"]', visible: false + expect(page).to have_property "og:title", with: meta_title + expect(page).to have_property "article:publisher", with: url + expect(page).to have_property "article:author", with: 'https://www.facebook.com/' + + facebook_handle + expect(page).to have_property "og:url", with: 'http://www.example.com/' + expect(page).to have_property "og:image", with: 'http://www.example.com/social_media_icon.png' + expect(page).to have_property "og:site_name", with: org_name + expect(page).to have_property "og:description", with: meta_description end end diff --git a/spec/support/matchers/have_meta.rb b/spec/support/matchers/have_meta.rb new file mode 100644 index 000000000..cdee080a1 --- /dev/null +++ b/spec/support/matchers/have_meta.rb @@ -0,0 +1,15 @@ +RSpec::Matchers.define :have_meta do |name, with:| + match do + has_css?("meta[name='#{name}'][content='#{with}']", visible: false) + end + + failure_message do + meta = first("meta[name='#{name}']", visible: false) + + if meta + "expected to find meta tag #{name} with '#{with}', but had '#{meta[:content]}'" + else + "expected to find meta tag #{name} but there were no matches." + end + end +end diff --git a/spec/support/matchers/have_property.rb b/spec/support/matchers/have_property.rb new file mode 100644 index 000000000..17d83ccd0 --- /dev/null +++ b/spec/support/matchers/have_property.rb @@ -0,0 +1,15 @@ +RSpec::Matchers.define :have_property do |property, with:| + match do + has_css?("meta[property='#{property}'][content='#{with}']", visible: false) + end + + failure_message do + meta = first("meta[property='#{property}']", visible: false) + + if meta + "expected to find meta tag #{property} with '#{with}', but had '#{meta[:content]}'" + else + "expected to find meta tag #{property} but there were no matches." + end + end +end