Skip to content

Commit

Permalink
add helper spec
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan committed Oct 20, 2016
1 parent a5fa9a0 commit 1fdedf3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ RSpec/MultipleExpectations:
# Configuration parameters: MaxNesting.
RSpec/NestedGroups:
Exclude:
- 'spec/helpers/index_helper_spec.rb'
- 'spec/models/iiif_resource_spec.rb'

# Offense count: 2
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/index_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module IndexHelper
def render_index_document(document)
field = index_presenter(document).label(document_show_link_field(document))
span = []
if field.length == 1 || !field.is_a?(Array)
if !field.is_a?(Array)
span << content_tag(:span, style: 'display: block;', dir: field.dir) do
link_to_document(document, document_show_link_field(document))
link_to(field, url_for_document(document), document_link_params(document, {}))
end
else
field.each do |value|
Expand Down
61 changes: 61 additions & 0 deletions spec/helpers/index_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'rails_helper'

describe IndexHelper do
let(:helper) { TestingHelper.new }
let(:index_presenter) { instance_double('RTLIndexPresenter', class: RTLIndexPresenter) }
let(:document) do
{
title: 'title',
alternate_title: 'alternate_title'
}
end
before do
class TestingHelper
include IndexHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Context
end
end
after do
Object.send(:remove_const, :TestingHelper)
end

describe '#render_index_document' do
before do
allow(helper).to receive(:index_presenter).and_return(index_presenter)
allow(helper).to receive(:document_show_link_field).and_return(:title)
allow(helper).to receive(:url_for_document).and_return('link')
allow(helper).to receive(:document_link_params).and_return({})
allow(index_presenter).to receive(:label).and_return(label)
end

context 'when given a ltr label' do
let(:label) { 'title' }

it 'returns a single ltr span tag' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="ltr"><a href="link">title</a></span>'
end
end

context 'when given a rtl label' do
let(:label) { 'تضيح المقال' }

it 'returns a single rtl span tag' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="rtl"><a href="link">تضيح المقال</a></span>'
end
end

context 'when given a multivalued title' do
let(:label) { ['تضيح المقال', 'title'] }

it 'returns span tags' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="rtl"><a href="link">تضيح المقال</a></span>'\
'<span style="display: block;" dir="ltr"><a href="link">title</a></span>'
end
end
end
end

0 comments on commit 1fdedf3

Please sign in to comment.