2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::View::Presenter::Base do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { double(:project) }
|
|
|
|
let(:presenter_class) do
|
2022-06-21 17:19:12 +05:30
|
|
|
Struct.new(:__subject__).include(described_class)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '.presenter?' do
|
|
|
|
it 'returns true' do
|
|
|
|
presenter = presenter_class.new(project)
|
|
|
|
|
|
|
|
expect(presenter.class).to be_presenter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.presents' do
|
2021-11-18 22:05:49 +05:30
|
|
|
it 'raises an error when symbol is passed' do
|
|
|
|
expect { presenter_class.presents(:foo) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'when the presenter class specifies a custom keyword' do
|
|
|
|
subject(:presenter) { presenter_class.new(project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
presenter_class.class_eval do
|
|
|
|
presents Object, as: :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes the subject with the given keyword' do
|
|
|
|
expect(presenter.foo).to be(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
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
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(presenter_class).not_to receive(:delegator_target)
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
presenter_class.presents(Object, as: :foo)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
describe '#__subject__' do
|
|
|
|
it 'returns the subject' do
|
|
|
|
subject = double
|
|
|
|
presenter = presenter_class.new(subject)
|
|
|
|
|
|
|
|
expect(presenter.__subject__).to be(subject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#can?' do
|
|
|
|
context 'user is not allowed' do
|
|
|
|
it 'returns false' do
|
2017-09-10 17:25:29 +05:30
|
|
|
presenter = presenter_class.new(build_stubbed(:project))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(presenter.can?(nil, :read_project)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is allowed' do
|
|
|
|
it 'returns true' do
|
2017-09-10 17:25:29 +05:30
|
|
|
presenter = presenter_class.new(build_stubbed(:project, :public))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(presenter.can?(nil, :read_project)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context 'subject is overridden' do
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns true' do
|
2017-09-10 17:25:29 +05:30
|
|
|
presenter = presenter_class.new(build_stubbed(:project, :public))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(presenter.can?(nil, :read_project, build_stubbed(:project))).to be_falsy
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
describe '#present' do
|
|
|
|
it 'returns self' do
|
|
|
|
presenter = presenter_class.new(build_stubbed(:project))
|
|
|
|
expect(presenter.present).to eq(presenter)
|
|
|
|
end
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|