Commit 5f04d84b authored by Rajendra Kadam's avatar Rajendra Kadam Committed by Peter Leitzen

Move common specs to shared examples

Use be_empty wherever equated with 0
parent 79e19c5e
......@@ -10,6 +10,10 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
let_it_be(:custom_emoji) { create(:custom_emoji, name: 'tanuki', group: group) }
let_it_be(:custom_emoji2) { create(:custom_emoji, name: 'happy_tanuki', group: group, file: 'https://foo.bar/happy.png') }
it_behaves_like 'emoji filter' do
let(:emoji_name) { ':tanuki:' }
end
it 'replaces supported name custom emoji' do
doc = filter('<p>:tanuki:</p>', project: project)
......@@ -17,25 +21,12 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
expect(doc.css('gl-emoji img').size).to eq 1
end
it 'ignores non existent custom emoji' do
exp = '<p>:foo:</p>'
doc = filter(exp)
expect(doc.to_html).to eq(exp)
end
it 'correctly uses the custom emoji URL' do
doc = filter('<p>:tanuki:</p>')
expect(doc.css('img').first.attributes['src'].value).to eq(custom_emoji.file)
end
it 'matches with adjacent text' do
doc = filter('tanuki (:tanuki:)')
expect(doc.css('img').size).to eq 1
end
it 'matches multiple same custom emoji' do
doc = filter(':tanuki: :tanuki:')
......@@ -54,18 +45,6 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
expect(doc.css('img').size).to be 0
end
it 'keeps whitespace intact' do
doc = filter('This deserves a :tanuki:, big time.')
expect(doc.to_html).to match(/^This deserves a <gl-emoji.+>, big time\.\z/)
end
it 'does not match emoji in a string' do
doc = filter("'2a00:tanuki:100::1'")
expect(doc.css('gl-emoji').size).to eq 0
end
it 'does not do N+1 query' do
create(:custom_emoji, name: 'party-parrot', group: group)
......@@ -77,10 +56,4 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
filter('<p>:tanuki: :party-parrot:</p>')
end.not_to exceed_all_query_limit(control_count.count)
end
context 'ancestor tags' do
let(:emoji_name) { ':tanuki:' }
it_behaves_like 'ignored ancestor tags'
end
end
......@@ -5,6 +5,10 @@ require 'spec_helper'
RSpec.describe Banzai::Filter::EmojiFilter do
include FilterSpecHelper
it_behaves_like 'emoji filter' do
let(:emoji_name) { ':+1:' }
end
it 'replaces supported name emoji' do
doc = filter('<p>:heart:</p>')
expect(doc.css('gl-emoji').first.text).to eq '❤'
......@@ -15,12 +19,6 @@ RSpec.describe Banzai::Filter::EmojiFilter do
expect(doc.css('gl-emoji').first.text).to eq '❤'
end
it 'ignores unsupported emoji' do
exp = act = '<p>:foo:</p>'
doc = filter(act)
expect(doc.to_html).to match Regexp.escape(exp)
end
it 'ignores unicode versions of trademark, copyright, and registered trademark' do
exp = act = '<p>™ © ®</p>'
doc = filter(act)
......@@ -65,11 +63,6 @@ RSpec.describe Banzai::Filter::EmojiFilter do
expect(doc.css('gl-emoji').size).to eq 1
end
it 'matches with adjacent text' do
doc = filter('+1 (:+1:)')
expect(doc.css('gl-emoji').size).to eq 1
end
it 'unicode matches with adjacent text' do
doc = filter('+1 (👍)')
expect(doc.css('gl-emoji').size).to eq 1
......@@ -90,12 +83,6 @@ RSpec.describe Banzai::Filter::EmojiFilter do
expect(doc.css('gl-emoji').size).to eq 6
end
it 'does not match emoji in a string' do
doc = filter("'2a00:a4c0:100::1'")
expect(doc.css('gl-emoji').size).to eq 0
end
it 'has a data-name attribute' do
doc = filter(':-1:')
expect(doc.css('gl-emoji').first.attr('data-name')).to eq 'thumbsdown'
......@@ -106,21 +93,9 @@ RSpec.describe Banzai::Filter::EmojiFilter do
expect(doc.css('gl-emoji').first.attr('data-unicode-version')).to eq '6.0'
end
it 'keeps whitespace intact' do
doc = filter('This deserves a :+1:, big time.')
expect(doc.to_html).to match(/^This deserves a <gl-emoji.+>, big time\.\z/)
end
it 'unicode keeps whitespace intact' do
doc = filter('This deserves a 🎱, big time.')
expect(doc.to_html).to match(/^This deserves a <gl-emoji.+>, big time\.\z/)
end
context 'ancestor tags' do
let(:emoji_name) { ':see_no_evil:' }
it_behaves_like 'ignored ancestor tags'
end
end
# frozen_string_literal: true
RSpec.shared_examples 'ignored ancestor tags' do
RSpec.shared_examples 'emoji filter' do
it 'keeps whitespace intact' do
doc = filter("This deserves a #{emoji_name}, big time.")
expect(doc.to_html).to match(/^This deserves a <gl-emoji.+>, big time\.\z/)
end
it 'does not match emoji in a string' do
doc = filter("'2a00:a4c0#{emoji_name}:1'")
expect(doc.css('gl-emoji')).to be_empty
end
it 'ignores non existent/unsupported emoji' do
exp = '<p>:foo:</p>'
doc = filter(exp)
expect(doc.to_html).to eq(exp)
end
it 'matches with adjacent text' do
doc = filter("#{emoji_name.delete(':')} (#{emoji_name})")
expect(doc.css('gl-emoji').size).to eq 1
end
it 'does not match emoji in a pre tag' do
doc = filter("<p><pre>#{emoji_name}</pre></p>")
expect(doc.css('img').size).to eq 0
expect(doc.css('img')).to be_empty
end
it 'does not match emoji in code tag' do
doc = filter("<p><code>#{emoji_name} wow</code></p>")
expect(doc.css('img').size).to eq 0
expect(doc.css('img')).to be_empty
end
it 'does not match emoji in tt tag' do
doc = filter("<p><tt>#{emoji_name} yes!</tt></p>")
expect(doc.css('img').size).to eq 0
expect(doc.css('img')).to be_empty
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment