Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_spec.rb « presenter « view « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7083bd272231955c8d2a752814d55dea6ee8fb9 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::View::Presenter::Base do
  let(:project) { double(:project) }
  let(:presenter_class) do
    Struct.new(:subject).include(described_class)
  end

  describe '.presenter?' do
    it 'returns true' do
      presenter = presenter_class.new(project)

      expect(presenter.class).to be_presenter
    end
  end

  describe '.presents' do
    it 'exposes #subject with the given keyword' do
      presenter_class.presents(Object, as: :foo)
      presenter = presenter_class.new(project)

      expect(presenter.foo).to eq(project)
    end

    it 'raises an error when symbol is passed' do
      expect { presenter_class.presents(:foo) }.to raise_error(ArgumentError)
    end

    context 'when the presenter class inherits Presenter::Delegated' do
      let(:presenter_class) do
        Class.new(::Gitlab::View::Presenter::Delegated) do
          include(::Gitlab::View::Presenter::Base)
        end
      end

      it 'sets the delegator target' do
        expect(presenter_class).to receive(:delegator_target).with(Object)

        presenter_class.presents(Object, as: :foo)
      end
    end

    context 'when the presenter class inherits Presenter::Simple' do
      let(:presenter_class) do
        Class.new(::Gitlab::View::Presenter::Simple) do
          include(::Gitlab::View::Presenter::Base)
        end
      end

      it 'does not set the delegator target' do
        expect(presenter_class).not_to receive(:delegator_target).with(Object)

        presenter_class.presents(Object, as: :foo)
      end
    end
  end

  describe '#can?' do
    context 'user is not allowed' do
      it 'returns false' do
        presenter = presenter_class.new(build_stubbed(:project))

        expect(presenter.can?(nil, :read_project)).to be_falsy
      end
    end

    context 'user is allowed' do
      it 'returns true' do
        presenter = presenter_class.new(build_stubbed(:project, :public))

        expect(presenter.can?(nil, :read_project)).to be_truthy
      end
    end

    context 'subject is overridden' do
      it 'returns true' do
        presenter = presenter_class.new(build_stubbed(:project, :public))

        expect(presenter.can?(nil, :read_project, build_stubbed(:project))).to be_falsy
      end
    end
  end

  describe '#present' do
    it 'returns self' do
      presenter = presenter_class.new(build_stubbed(:project))
      expect(presenter.present).to eq(presenter)
    end
  end

  describe '#url_builder' do
    it 'returns the UrlBuilder instance' do
      presenter = presenter_class.new(project)

      expect(presenter.url_builder).to eq(Gitlab::UrlBuilder.instance)
    end
  end

  describe '#web_url' do
    it 'delegates to the UrlBuilder' do
      presenter = presenter_class.new(project)

      expect(presenter.url_builder).to receive(:build).with(project)

      presenter.web_url
    end
  end

  describe '#web_path' do
    it 'delegates to the UrlBuilder' do
      presenter = presenter_class.new(project)

      expect(presenter.url_builder).to receive(:build).with(project, only_path: true)

      presenter.web_path
    end
  end
end