spec/presenters/blacklight/field_presenter_spec.rb in blacklight-7.11.1 vs spec/presenters/blacklight/field_presenter_spec.rb in blacklight-7.12.0

- old
+ new

@@ -1,11 +1,11 @@ # frozen_string_literal: true RSpec.describe Blacklight::FieldPresenter, api: true do subject(:presenter) { described_class.new(request_context, document, field_config, options) } - let(:request_context) { double('View context', search_state: search_state, should_render_field?: true, blacklight_config: config) } + let(:request_context) { double('View context', params: { x: '1' }, search_state: search_state, should_render_field?: true, blacklight_config: config) } let(:document) do SolrDocument.new(id: 1, 'link_to_facet_true' => 'x', 'link_to_facet_named' => 'x', 'qwer' => 'document qwer value', @@ -16,10 +16,18 @@ let(:controller) { double } let(:search_state) { Blacklight::SearchState.new(params, config, controller) } let(:field_config) { config.index_fields[field_name] } let(:field_name) { 'asdf' } + + let(:custom_step) do + Class.new(Blacklight::Rendering::AbstractStep) do + def render + 'Static step' + end + end + end let(:config) do Blacklight::Configuration.new.configure do |config| config.add_index_field 'qwer' config.add_index_field 'asdf', helper_method: :render_asdf_index_field config.add_index_field 'link_to_facet_true', link_to_facet: true @@ -27,12 +35,14 @@ config.add_index_field 'highlight', highlight: true config.add_index_field 'solr_doc_accessor', accessor: true config.add_index_field 'explicit_accessor', accessor: :solr_doc_accessor config.add_index_field 'explicit_array_accessor', accessor: [:solr_doc_accessor, :some_method] config.add_index_field 'explicit_values', values: ->(_config, _doc) { ['some-value'] } + config.add_index_field 'explicit_values_with_context', values: ->(_config, _doc, view_context) { [view_context.params[:x]] } config.add_index_field 'alias', field: 'qwer' config.add_index_field 'with_default', default: 'value' + config.add_index_field 'with_steps', steps: [custom_step] end end describe '#render' do subject(:result) { presenter.render } @@ -152,14 +162,23 @@ context 'when the values lambda is provided' do let(:field_name) { 'explicit_values' } it 'calls the accessors on the return of the preceeding' do + allow(Deprecation).to receive(:warn) expect(subject).to eq 'some-value' end end + context 'when the values lambda is provided and accepts the view contexts' do + let(:field_name) { 'explicit_values_with_context' } + + it 'calls the accessors on the return of the preceeding' do + expect(subject).to eq '1' + end + end + context 'when the field is an alias' do let(:field_name) { 'alias' } it { is_expected.to eq 'document qwer value' } end @@ -168,10 +187,16 @@ let(:field_name) { 'with_default' } it { is_expected.to eq 'value' } end + context 'with steps' do + let(:field_name) { 'with_steps' } + + it { is_expected.to eq 'Static step' } + end + 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' } let(:document) do SolrDocument.new(id: 1, 'field_with_helper' => 'value') @@ -210,11 +235,11 @@ end describe '#render_field?' do subject { presenter.render_field? } - let(:field_config) { double('field config', if: true, unless: false) } + let(:field_config) { double('field config', if: true, unless: false, except_operations: nil) } before do allow(presenter).to receive_messages(document_has_value?: true) end @@ -233,31 +258,31 @@ describe '#any?' do subject { presenter.any? } context 'when the document has the field value' do - let(:field_config) { double(field: 'asdf', highlight: false, accessor: nil, default: nil, values: nil) } + let(:field_config) { double(field: 'asdf', highlight: false, accessor: nil, default: nil, values: nil, except_operations: nil) } before do allow(document).to receive(:fetch).with('asdf', nil).and_return(['value']) end it { is_expected.to be true } end context 'when the document has a highlight field value' do - let(:field_config) { double(field: 'asdf', highlight: true) } + let(:field_config) { double(field: 'asdf', highlight: true, except_operations: nil) } before do allow(document).to receive(:has_highlight_field?).with('asdf').and_return(true) allow(document).to receive(:highlight_field).with('asdf').and_return(['value']) end it { is_expected.to be true } end context 'when the field is a model accessor' do - let(:field_config) { double(field: 'asdf', highlight: false, accessor: true) } + let(:field_config) { double(field: 'asdf', highlight: false, accessor: true, except_operations: nil) } before do allow(document).to receive(:send).with('asdf').and_return(['value']) end