2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-02 20:46:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Banzai::CrossProjectReference do
|
2018-10-02 22:56:51 -04:00
|
|
|
let(:including_class) { Class.new.include(described_class).new }
|
2021-05-11 11:10:20 -04:00
|
|
|
let(:reference_cache) { Banzai::Filter::References::ReferenceCache.new(including_class, {})}
|
2018-10-02 22:56:51 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(including_class).to receive(:context).and_return({})
|
|
|
|
allow(including_class).to receive(:parent_from_ref).and_call_original
|
2021-05-11 11:10:20 -04:00
|
|
|
allow(including_class).to receive(:reference_cache).and_return(reference_cache)
|
2018-10-02 22:56:51 -04:00
|
|
|
end
|
2015-04-02 20:46:43 -04:00
|
|
|
|
2017-12-01 04:21:05 -05:00
|
|
|
describe '#parent_from_ref' do
|
2015-11-19 10:18:13 -05:00
|
|
|
context 'when no project was referenced' do
|
|
|
|
it 'returns the project from context' do
|
|
|
|
project = double
|
2015-09-02 23:45:42 -04:00
|
|
|
|
2018-10-02 22:56:51 -04:00
|
|
|
allow(including_class).to receive(:context).and_return({ project: project })
|
2015-09-02 23:45:42 -04:00
|
|
|
|
2018-10-02 22:56:51 -04:00
|
|
|
expect(including_class.parent_from_ref(nil)).to eq project
|
2015-04-17 06:34:51 -04:00
|
|
|
end
|
2015-11-19 10:18:13 -05:00
|
|
|
end
|
2015-04-17 06:34:51 -04:00
|
|
|
|
2018-03-30 13:27:03 -04:00
|
|
|
context 'when no project was referenced in group context' do
|
|
|
|
it 'returns the group from context' do
|
|
|
|
group = double
|
|
|
|
|
2018-10-02 22:56:51 -04:00
|
|
|
allow(including_class).to receive(:context).and_return({ group: group })
|
2018-03-30 13:27:03 -04:00
|
|
|
|
2018-10-02 22:56:51 -04:00
|
|
|
expect(including_class.parent_from_ref(nil)).to eq group
|
2018-03-30 13:27:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-19 10:18:13 -05:00
|
|
|
context 'when referenced project does not exist' do
|
|
|
|
it 'returns nil' do
|
2018-10-02 22:56:51 -04:00
|
|
|
expect(including_class.parent_from_ref('invalid/reference')).to be_nil
|
2015-04-14 16:04:37 -04:00
|
|
|
end
|
2015-11-19 10:18:13 -05:00
|
|
|
end
|
2015-04-02 20:46:43 -04:00
|
|
|
|
2015-11-19 10:18:13 -05:00
|
|
|
context 'when referenced project exists' do
|
|
|
|
it 'returns the referenced project' do
|
|
|
|
project2 = double('referenced project')
|
2015-04-02 20:46:43 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Project).to receive(:find_by_full_path)
|
|
|
|
.with('cross/reference').and_return(project2)
|
2015-04-14 16:04:37 -04:00
|
|
|
|
2018-10-02 22:56:51 -04:00
|
|
|
expect(including_class.parent_from_ref('cross/reference')).to eq project2
|
2015-04-02 20:46:43 -04:00
|
|
|
end
|
|
|
|
end
|
2021-05-11 11:10:20 -04:00
|
|
|
|
|
|
|
context 'when reference cache is loaded' do
|
|
|
|
let(:project2) { double('referenced project') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(reference_cache).to receive(:cache_loaded?).and_return(true)
|
|
|
|
allow(reference_cache).to receive(:parent_per_reference).and_return({ 'cross/reference' => project2 })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'pulls from the reference cache' do
|
|
|
|
expect(including_class.parent_from_ref('cross/reference')).to eq project2
|
|
|
|
end
|
|
|
|
end
|
2015-04-02 20:46:43 -04:00
|
|
|
end
|
|
|
|
end
|