summaryrefslogtreecommitdiff
path: root/spec/helpers
diff options
context:
space:
mode:
authorFrancisco Javier Lรณpez <fjlopez@gitlab.com>2017-11-06 16:52:56 +0000
committerDouwe Maan <douwe@gitlab.com>2017-11-06 16:52:56 +0000
commitbf0331dc72db658b012d58e33ca47da6d8f99d46 (patch)
treef596c4876cd50bf13afd1f3e60ccd57640b61656 /spec/helpers
parent34a205b308d80d600c5651cfb9694f978ef01cab (diff)
downloadgitlab-ce-bf0331dc72db658b012d58e33ca47da6d8f99d46.tar.gz
Resolve "DashboardController#activity.json is slow due to SQL"
Diffstat (limited to 'spec/helpers')
-rw-r--r--spec/helpers/events_helper_spec.rb90
-rw-r--r--spec/helpers/markup_helper_spec.rb151
2 files changed, 127 insertions, 114 deletions
diff --git a/spec/helpers/events_helper_spec.rb b/spec/helpers/events_helper_spec.rb
index d5536fcb22b..8a80b88da5d 100644
--- a/spec/helpers/events_helper_spec.rb
+++ b/spec/helpers/events_helper_spec.rb
@@ -1,96 +1,6 @@
require 'spec_helper'
describe EventsHelper do
- describe '#event_note' do
- let(:user) { build(:user) }
-
- before do
- allow(helper).to receive(:current_user).and_return(user)
- end
-
- it 'displays one line of plain text without alteration' do
- input = 'A short, plain note'
- expect(helper.event_note(input)).to match(input)
- expect(helper.event_note(input)).not_to match(/\.\.\.\z/)
- end
-
- it 'displays inline code' do
- input = 'A note with `inline code`'
- expected = 'A note with <code>inline code</code>'
-
- expect(helper.event_note(input)).to match(expected)
- end
-
- it 'truncates a note with multiple paragraphs' do
- input = "Paragraph 1\n\nParagraph 2"
- expected = 'Paragraph 1...'
-
- expect(helper.event_note(input)).to match(expected)
- end
-
- it 'displays the first line of a code block' do
- input = "```\nCode block\nwith two lines\n```"
- expected = %r{<pre.+><code><span class="line">Code block\.\.\.</span>\n</code></pre>}
-
- expect(helper.event_note(input)).to match(expected)
- end
-
- it 'truncates a single long line of text' do
- text = 'The quick brown fox jumped over the lazy dog twice' # 50 chars
- input = text * 4
- expected = (text * 2).sub(/.{3}/, '...')
-
- expect(helper.event_note(input)).to match(expected)
- end
-
- it 'preserves a link href when link text is truncated' do
- text = 'The quick brown fox jumped over the lazy dog' # 44 chars
- input = "#{text}#{text}#{text} " # 133 chars
- link_url = 'http://example.com/foo/bar/baz' # 30 chars
- input << link_url
- expected_link_text = 'http://example...</a>'
-
- expect(helper.event_note(input)).to match(link_url)
- expect(helper.event_note(input)).to match(expected_link_text)
- end
-
- it 'preserves code color scheme' do
- input = "```ruby\ndef test\n 'hello world'\nend\n```"
- expected = "\n<pre class=\"code highlight js-syntax-highlight ruby\">" \
- "<code><span class=\"line\"><span class=\"k\">def</span> <span class=\"nf\">test</span>...</span>\n" \
- "</code></pre>"
- expect(helper.event_note(input)).to eq(expected)
- end
-
- it 'preserves data-src for lazy images' do
- input = "![ImageTest](/uploads/test.png)"
- image_url = "data-src=\"/uploads/test.png\""
- expect(helper.event_note(input)).to match(image_url)
- end
-
- context 'labels formatting' do
- let(:input) { 'this should be ~label_1' }
-
- def format_event_note(project)
- create(:label, title: 'label_1', project: project)
-
- helper.event_note(input, { project: project })
- end
-
- it 'preserves style attribute for a label that can be accessed by current_user' do
- project = create(:project, :public)
-
- expect(format_event_note(project)).to match(/span class=.*style=.*/)
- end
-
- it 'does not style a label that can not be accessed by current_user' do
- project = create(:project, :private)
-
- expect(format_event_note(project)).to eq("<p>#{input}</p>")
- end
- end
- end
-
describe '#event_commit_title' do
let(:message) { "foo & bar " + "A" * 70 + "\n" + "B" * 80 }
subject { helper.event_commit_title(message) }
diff --git a/spec/helpers/markup_helper_spec.rb b/spec/helpers/markup_helper_spec.rb
index 03d706062b7..62ea6d48542 100644
--- a/spec/helpers/markup_helper_spec.rb
+++ b/spec/helpers/markup_helper_spec.rb
@@ -67,7 +67,7 @@ describe MarkupHelper do
describe 'without redacted attribute' do
it 'renders the markdown value' do
- expect(Banzai).to receive(:render_field).with(commit, attribute).and_call_original
+ expect(Banzai).to receive(:render_field).with(commit, attribute, {}).and_call_original
helper.markdown_field(commit, attribute)
end
@@ -252,38 +252,141 @@ describe MarkupHelper do
end
describe '#first_line_in_markdown' do
- it 'truncates Markdown properly' do
- text = "@#{user.username}, can you look at this?\nHello world\n"
- actual = first_line_in_markdown(text, 100, project: project)
+ shared_examples_for 'common markdown examples' do
+ let(:project_base) { build(:project, :repository) }
- doc = Nokogiri::HTML.parse(actual)
+ it 'displays inline code' do
+ object = create_object('Text with `inline code`')
+ expected = 'Text with <code>inline code</code>'
- # Make sure we didn't create invalid markup
- expect(doc.errors).to be_empty
+ expect(first_line_in_markdown(object, attribute, 100, project: project)).to match(expected)
+ end
- # Leading user link
- expect(doc.css('a').length).to eq(1)
- expect(doc.css('a')[0].attr('href')).to eq user_path(user)
- expect(doc.css('a')[0].text).to eq "@#{user.username}"
+ it 'truncates the text with multiple paragraphs' do
+ object = create_object("Paragraph 1\n\nParagraph 2")
+ expected = 'Paragraph 1...'
- expect(doc.content).to eq "@#{user.username}, can you look at this?..."
- end
+ expect(first_line_in_markdown(object, attribute, 100, project: project)).to match(expected)
+ end
- it 'truncates Markdown with emoji properly' do
- text = "foo :wink:\nbar :grinning:"
- actual = first_line_in_markdown(text, 100, project: project)
+ it 'displays the first line of a code block' do
+ object = create_object("```\nCode block\nwith two lines\n```")
+ expected = %r{<pre.+><code><span class="line">Code block\.\.\.</span>\n</code></pre>}
- doc = Nokogiri::HTML.parse(actual)
+ expect(first_line_in_markdown(object, attribute, 100, project: project)).to match(expected)
+ end
- # Make sure we didn't create invalid markup
- # But also account for the 2 errors caused by the unknown `gl-emoji` elements
- expect(doc.errors.length).to eq(2)
+ it 'truncates a single long line of text' do
+ text = 'The quick brown fox jumped over the lazy dog twice' # 50 chars
+ object = create_object(text * 4)
+ expected = (text * 2).sub(/.{3}/, '...')
+
+ expect(first_line_in_markdown(object, attribute, 150, project: project)).to match(expected)
+ end
+
+ it 'preserves a link href when link text is truncated' do
+ text = 'The quick brown fox jumped over the lazy dog' # 44 chars
+ input = "#{text}#{text}#{text} " # 133 chars
+ link_url = 'http://example.com/foo/bar/baz' # 30 chars
+ input << link_url
+ object = create_object(input)
+ expected_link_text = 'http://example...</a>'
+
+ expect(first_line_in_markdown(object, attribute, 150, project: project)).to match(link_url)
+ expect(first_line_in_markdown(object, attribute, 150, project: project)).to match(expected_link_text)
+ end
+
+ it 'preserves code color scheme' do
+ object = create_object("```ruby\ndef test\n 'hello world'\nend\n```")
+ expected = "\n<pre class=\"code highlight js-syntax-highlight ruby\">" \
+ "<code><span class=\"line\"><span class=\"k\">def</span> <span class=\"nf\">test</span>...</span>\n" \
+ "</code></pre>"
+
+ expect(first_line_in_markdown(object, attribute, 150, project: project)).to eq(expected)
+ end
+
+ it 'preserves data-src for lazy images' do
+ object = create_object("![ImageTest](/uploads/test.png)")
+ image_url = "data-src=\".*/uploads/test.png\""
+
+ expect(first_line_in_markdown(object, attribute, 150, project: project)).to match(image_url)
+ end
+
+ context 'labels formatting' do
+ let(:label_title) { 'this should be ~label_1' }
+
+ def create_and_format_label(project)
+ create(:label, title: 'label_1', project: project)
+ object = create_object(label_title, project: project)
- expect(doc.css('gl-emoji').length).to eq(2)
- expect(doc.css('gl-emoji')[0].attr('data-name')).to eq 'wink'
- expect(doc.css('gl-emoji')[1].attr('data-name')).to eq 'grinning'
+ first_line_in_markdown(object, attribute, 150, project: project)
+ end
- expect(doc.content).to eq "foo ๐Ÿ˜‰\nbar ๐Ÿ˜€"
+ it 'preserves style attribute for a label that can be accessed by current_user' do
+ project = create(:project, :public)
+
+ expect(create_and_format_label(project)).to match(/span class=.*style=.*/)
+ end
+
+ it 'does not style a label that can not be accessed by current_user' do
+ project = create(:project, :private)
+
+ expect(create_and_format_label(project)).to eq("<p>#{label_title}</p>")
+ end
+ end
+
+ it 'truncates Markdown properly' do
+ object = create_object("@#{user.username}, can you look at this?\nHello world\n")
+ actual = first_line_in_markdown(object, attribute, 100, project: project)
+
+ doc = Nokogiri::HTML.parse(actual)
+
+ # Make sure we didn't create invalid markup
+ expect(doc.errors).to be_empty
+
+ # Leading user link
+ expect(doc.css('a').length).to eq(1)
+ expect(doc.css('a')[0].attr('href')).to eq user_path(user)
+ expect(doc.css('a')[0].text).to eq "@#{user.username}"
+
+ expect(doc.content).to eq "@#{user.username}, can you look at this?..."
+ end
+
+ it 'truncates Markdown with emoji properly' do
+ object = create_object("foo :wink:\nbar :grinning:")
+ actual = first_line_in_markdown(object, attribute, 100, project: project)
+
+ doc = Nokogiri::HTML.parse(actual)
+
+ # Make sure we didn't create invalid markup
+ # But also account for the 2 errors caused by the unknown `gl-emoji` elements
+ expect(doc.errors.length).to eq(2)
+
+ expect(doc.css('gl-emoji').length).to eq(2)
+ expect(doc.css('gl-emoji')[0].attr('data-name')).to eq 'wink'
+ expect(doc.css('gl-emoji')[1].attr('data-name')).to eq 'grinning'
+
+ expect(doc.content).to eq "foo ๐Ÿ˜‰\nbar ๐Ÿ˜€"
+ end
+ end
+
+ context 'when the asked attribute can be redacted' do
+ include_examples 'common markdown examples' do
+ let(:attribute) { :note }
+ def create_object(title, project: project_base)
+ build(:note, note: title, project: project)
+ end
+ end
+ end
+
+ context 'when the asked attribute can not be redacted' do
+ include_examples 'common markdown examples' do
+ let(:attribute) { :body }
+ def create_object(title, project: project_base)
+ issue = build(:issue, title: title)
+ build(:todo, :done, project: project_base, author: user, target: issue)
+ end
+ end
end
end