2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Banzai::Renderer do
|
2017-08-17 22:00:37 +05:30
|
|
|
def fake_object(fresh:)
|
|
|
|
object = double('object')
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def fake_cacheless_object
|
|
|
|
object = double('cacheless object')
|
|
|
|
|
|
|
|
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(false)
|
|
|
|
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
describe '#cache_collection_render' do
|
|
|
|
let(:merge_request) { fake_object(fresh: true) }
|
|
|
|
let(:context) { { cache_key: [merge_request, 'field'], rendered: merge_request.field_html } }
|
|
|
|
|
|
|
|
context 'when an item has a rendered field' do
|
|
|
|
before do
|
|
|
|
allow(merge_request).to receive(:field).and_return('This is the field')
|
|
|
|
allow(merge_request).to receive(:field_html).and_return('This is the field')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not touch redis if the field is in the cache' do
|
|
|
|
expect(Rails).not_to receive(:cache)
|
|
|
|
|
|
|
|
described_class.cache_collection_render([{ text: merge_request.field, context: context }])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#render_field' do
|
|
|
|
let(:renderer) { described_class }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'without cache' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:commit) { fake_cacheless_object }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'returns cacheless render field' do
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(renderer).to receive(:cacheless_render_field).with(commit, :field, {})
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
renderer.render_field(commit, :field)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'with cache' do
|
|
|
|
subject { renderer.render_field(object, :field) }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'with a stale cache' do
|
|
|
|
let(:object) { fake_object(fresh: false) }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'caches and returns the result' do
|
|
|
|
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!)
|
|
|
|
|
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|