Skip to content

Commit

Permalink
add presenter spec
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan committed Oct 20, 2016
1 parent 2c8f74e commit 2c4d3da
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ RSpec/SubjectStub:
# Configuration parameters: IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/presenters/rtl_index_presenter_spec.rb'
- 'spec/presenters/rtl_show_presenter_spec.rb'
13 changes: 10 additions & 3 deletions app/presenters/rtl_index_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class RTLIndexPresenter < ::Blacklight::IndexPresenter
# Override the IndexPresenter label method to render
# the label as an Array if it's multivalued.
def label(field_or_string_or_proc, opts = {})
config = Blacklight::Configuration::NullField.new
value = case field_or_string_or_proc
Expand All @@ -16,11 +18,16 @@ def label(field_or_string_or_proc, opts = {})

private

# Checks if the requested field is the title field and if the configured
# display title field exists on the document. If so, it returns the
# display title field value. This method allows pom to properly display
# records with non-standard title fields. This is the case with multivalued
# titles.
def value_from_symbol(field)
default_title_field = @configuration.index.title_field
display_title_field = @configuration.index.display_title_field
default_title_field = @configuration.index.title_field.to_sym
display_title_field = @configuration.index.display_title_field.to_sym

if field.to_s == default_title_field && @document.key?(display_title_field)
if field == default_title_field && @document.key?(display_title_field)
@document[display_title_field]
else
@document[field]
Expand Down
65 changes: 65 additions & 0 deletions spec/presenters/rtl_index_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'rails_helper'

RSpec.describe RTLIndexPresenter do
subject(:presenter) { described_class.new(document, double(blacklight_config: blacklight_config)) }

let(:document) do
{
title: title,
alternate_title: alternate_title
}
end
let(:title) { ['تضيح المقال'] }
let(:alternate_title) { ['a different title'] }
let(:index_config) { double(title_field: 'title', display_title_field: '') }
let(:field_config) { double }
let(:blacklight_config) do
double(
index: index_config,
index_fields: { field: field_config }
)
end

before do
allow(field_config).to receive(:to_h).and_return({})
end

describe '#label' do
context 'when given a single-valued title' do
it 'renders as a String' do
expect(presenter.label(:title)).to eq 'تضيح المقال'
end
end

context 'when given a multivalued title' do
let(:title) { ['تضيح المقال', 'Tawḍīḥ al-maqāl'] }

it 'renders as an Array' do
expect(presenter.label(:title)).to be_an Array
expect(presenter.label(:title)).to include 'Tawḍīḥ al-maqāl'
end
end

context 'when configured with a display title field' do
let(:index_config) { double(title_field: 'title', display_title_field: 'alternate_title') }

it 'renders the display title field' do
expect(presenter.label(:title)).to eq 'a different title'
end
end

context 'when passed a string' do
it 'renders the string as the label' do
label_value = 'a string'
expect(presenter.label(label_value)).to eq 'a string'
end
end

context 'when passed a proc' do
it 'calls the proc' do
label_value = proc { 'a string' }
expect(presenter.label(label_value)).to eq 'a string'
end
end
end
end

0 comments on commit 2c4d3da

Please sign in to comment.