Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Created January 13, 2009 19:19
Show Gist options
  • Save lmarburger/46571 to your computer and use it in GitHub Desktop.
Save lmarburger/46571 to your computer and use it in GitHub Desktop.
require 'test_helper'
require 'ostruct'
class SomeHelperTest < ActionView::TestCase
tests SomeHelper
context "pager" do
setup { @total_pages = 10 }
should "show pagination form" do
get_pagination
assert_select "form.pagination[action=/search][method=post]" do
assert_select 'input[type=hidden][name=page]'
assert_select 'input[type=submit][name=move_page][value=Previous]'
assert_select 'input[type=submit][name=move_page][value=Next]'
assert_select 'input[type=text][name=go_to_page]'
end
end
should "add \"first\" class on the first page and disable previous button" do
get_pagination(0)
assert_select 'form.pagination.first'
assert_select 'input[type=submit][value=Previous][disabled]'
end
should "add \"last\" class on the last page and disable next button" do
get_pagination(@total_pages)
assert_select 'form.pagination.last'
assert_select 'input[type=submit][value=Next][disabled]'
end
should "not add \"first\" class or disable the previous button elsewhere" do
get_pagination
assert_select 'form.pagination.first', false
assert_select 'input[type=submit][value=Previous][disabled]', false
end
should "not add \"last\" class or disable the next button elsewhere" do
get_pagination
assert_select 'form.pagination.last', false
assert_select 'input[type=submit][value=Next][disabled]', false
end
should "display current page and total" do
get_pagination(page = 5)
assert_select 'form.pagination span.pages span', :text => "Page #{page + 1} of #{@total_pages + 1}"
end
should "include search parameters if they exist" do
flexmock(self).should_receive(:create_hidden_fields_for_search_params).with_no_args.once.and_return('')
get_pagination
end
end
private
def get_pagination(current = 5, search_params = {})
# #pagination is the helper I'm testing.
set_response_body(pagination(current, @total_pages))
end
def set_response_body(value)
@response = OpenStruct.new(:body => value)
end
end
# Monkeypatching so we can write tests for helpers.
class ActionView::TestCase::TestController < ActionController::Base
attr_accessor :params
# Save the current initialize method to call in the new initialize method below.
alias :base_initialize :initialize # unless method_defined?(:base_initialize)
def initialize
base_initialize
#Hack Hack Hack: TestCase doesn't have context of a current url so cheat a bit
@params = {}
send(:initialize_current_url)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment