summaryrefslogtreecommitdiff
path: root/spec/presenters/blob_presenter_spec.rb
blob: 95db2ba6a0dfdd1ab4e2dcd4ef2ccb578435a6d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'spec_helper'

describe BlobPresenter, :seed_helper do
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') }

  let(:git_blob) do
    Gitlab::Git::Blob.find(
      repository,
      'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
      'files/ruby/regex.rb'
    )
  end
  let(:blob) { Blob.new(git_blob) }

  describe '.web_url' do
    let(:project) { create(:project, :repository) }
    let(:repository) { project.repository }
    let(:blob) { Gitlab::Graphql::Representation::TreeEntry.new(repository.tree.blobs.first, repository) }

    subject { described_class.new(blob) }

    it { expect(subject.web_url).to eq("http://localhost/#{project.full_path}/blob/#{blob.commit_id}/#{blob.path}") }
  end

  describe '#highlight' do
    subject { described_class.new(blob) }

    it 'returns highlighted content' do
      expect(Gitlab::Highlight)
        .to receive(:highlight)
        .with(
          'files/ruby/regex.rb',
          git_blob.data,
          since: nil,
          plain: nil,
          language: nil
        )

      subject.highlight
    end

    it 'returns plain content when :plain is true' do
      expect(Gitlab::Highlight)
        .to receive(:highlight)
        .with(
          'files/ruby/regex.rb',
          git_blob.data,
          since: nil,
          plain: true,
          language: nil
        )

      subject.highlight(plain: true)
    end

    context '"since" and "to" are present' do
      before do
        allow(git_blob)
          .to receive(:data)
          .and_return("line one\nline two\nline 3\nline 4")
      end

      it 'returns limited highlighted content' do
        expect(Gitlab::Highlight)
          .to receive(:highlight)
          .with(
            'files/ruby/regex.rb',
            "line two\nline 3\n",
            since: 2,
            language: nil,
            plain: nil
          )

        subject.highlight(since: 2, to: 3)
      end
    end

    context 'gitlab-language contains a match' do
      before do
        allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
      end

      it 'passes language to inner call' do
        expect(Gitlab::Highlight)
          .to receive(:highlight)
          .with(
            'files/ruby/regex.rb',
            git_blob.data,
            since: nil,
            plain: nil,
            language: 'ruby'
          )

        subject.highlight
      end
    end
  end
end