BigW Consortium Gitlab

gitlab_markdown_helper_spec.rb 4.69 KB
Newer Older
1
require 'spec_helper'
Riyad Preukschas committed
2

randx committed
3
describe GitlabMarkdownHelper do
4
  include ApplicationHelper
Douwe Maan committed
5

6
  let!(:project) { create(:project) }
7

8
  let(:user)          { create(:user, username: 'gfm') }
9
  let(:commit)        { project.commit }
10
  let(:issue)         { create(:issue, project: project) }
11
  let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
Andrew8xx8 committed
12
  let(:snippet)       { create(:project_snippet, project: project) }
13

Douwe Maan committed
14 15 16
  # Helper expects a current_user method.
  let(:current_user) { user }

Riyad Preukschas committed
17
  before do
18 19
    # Helper expects a @project instance variable
    @project = project
Riyad Preukschas committed
20 21
  end

22
  describe "#markdown" do
23
    describe "referencing multiple objects" do
24
      let(:actual) { "#{merge_request.to_reference} -> #{commit.to_reference} -> #{issue.to_reference}" }
25 26

      it "should link to the merge request" do
Vinnie Okada committed
27
        expected = namespace_project_merge_request_path(project.namespace, project, merge_request)
28
        expect(markdown(actual)).to match(expected)
Riyad Preukschas committed
29 30
      end

31
      it "should link to the commit" do
Vinnie Okada committed
32
        expected = namespace_project_commit_path(project.namespace, project, commit)
33
        expect(markdown(actual)).to match(expected)
Riyad Preukschas committed
34 35
      end

36
      it "should link to the issue" do
Vinnie Okada committed
37
        expected = namespace_project_issue_path(project.namespace, project, issue)
38
        expect(markdown(actual)).to match(expected)
Riyad Preukschas committed
39 40
      end
    end
41
  end
Riyad Preukschas committed
42

43
  describe '#link_to_gfm' do
Vinnie Okada committed
44
    let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) }
45
    let(:issues)      { create_list(:issue, 2, project: project) }
Riyad Preukschas committed
46

47
    it 'should handle references nested in links with all the text' do
48
      actual = link_to_gfm("This should finally fix #{issues[0].to_reference} and #{issues[1].to_reference} for real", commit_path)
49
      doc = Nokogiri::HTML.parse(actual)
Riyad Preukschas committed
50

51 52 53
      # Make sure we didn't create invalid markup
      expect(doc.errors).to be_empty

54
      # Leading commit link
55 56
      expect(doc.css('a')[0].attr('href')).to eq commit_path
      expect(doc.css('a')[0].text).to eq 'This should finally fix '
Riyad Preukschas committed
57

58
      # First issue link
59 60
      expect(doc.css('a')[1].attr('href')).
        to eq namespace_project_issue_path(project.namespace, project, issues[0])
61
      expect(doc.css('a')[1].text).to eq issues[0].to_reference
62

63
      # Internal commit link
64 65
      expect(doc.css('a')[2].attr('href')).to eq commit_path
      expect(doc.css('a')[2].text).to eq ' and '
66

67
      # Second issue link
68 69
      expect(doc.css('a')[3].attr('href')).
        to eq namespace_project_issue_path(project.namespace, project, issues[1])
70
      expect(doc.css('a')[3].text).to eq issues[1].to_reference
71 72

      # Trailing commit link
73 74
      expect(doc.css('a')[4].attr('href')).to eq commit_path
      expect(doc.css('a')[4].text).to eq ' for real'
75 76
    end

77 78 79 80 81 82 83 84 85 86
    it 'should forward HTML options' do
      actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
      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
    end

87
    it "escapes HTML passed in as the body" do
88
      actual = "This is a <h1>test</h1> - see #{issues[0].to_reference}"
89 90
      expect(link_to_gfm(actual, commit_path)).
        to match('&lt;h1&gt;test&lt;/h1&gt;')
91
    end
92 93 94 95 96 97

    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>)
    end
98
  end
99

100
  describe '#render_wiki_content' do
101
    before do
skv committed
102
      @wiki = double('WikiPage')
103
      allow(@wiki).to receive(:content).and_return('wiki content')
104 105
    end

106
    it "should use GitLab Flavored Markdown for markdown files" do
107
      allow(@wiki).to receive(:format).and_return(:markdown)
108

109
      expect(helper).to receive(:markdown).with('wiki content')
110 111 112 113

      helper.render_wiki_content(@wiki)
    end

114 115 116 117 118 119 120 121
    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

122
    it "should use the Gollum renderer for all other file types" do
123
      allow(@wiki).to receive(:format).and_return(:rdoc)
skv committed
124
      formatted_content_stub = double('formatted_content')
125 126
      expect(formatted_content_stub).to receive(:html_safe)
      allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub)
127 128 129 130

      helper.render_wiki_content(@wiki)
    end
  end
131 132 133 134

  describe 'random_markdown_tip' do
    it 'returns a random Markdown tip' do
      stub_const("#{described_class}::MARKDOWN_TIPS", ['Random tip'])
Darby committed
135
      expect(random_markdown_tip).to eq 'Random tip'
136 137
    end
  end
Riyad Preukschas committed
138
end