require 'test/helper'
class Admin::SidebarHelperTest < ActiveSupport::TestCase
include Admin::SidebarHelper
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormTagHelper
def setup
default_url_options[:host] = 'test.host'
end
def test_actions
self.expects(:default_actions).returns(['action1', 'action2'])
self.expects(:previous_and_next).returns(['previous', 'next'])
self.expects(:export).returns(['csv', 'pdf'])
output = actions
expected = <<-HTML
Actions
Go to
Export
HTML
assert_equal expected, output
end
def test_default_actions
assert true
end
# FIXME
def test_export
return
@resource = { :class => CustomUser }
params = { :controller => 'admin/custom_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
output = export
expected = ["CSV"]
assert_equal expected, output
end
def test_build_typus_list_with_empty_content_and_empty_header
output = build_typus_list([], :header => nil)
assert output.empty?
end
def test_build_typus_list_with_content_and_header
output = build_typus_list(['item1', 'item2'], :header => "Chunky Bacon")
assert !output.empty?
assert_match /Chunky bacon/, output
end
def test_build_typus_list_with_content_without_header
output = build_typus_list(['item1', 'item2'])
assert !output.empty?
assert_no_match /h2/, output
assert_no_match /\/h2/, output
end
def test_previous_and_next_when_edit
@resource = { :class => TypusUser }
@current_user = typus_users(:admin)
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
output = previous_and_next
assert output.empty?
# Test when there are no records.
typus_user = TypusUser.first
@next = nil
@previous = nil
params = { :controller => 'admin/typus_users', :action => 'edit', :id => typus_user.id }
self.expects(:params).at_least_once.returns(params)
output = previous_and_next
assert output.empty?
# Test when we are on the first item.
typus_user = TypusUser.first
@previous, @next = typus_user.previous_and_next
output = previous_and_next
expected = [ "Next" ]
assert_equal expected, output
# Test when we are on the last item.
typus_user = TypusUser.last
@previous, @next = typus_user.previous_and_next
output = previous_and_next
expected = [ "Previous" ]
assert_equal expected, output
# Test when we are on the middle.
typus_user = TypusUser.find(3)
@previous, @next = typus_user.previous_and_next
output = previous_and_next
expected = [ "Next",
"Previous" ]
assert_equal expected, output
end
def test_previous_and_next_when_show
@resource = { :class => TypusUser }
@current_user = typus_users(:admin)
typus_user = TypusUser.find(3)
params = { :controller => 'admin/typus_users', :action => 'show', :id => typus_user.id }
self.expects(:params).at_least_once.returns(params)
@previous, @next = typus_user.previous_and_next
output = previous_and_next
expected = [ "Next",
"Previous" ]
assert_equal expected, output
end
def test_previous_and_next_with_params
assert true
end
def test_search
@resource = { :class => TypusUser, :self => 'typus_users' }
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
output = search
expected = <<-HTML
Search
Search by first name, last name, email, and role.
HTML
assert_equal expected, output
end
def test_filters
@resource = { :class => TypusUser, :self => 'typus_users' }
@resource[:class].expects(:typus_filters).returns(Array.new)
output = filters
assert output.nil?
end
# TODO: Test filters when @resource[:class].typus_filters returns filters.
#
# Yes, I know, it's an ugly name for a test, but don't know how to
# name this test. Suggestions are welcome. ;)
def test_filters_with_filters
assert true
end
def test_relationship_filter
assert true
end
def test_datetime_filter
@resource = { :class => TypusUser, :self => 'typus_users' }
filter = 'created_at'
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
request = ''
output = datetime_filter(request, filter)
expected = <<-HTML
Created at
HTML
assert_equal expected, output
request = 'created_at=today&page=1'
output = datetime_filter(request, filter)
expected = <<-HTML
Created at
HTML
assert_equal expected, output
end
def test_boolean_filter
@resource = { :class => TypusUser, :self => 'typus_users' }
filter = 'status'
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
# Status is true
request = 'status=true&page=1'
output = boolean_filter(request, filter)
expected = <<-HTML
Status
HTML
assert_equal expected, output
# Status is false
request = 'status=false&page=1'
output = boolean_filter(request, filter)
expected = <<-HTML
Status
HTML
assert_equal expected, output
end
def test_string_filter_when_values_are_strings
@resource = { :class => TypusUser, :self => 'typus_users' }
filter = 'role'
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
# Roles is admin
request = 'role=admin&page=1'
@resource[:class].expects('role').returns(['admin', 'designer', 'editor'])
output = string_filter(request, filter)
expected = <<-HTML
Role
HTML
assert_equal expected, output
# Roles is editor
request = 'role=editor&page=1'
@resource[:class].expects('role').returns(['admin', 'designer', 'editor'])
output = string_filter(request, filter)
expected = <<-HTML
Role
HTML
assert_equal expected, output
end
def test_string_filter_when_values_are_arrays_of_strings
@resource = { :class => TypusUser, :self => 'typus_users' }
filter = 'role'
params = { :controller => 'admin/typus_users', :action => 'index' }
self.expects(:params).at_least_once.returns(params)
request = 'role=admin&page=1'
array = [['Administrador', 'admin'],
['DiseƱador', 'designer'],
['Editor', 'editor']]
@resource[:class].expects('role').returns(array)
output = string_filter(request, filter)
expected = <<-HTML
Role
HTML
assert_equal expected, output
end
def test_string_filter_when_empty_values
@resource = { :class => TypusUser }
filter = 'role'
request = 'role=admin&page=1'
@resource[:class].expects('role').returns([])
output = string_filter(request, filter)
assert output.empty?
end
end