test/unit/body_test.rb in table_helper-0.1.0 vs test/unit/body_test.rb in table_helper-0.2.0

- old
+ new

@@ -1,193 +1,229 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class BodyByDefaultTest < Test::Unit::TestCase def setup - header = TableHelper::Header.new([]) - @body = TableHelper::Body.new([], header) + @table = TableHelper::CollectionTable.new([]) + @body = TableHelper::Body.new(@table) end - def test_should_not_alternate_rows - assert_nil @body.alternate_rows + def test_should_have_a_table + assert_equal @table, @body.table end + def test_should_not_alternate + assert_nil @body.alternate + end + def test_should_have_an_empty_caption assert_equal 'No matches found.', @body.empty_caption end end class BodyTest < Test::Unit::TestCase - class Post - attr_accessor :title - - def initialize(title) - @title = title - end + def setup + @table = TableHelper::CollectionTable.new([]) + @body = TableHelper::Body.new(@table) end + def test_should_raise_exception_if_invalid_alternate_specified + assert_raise(ArgumentError) {@body.alternate = :invalid} + end + + def test_should_not_raise_exception_for_odd_alternate + assert_nothing_raised {@body.alternate = :odd} + assert_equal :odd, @body.alternate + end + + def test_should_not_raise_exception_for_even_alternate + assert_nothing_raised {@body.alternate = :even} + assert_equal :even, @body.alternate + end +end + +class BodyWithEmptyCollectionTest < Test::Unit::TestCase def setup - @collection = [ - Post.new('first'), - Post.new('second'), - Post.new('last') - ] - @header = TableHelper::Header.new(@collection) - @body = TableHelper::Body.new(@collection, @header) + @table = TableHelper::CollectionTable.new([]) + @body = TableHelper::Body.new(@table) end - def test_should_raise_exception_if_invalid_alternate_rows_specified - assert_raise(ArgumentError) {@body.alternate_rows = :invalid} + def test_should_have_no_content_if_no_empty_caption + @body.empty_caption = nil + assert_html_equal '<tbody></tbody>', @body.html end - def test_should_not_raise_exception_for_odd_alternate_rows - assert_nothing_raised {@body.alternate_rows = :odd} - assert_equal :odd, @body.alternate_rows + def test_should_show_content_if_empty_caption + expected = <<-end_str + <tbody> + <tr class="ui-collection-empty"> + <td>No matches found.</td> + </tr> + </tbody> + end_str + assert_html_equal expected, @body.html end - def test_should_not_raise_exception_for_even_alternate_rows - assert_nothing_raised {@body.alternate_rows = :even} - assert_equal :even, @body.alternate_rows + def test_should_use_custom_empty_caption_class_if_specified + original_empty_caption_class = TableHelper::Body.empty_caption_class + TableHelper::Body.empty_caption_class = 'ui-collection-empty_caption' + + expected = <<-end_str + <tbody> + <tr class="ui-collection-empty_caption"> + <td>No matches found.</td> + </tr> + </tbody> + end_str + assert_html_equal expected, @body.html + ensure + TableHelper::Body.empty_caption_class = original_empty_caption_class end + def test_should_set_colspan_if_header_has_multiple_columns + @table.header :title, :author_name + + expected = <<-end_str + <tbody> + <tr class="ui-collection-empty"> + <td colspan="2">No matches found.</td> + </tr> + </tbody> + end_str + assert_html_equal expected, @body.html + end +end + +class BodyWithCollectionTest < Test::Unit::TestCase + class Post + attr_accessor :title + + def initialize(title) + @title = title + end + end + + def setup + @collection = [Post.new('first'), Post.new('second'), Post.new('last')] + @table = TableHelper::CollectionTable.new(@collection) + @table.header :title + + @body = TableHelper::Body.new(@table) + end + def test_should_build_row_using_object_location_for_default_index - @header.column :title + build_post = nil + index = nil + @body.each {|row, build_post, index|} @collection.each do |post| - html = @body.build_row(post) do |row, built_post, index| - assert_equal post, built_post - assert_equal @collection.index(post), index - end + html = @body.build_row(post) + assert_equal post, build_post + assert_equal @collection.index(post), index expected = <<-end_str - <tr class="row"> - <td class="title">#{post.title}</td> + <tr class="post ui-collection-result"> + <td class="post-title">#{post.title}</td> </tr> end_str assert_html_equal expected, html end end def test_should_build_row_using_custom_value_for_index - @header.column :title + post = nil + index = nil + @body.each {|row, post, index|} - html = @body.build_row(@collection.first, 1) do |row, post, index| - assert_equal @collection.first, post - assert_equal 1, index - end + html = @body.build_row(@collection.first, 1) + assert_equal @collection.first, post + assert_equal 1, index expected = <<-end_str - <tr class="row"> - <td class="title">first</td> + <tr class="post ui-collection-result"> + <td class="post-title">first</td> </tr> end_str assert_html_equal expected, html end def test_should_build_row_with_missing_cells - @header.column :title - @header.column :author_name + header = @table.header :author_name expected = <<-end_str - <tr class="row"> - <td class="title">first</td> - <td class="author_name empty"></td> + <tr class="post ui-collection-result"> + <td class="post-title">first</td> + <td class="post-author_name ui-state-empty"></td> </tr> end_str assert_html_equal expected, @body.build_row(@collection.first) end - def test_should_build_all_rows - @header.column :title - + def test_should_build_html expected = <<-end_str - <tr class="row"> - <td class="title">first</td> - </tr> - <tr class="row"> - <td class="title">second</td> - </tr> - <tr class="row"> - <td class="title">last</td> - </tr> - end_str - assert_html_equal expected, @body.build - end - - def test_html_should_use_body_tag - @header.column :title - @body.build - - expected = <<-end_str <tbody> - <tr class="row"> - <td class="title">first</td> + <tr class="post ui-collection-result"> + <td class="post-title">first</td> </tr> - <tr class="row"> - <td class="title">second</td> + <tr class="post ui-collection-result"> + <td class="post-title">second</td> </tr> - <tr class="row"> - <td class="title">last</td> + <tr class="post ui-collection-result"> + <td class="post-title">last</td> </tr> </tbody> end_str assert_html_equal expected, @body.html end def test_should_include_custom_attributes_in_body_tag - @header.column :title @body[:class] = 'pretty' - @body.build expected = <<-end_str <tbody class="pretty"> - <tr class="row"> - <td class="title">first</td> + <tr class="post ui-collection-result"> + <td class="post-title">first</td> </tr> - <tr class="row"> - <td class="title">second</td> + <tr class="post ui-collection-result"> + <td class="post-title">second</td> </tr> - <tr class="row"> - <td class="title">last</td> + <tr class="post ui-collection-result"> + <td class="post-title">last</td> </tr> </tbody> end_str assert_html_equal expected, @body.html end end -class BodyWithEmptyCollectionTest < Test::Unit::TestCase +class BodyWithCustomBuilderTest < Test::Unit::TestCase def setup - @collection = [] - @header = TableHelper::Header.new(@collection) - @body = TableHelper::Body.new(@collection, @header) + @collection = [Object.new, Object.new, Object.new] + @table = TableHelper::CollectionTable.new(@collection) + @table.header :index + + @body = TableHelper::Body.new(@table) + @body.each do |row, object, index| + row.index index.to_s + end end - def test_should_show_no_content + def test_should_use_custom_builder expected = <<-end_str - <tr class="no_content"> - <td>No matches found.</td> - </tr> + <tbody> + <tr class="object ui-collection-result"> + <td class="object-index">0</td> + </tr> + <tr class="object ui-collection-result"> + <td class="object-index">1</td> + </tr> + <tr class="object ui-collection-result"> + <td class="object-index">2</td> + </tr> + </tbody> end_str - assert_html_equal expected, @body.build + assert_html_equal expected, @body.html end - - def test_should_be_empty_if_no_empty_caption - @body.empty_caption = nil - assert_html_equal '', @body.build - end - - def test_should_set_colspan_if_header_has_multiple_columns - @header.column :title - @header.column :author_name - - expected = <<-end_str - <tr class="no_content"> - <td colspan="2">No matches found.</td> - </tr> - end_str - assert_html_equal expected, @body.build - end end class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase class Post attr_accessor :title @@ -196,35 +232,31 @@ @title = title end end def setup - @collection = [ - Post.new('first'), - Post.new('second'), - Post.new('last') - ] - @header = TableHelper::Header.new(@collection) - @header.column :title + @collection = [Post.new('first'), Post.new('second'), Post.new('last')] + table = TableHelper::CollectionTable.new(@collection) + table.header :title - @body = TableHelper::Body.new(@collection, @header) - @body.alternate_rows = :even + @body = TableHelper::Body.new(table) + @body.alternate = :even end def test_should_alternate_even_row expected = <<-end_str - <tr class="row alternate"> - <td class="title">first</td> + <tr class="post ui-collection-result ui-state-alternate"> + <td class="post-title">first</td> </tr> end_str assert_html_equal expected, @body.build_row(@collection.first) end def test_should_not_alternate_odd_row expected = <<-end_str - <tr class="row"> - <td class="title">second</td> + <tr class="post ui-collection-result"> + <td class="post-title">second</td> </tr> end_str assert_html_equal expected, @body.build_row(@collection[1]) end end @@ -237,34 +269,30 @@ @title = title end end def setup - @collection = [ - Post.new('first'), - Post.new('second'), - Post.new('last') - ] - @header = TableHelper::Header.new(@collection) - @header.column :title + @collection = [Post.new('first'), Post.new('second'), Post.new('last')] + table = TableHelper::CollectionTable.new(@collection) + table.header :title - @body = TableHelper::Body.new(@collection, @header) - @body.alternate_rows = :odd + @body = TableHelper::Body.new(table) + @body.alternate = :odd end def test_should_alternate_odd_row expected = <<-end_str - <tr class="row alternate"> - <td class="title">second</td> + <tr class="post ui-collection-result ui-state-alternate"> + <td class="post-title">second</td> </tr> end_str assert_html_equal expected, @body.build_row(@collection[1]) end def test_should_not_alternate_even_row expected = <<-end_str - <tr class="row"> - <td class="title">first</td> + <tr class="post ui-collection-result"> + <td class="post-title">first</td> </tr> end_str assert_html_equal expected, @body.build_row(@collection.first) end end