debian-mirror-gitlab/spec/helpers/gitlab_markdown_helper_spec.rb

144 lines
4.8 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2014-09-02 18:07:02 +05:30
describe GitlabMarkdownHelper do
include ApplicationHelper
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') }
2015-09-11 14:41:01 +05:30
let(:commit) { project.commit }
2014-09-02 18:07:02 +05:30
let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
let(:snippet) { create(:project_snippet, project: project) }
2015-04-26 12:48:37 +05:30
# Helper expects a current_user method.
let(:current_user) { user }
2014-09-02 18:07:02 +05:30
before do
# Helper expects a @project instance variable
@project = project
end
describe "#gfm" do
it "should forward HTML options to links" do
2015-09-11 14:41:01 +05:30
expect(gfm("Fixed in #{commit.id}", { project: @project }, class: 'foo')).
to have_selector('a.gfm.foo')
2014-09-02 18:07:02 +05:30
end
describe "referencing multiple objects" do
2015-09-11 14:41:01 +05:30
let(:actual) { "#{merge_request.to_reference} -> #{commit.to_reference} -> #{issue.to_reference}" }
2014-09-02 18:07:02 +05:30
it "should link to the merge request" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_merge_request_path(project.namespace, project, merge_request)
expect(gfm(actual)).to match(expected)
2014-09-02 18:07:02 +05:30
end
it "should link to the commit" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_commit_path(project.namespace, project, commit)
expect(gfm(actual)).to match(expected)
2014-09-02 18:07:02 +05:30
end
it "should link to the issue" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_issue_path(project.namespace, project, issue)
expect(gfm(actual)).to match(expected)
2014-09-02 18:07:02 +05:30
end
end
end
2015-09-11 14:41:01 +05:30
describe '#link_to_gfm' do
2015-04-26 12:48:37 +05:30
let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) }
2014-09-02 18:07:02 +05:30
let(:issues) { create_list(:issue, 2, project: project) }
2015-09-11 14:41:01 +05:30
it 'should handle references nested in links with all the text' do
actual = link_to_gfm("This should finally fix #{issues[0].to_reference} and #{issues[1].to_reference} for real", commit_path)
doc = Nokogiri::HTML.parse(actual)
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
# Make sure we didn't create invalid markup
expect(doc.errors).to be_empty
2014-09-02 18:07:02 +05:30
# Leading commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[0].attr('href')).to eq commit_path
expect(doc.css('a')[0].text).to eq 'This should finally fix '
2014-09-02 18:07:02 +05:30
# First issue link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[1].attr('href')).
to eq namespace_project_issue_path(project.namespace, project, issues[0])
expect(doc.css('a')[1].text).to eq issues[0].to_reference
2014-09-02 18:07:02 +05:30
# Internal commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[2].attr('href')).to eq commit_path
expect(doc.css('a')[2].text).to eq ' and '
2014-09-02 18:07:02 +05:30
# Second issue link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[3].attr('href')).
to eq namespace_project_issue_path(project.namespace, project, issues[1])
expect(doc.css('a')[3].text).to eq issues[1].to_reference
2014-09-02 18:07:02 +05:30
# Trailing commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[4].attr('href')).to eq commit_path
expect(doc.css('a')[4].text).to eq ' for real'
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
it 'should forward HTML options' do
2014-09-02 18:07:02 +05:30
actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
2015-09-11 14:41:01 +05:30
doc = Nokogiri::HTML.parse(actual)
expect(doc.css('a')).to satisfy do |v|
# 'foo' gets added to all links
v.all? { |a| a.attr('class').match(/foo$/) }
end
2014-09-02 18:07:02 +05:30
end
it "escapes HTML passed in as the body" do
2015-09-11 14:41:01 +05:30
actual = "This is a <h1>test</h1> - see #{issues[0].to_reference}"
2015-04-26 12:48:37 +05:30
expect(link_to_gfm(actual, commit_path)).
to match('&lt;h1&gt;test&lt;/h1&gt;')
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
it 'ignores reference links when they are the entire body' do
text = issues[0].to_reference
act = link_to_gfm(text, '/foo')
expect(act).to eq %Q(<a href="/foo">#{issues[0].to_reference}</a>)
2014-09-02 18:07:02 +05:30
end
end
2015-09-11 14:41:01 +05:30
describe '#render_wiki_content' do
2014-09-02 18:07:02 +05:30
before do
@wiki = double('WikiPage')
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:content).and_return('wiki content')
2014-09-02 18:07:02 +05:30
end
it "should use GitLab Flavored Markdown for markdown files" do
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:format).and_return(:markdown)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(helper).to receive(:markdown).with('wiki content')
2014-09-02 18:07:02 +05:30
helper.render_wiki_content(@wiki)
end
2015-09-11 14:41:01 +05:30
it "should use Asciidoctor for asciidoc files" do
allow(@wiki).to receive(:format).and_return(:asciidoc)
expect(helper).to receive(:asciidoc).with('wiki content')
helper.render_wiki_content(@wiki)
end
2014-09-02 18:07:02 +05:30
it "should use the Gollum renderer for all other file types" do
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:format).and_return(:rdoc)
2014-09-02 18:07:02 +05:30
formatted_content_stub = double('formatted_content')
2015-04-26 12:48:37 +05:30
expect(formatted_content_stub).to receive(:html_safe)
allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub)
2014-09-02 18:07:02 +05:30
helper.render_wiki_content(@wiki)
end
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
describe 'random_markdown_tip' do
it 'returns a random Markdown tip' do
stub_const("#{described_class}::MARKDOWN_TIPS", ['Random tip'])
expect(random_markdown_tip).to eq 'Random tip'
2015-04-26 12:48:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
end