require 'helper'
require 'lib/view_test_process'
class I18nTest < WillPaginate::ViewTestCase
## basic pagination ##
def setup
clear_translations
super
end
def teardown
clear_translations
super
end
def clear_translations
translations = ::I18n.backend.send(:instance_variable_get, :@translations)
translations.clear unless translations.nil?
WillPaginate::I18n.append_translations_load_path
::I18n.backend.load_translations(*::I18n.load_path)
end
def test_will_paginate_with_default_previous_label
paginate({ :page => 2 }) do
assert_select 'a[href]' do |elements|
assert_select elements.first, 'a', '« Previous'
end
end
end
def test_will_paginate_with_custom_global_previous_label
I18n.backend.store_translations :en, :will_paginate => { :previous_label => '<~ Prev' }
paginate({ :page => 2 }) do
assert_select 'a[href]' do |elements|
assert_select elements.first, 'a', '<~ Prev'
end
end
end
def test_will_paginate_with_default_next_label
paginate({ :page => 2 }) do
assert_select 'a[href]' do |elements|
assert_select elements.last, 'a', 'Next »'
end
end
end
def test_will_paginate_with_custom_global_next_label
I18n.backend.store_translations :en, :will_paginate => { :next_label => 'Next ~>' }
paginate({ :page => 2 }) do
assert_select 'a[href]' do |elements|
assert_select elements.last, 'a', 'Next ~>'
end
end
end
def test_will_paginate_with_default_gap_marker
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0) do
assert_select 'span.gap', '…'
end
end
def test_will_paginate_with_custom_global_gap_marker
I18n.backend.store_translations :en, :will_paginate => { :gap_marker => '~~' }
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0) do
assert_select 'abbr', '~~'
end
end
def test_default_page_entries_info_0
@template = '<%= page_entries_info collection %>'
array = []
paginate array.paginate
assert_equal "No entries found", @html_result
end
def test_default_page_entries_info_1
@template = '<%= page_entries_info collection %>'
array = ['Foo']
paginate array.paginate
assert_equal "Displaying 1 string", @html_result
end
def test_default_page_entries_info_all
@template = '<%= page_entries_info collection %>'
array = ['Foo', 'Bar']
paginate array.paginate({ :per_page => array.size + 1 })
assert_equal "Displaying all 2 strings", @html_result
end
def test_default_page_entries_info_n_to_m_of_x
@template = '<%= page_entries_info collection %>'
array = ('a'..'z').to_a
paginate array.paginate(:page => 2, :per_page => 5)
assert_equal %{Displaying strings 6 - 10 of 26 in total},
@html_result
paginate array.paginate(:page => 7, :per_page => 4)
assert_equal %{Displaying strings 25 - 26 of 26 in total},
@html_result
end
def test_page_entries_info_with_custom_globals
I18n.backend.store_translations :en, :will_paginate => {
:page_entries_info => {
:zero => 'No {{pluralized_entry_name}}',
:one => 'One {{entry_name}}:',
:all => 'All {{total_count}} {{pluralized_entry_name}}:',
:n_to_m_of_x => '{{pluralized_entry_name}} {{start_count}} to {{end_count}} of {{total_count}}:'
}
}
@template = '<%= page_entries_info collection %>'
paginate [].paginate
assert_equal 'No entries', @html_result
paginate ['Baz'].paginate
assert_equal 'One string:', @html_result
paginate ['Wonderment', 'Traction'].paginate
assert_equal 'All 2 strings:', @html_result
paginate ('a'..'z').to_a.paginate(:page => 3, :per_page => 3)
assert_equal 'strings 7 to 9 of 26:', @html_result
end
def test_page_entries_info_uses_human_name
I18n.backend.store_translations :en, :activerecord => {
:models => {
:developer => {
:one => 'Code Monkey',
:other => 'Code Monkeys'
}
}
}
@template = '<%= page_entries_info collection %>'
paginate [Developer.new].paginate
assert_equal 'Displaying 1 Code Monkey', @html_result
paginate [Developer.new, Developer.new].paginate
assert_equal 'Displaying all 2 Code Monkeys', @html_result
end
uses_mocha 'class name' do
def test_page_entries_info_with_longer_class_name
@template = '<%= page_entries_info collection %>'
collection = ('a'..'z').to_a.paginate
collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
paginate collection
assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
end
end
def test_previous_label_deprecated
assert_deprecated ':previous_label' do
paginate({ :page => 2 }, :previous_label => 'Prev') do
assert_select 'a[href]:first-child', 'Prev'
end
end
end
def test_prev_label_deprecated
assert_deprecated ':prev_label' do
paginate({ :page => 2 }, :prev_label => 'Prev') do
assert_select 'a[href]:first-child', 'Prev'
end
end
end
def test_setting_gap_marker_manually_is_deprecated
assert_deprecated 'gap' do
renderer = WillPaginate::LinkRenderer.new
renderer.gap_marker = '~~'
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
assert_select 'span.my-gap', '~~'
end
end
end
def test_entry_name_is_deprecated
assert_deprecated ':entry_name' do
@template = '<%= page_entries_info collection, :entry_name => "author" %>'
entries = []
paginate(entries.paginate)
assert_equal %{No authors found}, @html_result
end
end
end