2016-10-06 17:17:11 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Banzai::Renderer do
|
2017-04-10 14:36:30 -04:00
|
|
|
def fake_object(fresh:)
|
|
|
|
object = double('object')
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(true)
|
2017-04-10 14:36:30 -04:00
|
|
|
allow(object).to receive(:cached_html_up_to_date?).with(:field).and_return(fresh)
|
|
|
|
allow(object).to receive(:cached_html_for).with(:field).and_return('field_html')
|
2016-10-06 17:17:11 -04:00
|
|
|
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
describe '#render_field' do
|
2017-05-01 11:13:33 -04:00
|
|
|
let(:renderer) { described_class }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'without cache' do
|
|
|
|
let(:commit) { create(:project, :repository).commit }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'returns cacheless render field' do
|
2017-11-06 11:52:56 -05:00
|
|
|
expect(renderer).to receive(:cacheless_render_field).with(commit, :title, {})
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
renderer.render_field(commit, :title)
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'with cache' do
|
|
|
|
subject { renderer.render_field(object, :field) }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'with a stale cache' do
|
|
|
|
let(:object) { fake_object(fresh: false) }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'caches and returns the result' do
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(object).to receive(:refresh_markdown_cache!)
|
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips database caching on a GitLab read-only instance" do
|
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
|
|
|
expect(object).to receive(:refresh_markdown_cache!)
|
2017-08-23 12:53:29 -04:00
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an up-to-date cache' do
|
|
|
|
let(:object) { fake_object(fresh: true) }
|
|
|
|
|
|
|
|
it 'uses the cache' do
|
|
|
|
expect(object).to receive(:refresh_markdown_cache!).never
|
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|