spec/presenters/document_presenter_spec.rb in blacklight-6.2.0 vs spec/presenters/document_presenter_spec.rb in blacklight-6.3.0
- old
+ new
@@ -1,15 +1,17 @@
# frozen_string_literal: true
require 'spec_helper'
describe Blacklight::DocumentPresenter do
include Capybara::RSpecMatchers
+ let(:show_presenter) { Blacklight::ShowPresenter.new(document, request_context, config) }
+ let(:index_presenter) { Blacklight::IndexPresenter.new(document, request_context, config) }
let(:request_context) { double }
let(:config) { Blacklight::Configuration.new }
subject { presenter }
- let(:presenter) { Blacklight::DocumentPresenter.new(document, request_context, config) }
+ let(:presenter) { described_class.new(document, request_context, config) }
let(:parameter_class) { ActionController::Parameters }
let(:params) { parameter_class.new }
let(:search_state) { Blacklight::SearchState.new(params, config) }
let(:document) do
@@ -20,10 +22,11 @@
'mnbv' => 'document mnbv value')
end
before do
allow(request_context).to receive(:search_state).and_return(search_state)
+ allow(Deprecation).to receive(:warn)
end
describe "link_rel_alternates" do
before do
class MockDocument
@@ -48,10 +51,11 @@
end
allow(request_context).to receive(:polymorphic_url) do |_, opts|
"url.#{opts[:format]}"
end
+ allow(request_context).to receive(:show_presenter).and_return(show_presenter)
end
let(:document) { MockDocument.new(id: "MOCK_ID1") }
context "with no arguments" do
@@ -110,82 +114,89 @@
config.add_index_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg
config.add_index_field 'alias', field: 'qwer'
config.add_index_field 'with_default', default: 'value'
end
end
- it "should check for an explicit value" do
+ before do
+ allow(request_context).to receive(:index_presenter).and_return(index_presenter)
+ end
+ it "checks for an explicit value" do
value = subject.render_index_field_value 'asdf', :value => 'asdf'
expect(value).to eq 'asdf'
end
- it "should check for a helper method to call" do
+ it "checks for a helper method to call" do
allow(request_context).to receive(:render_asdf_index_field).and_return('custom asdf value')
value = subject.render_index_field_value 'asdf'
expect(value).to eq 'custom asdf value'
end
- it "should check for a link_to_search" do
+ it "checks for a link_to_search" do
allow(request_context).to receive(:search_action_path).with('f' => { 'link_to_search_true' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.render_index_field_value 'link_to_search_true'
expect(value).to eq 'bar'
end
- it "should check for a link_to_search with a field name" do
+ it "checks for a link_to_search with a field name" do
allow(request_context).to receive(:search_action_path).with('f' => { 'some_field' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.render_index_field_value 'link_to_search_named'
expect(value).to eq 'bar'
end
- it "should gracefully handle when no highlight field is available" do
- allow(document).to receive(:has_highlight_field?).and_return(false)
- value = subject.render_index_field_value 'highlight'
- expect(value).to be_blank
+ context "when no highlight field is available" do
+ before do
+ allow(document).to receive(:has_highlight_field?).and_return(false)
+ end
+ let(:value) { subject.render_index_field_value 'highlight' }
+ it "is blank" do
+ expect(value).to be_blank
+ end
end
- it "should check for a highlighted field" do
+ it "checks for a highlighted field" do
allow(document).to receive(:has_highlight_field?).and_return(true)
allow(document).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
value = subject.render_index_field_value 'highlight'
expect(value).to eq '<em>highlight</em>'
end
- it "should check the document field value" do
+ it "checks the document field value" do
value = subject.render_index_field_value 'qwer'
expect(value).to eq 'document qwer value'
end
- it "should work with index fields that aren't explicitly defined" do
+ it "works with index fields that aren't explicitly defined" do
value = subject.render_index_field_value 'mnbv'
expect(value).to eq 'document mnbv value'
end
- it "should call an accessor on the solr document" do
+ it "calls an accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.render_index_field_value 'solr_doc_accessor'
expect(value).to eq "123"
end
- it "should call an explicit accessor on the solr document" do
+ it "calls an explicit accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.render_index_field_value 'explicit_accessor'
expect(value).to eq "123"
end
- it "should call an accessor on the solr document with the field as an argument" do
+ it "calls an accessor on the solr document with the field as an argument" do
allow(document).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123")
value = subject.render_index_field_value 'explicit_accessor_with_arg'
expect(value).to eq "123"
end
- it "should support solr field configuration" do
+ it "supports solr field configuration" do
value = subject.render_index_field_value 'alias'
expect(value).to eq "document qwer value"
end
- it "should support default values in the field configuration" do
+ it "supports default values in the field configuration" do
value = subject.render_index_field_value 'with_default'
expect(value).to eq "value"
end
end
@@ -202,10 +213,14 @@
config.add_show_field 'explicit_array_accessor', :accessor => [:solr_doc_accessor, :some_method]
config.add_show_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg
end
end
+ before do
+ allow(request_context).to receive(:show_presenter).and_return(show_presenter)
+ end
+
it 'html-escapes values' do
value = subject.render_document_show_field_value 'asdf', value: '<b>val1</b>'
expect(value).to eq '<b>val1</b>'
end
@@ -217,43 +232,47 @@
it 'joins multivalued valued fields' do
value = subject.render_document_show_field_value 'asdf', value: ['a', 'b', 'c']
expect(value).to eq 'a, b, and c'
end
- it "should check for an explicit value" do
+ it "checks for an explicit value" do
expect(request_context).to_not receive(:render_asdf_document_show_field)
value = subject.render_document_show_field_value 'asdf', :value => 'val1'
expect(value).to eq 'val1'
end
- it "should check for a helper method to call" do
+ it "checks for a helper method to call" do
allow(request_context).to receive(:render_asdf_document_show_field).and_return('custom asdf value')
value = subject.render_document_show_field_value 'asdf'
expect(value).to eq 'custom asdf value'
end
- it "should check for a link_to_search" do
+ it "checks for a link_to_search" do
allow(request_context).to receive(:search_action_path).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.render_document_show_field_value 'link_to_search_true'
expect(value).to eq 'bar'
end
- it "should check for a link_to_search with a field name" do
+ it "checks for a link_to_search with a field name" do
allow(request_context).to receive(:search_action_path).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.render_document_show_field_value 'link_to_search_named'
expect(value).to eq 'bar'
end
- it "should gracefully handle when no highlight field is available" do
- allow(document).to receive(:has_highlight_field?).and_return(false)
- value = subject.render_document_show_field_value 'highlight'
- expect(value).to be_blank
+ context "when no highlight field is available" do
+ before do
+ allow(document).to receive(:has_highlight_field?).and_return(false)
+ end
+ let(:value) { subject.render_document_show_field_value 'highlight' }
+ it "is blank" do
+ expect(value).to be_blank
+ end
end
- it "should check for a highlighted field" do
+ it "checks for a highlighted field" do
allow(document).to receive(:has_highlight_field?).and_return(true)
allow(document).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
value = subject.render_document_show_field_value 'highlight'
expect(value).to eq '<em>highlight</em>'
end
@@ -263,123 +282,148 @@
allow(document).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe, '<em>other highlight</em>'.html_safe])
value = subject.render_document_show_field_value 'highlight'
expect(value).to eq '<em>highlight</em> and <em>other highlight</em>'
end
- it "should check the document field value" do
+ it "checks the document field value" do
value = subject.render_document_show_field_value 'qwer'
expect(value).to eq 'document qwer value'
end
- it "should work with show fields that aren't explicitly defined" do
+ it "works with show fields that aren't explicitly defined" do
value = subject.render_document_show_field_value 'mnbv'
expect(value).to eq 'document mnbv value'
end
- it "should call an accessor on the solr document" do
+ it "calls an accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.render_document_show_field_value 'solr_doc_accessor'
expect(value).to eq "123"
end
- it "should call an explicit accessor on the solr document" do
+ it "calls an explicit accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.render_document_show_field_value 'explicit_accessor'
expect(value).to eq "123"
end
- it "should call an explicit array-style accessor on the solr document" do
+ it "calls an explicit array-style accessor on the solr document" do
allow(document).to receive_message_chain(:solr_doc_accessor, some_method: "123")
value = subject.render_document_show_field_value 'explicit_array_accessor'
expect(value).to eq "123"
end
- it "should call an accessor on the solr document with the field as an argument" do
+ it "calls an accessor on the solr document with the field as an argument" do
allow(document).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123")
value = subject.render_document_show_field_value 'explicit_accessor_with_arg'
expect(value).to eq "123"
end
end
describe "render_field_value" do
- it "should join and html-safe values" do
+ before do
+ expect(Deprecation).to receive(:warn)
+ end
+ it "joins and html-safe values" do
expect(subject.render_field_value(['a', 'b'])).to eq "a and b"
end
- it "should use the field_config.separator_options from the Blacklight field configuration" do
- expect(subject.render_field_value(['c', 'd'], double(separator: nil, itemprop: nil, separator_options: { two_words_connector: '; '}))).to eq "c; d"
+ context "with separator_options" do
+ let(:field_config) { double(to_h: { field: 'foo', separator: nil, itemprop: nil, separator_options: { two_words_connector: '; '}}) }
+ it "uses the field_config.separator_options" do
+ expect(subject.render_field_value(['c', 'd'], field_config)).to eq "c; d"
+ end
end
- it "should include schema.org itemprop attributes" do
- expect(subject.render_field_value('a', double(separator: nil, itemprop: 'some-prop', separator_options: nil))).to have_selector("span[@itemprop='some-prop']", :text => "a")
+ context "with itemprop attributes" do
+ let(:field_config) { double(to_h: { field: 'bar', separator: nil, itemprop: 'some-prop', separator_options: nil }) }
+ it "includes schema.org itemprop attributes" do
+ expect(subject.render_field_value('a', field_config)).to have_selector("span[@itemprop='some-prop']", :text => "a")
+ end
end
end
describe "#document_heading" do
- it "should fallback to an id" do
- allow(document).to receive(:id).and_return "xyz"
+ before do
+ allow(request_context).to receive(:show_presenter).and_return(show_presenter)
+ end
+ it "falls back to an id" do
+ allow(document).to receive(:[]).with('id').and_return "xyz"
expect(subject.document_heading).to eq document.id
end
- it "should return the value of the field" do
+ it "returns the value of the field" do
config.show.title_field = :x
allow(document).to receive(:has?).with(:x).and_return(true)
allow(document).to receive(:[]).with(:x).and_return("value")
expect(subject.document_heading).to eq "value"
end
- it "should return the first present value" do
+ it "returns the first present value" do
config.show.title_field = [:x, :y]
allow(document).to receive(:has?).with(:x).and_return(false)
allow(document).to receive(:has?).with(:y).and_return(true)
allow(document).to receive(:[]).with(:y).and_return("value")
expect(subject.document_heading).to eq "value"
end
end
describe "#document_show_html_title" do
- it "should fallback to an id" do
- allow(document).to receive(:id).and_return "xyz"
+ before do
+ allow(request_context).to receive(:show_presenter).and_return(show_presenter)
+ end
+ it "falls back to an id" do
+ allow(document).to receive(:[]).with('id').and_return "xyz"
expect(subject.document_show_html_title).to eq document.id
end
- it "should return the value of the field" do
+ it "returns the value of the field" do
config.show.html_title_field = :x
allow(document).to receive(:has?).with(:x).and_return(true)
- allow(document).to receive(:[]).with(:x).and_return("value")
+ allow(document).to receive(:fetch).with(:x, nil).and_return("value")
expect(subject.document_show_html_title).to eq "value"
end
- it "should return the first present value" do
+ it "returns the first present value" do
config.show.html_title_field = [:x, :y]
allow(document).to receive(:has?).with(:x).and_return(false)
allow(document).to receive(:has?).with(:y).and_return(true)
- allow(document).to receive(:[]).with(:y).and_return("value")
+ allow(document).to receive(:fetch).with(:y, nil).and_return("value")
expect(subject.document_show_html_title).to eq "value"
end
end
describe '#get_field_values' do
+ let(:field_config) { double }
+ let(:options) { double }
+ it "calls field_values" do
+ expect(Deprecation).to receive(:warn)
+ expect(presenter).to receive(:field_values).with(field_config, options)
+ presenter.get_field_values('name', field_config, options)
+ end
+ end
+
+ describe '#field_values' do
context 'for a field with the helper_method option' do
let(:field_name) { 'field_with_helper' }
let(:field_config) { config.add_facet_field 'field_with_helper', helper_method: 'render_field_with_helper' }
before do
document['field_with_helper'] = 'value'
end
- it "should check call the helper method with arguments" do
+ it "checks call the helper method with arguments" do
allow(request_context).to receive(:render_field_with_helper) do |*args|
args.first
end
render_options = { a: 1 }
- options = subject.get_field_values field_name, field_config, a: 1
+ options = subject.field_values field_config, a: 1
expect(options).to include :document, :field, :value, :config, :a
expect(options[:document]).to eq document
expect(options[:field]).to eq 'field_with_helper'
- expect(options[:value]).to eq 'value'
+ expect(options[:value]).to eq ['value']
expect(options[:config]).to eq field_config
expect(options[:a]).to eq 1
end
end
end