PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/collection_table_test.rb

https://github.com/eeng/table_helper
Ruby | 228 lines | 189 code | 39 blank | 0 comment | 0 complexity | 5a47afff99109f310843aff5117e648f MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class CollectionTableByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @collection = []
  5. @table = TableHelper::CollectionTable.new(@collection)
  6. end
  7. def test_cellspacing_should_be_zero
  8. assert_equal '0', @table[:cellspacing]
  9. end
  10. def test_cellpadding_should_be_zero
  11. assert_equal '0', @table[:cellpadding]
  12. end
  13. def test_should_have_a_css_class
  14. assert_equal 'ui-collection', @table[:class]
  15. end
  16. def test_should_use_custom_collection_css_class_if_specified
  17. original_collection_class = TableHelper::CollectionTable.collection_class
  18. TableHelper::CollectionTable.collection_class = 'ui-records'
  19. table = TableHelper::CollectionTable.new(@collection)
  20. assert_equal 'ui-records', table[:class]
  21. ensure
  22. TableHelper::CollectionTable.collection_class = original_collection_class
  23. end
  24. def test_should_not_have_a_klass
  25. assert_nil @table.klass
  26. end
  27. def test_should_not_have_an_object_name
  28. assert_nil @table.object_name
  29. end
  30. def test_should_have_a_collection
  31. assert_equal @collection, @table.collection
  32. end
  33. def test_should_have_a_header
  34. assert_not_nil @table.header
  35. assert_instance_of TableHelper::Header, @table.header
  36. end
  37. def test_should_have_a_body
  38. assert_not_nil @table.rows
  39. assert_instance_of TableHelper::Body, @table.rows
  40. end
  41. def test_should_have_a_footer
  42. assert_not_nil @table.footer
  43. assert_instance_of TableHelper::Footer, @table.footer
  44. end
  45. def test_should_be_empty
  46. assert @table.empty?
  47. end
  48. end
  49. class CollectionTableTest < Test::Unit::TestCase
  50. def test_should_accept_block_with_self_as_argument
  51. @args = nil
  52. table = TableHelper::CollectionTable.new([]) {|*args| @args = args}
  53. assert_equal [table], @args
  54. end
  55. end
  56. class CollectionTableWithClassDetectionTest < Test::Unit::TestCase
  57. class Post
  58. end
  59. class Reflection
  60. def klass
  61. Post
  62. end
  63. end
  64. class PostCollection < Array
  65. def proxy_reflection
  66. Reflection.new
  67. end
  68. end
  69. def test_should_not_detect_class_if_collection_is_empty_vanilla_array
  70. table = TableHelper::CollectionTable.new([])
  71. assert_nil table.klass
  72. end
  73. def test_should_detect_class_if_collection_has_objects
  74. table = TableHelper::CollectionTable.new([Post.new])
  75. assert_equal Post, table.klass
  76. end
  77. def test_should_detect_class_if_collection_is_model_proxy
  78. table = TableHelper::CollectionTable.new(PostCollection.new)
  79. assert_equal Post, table.klass
  80. end
  81. end
  82. class CollectionTableWithClassTest < Test::Unit::TestCase
  83. class Post
  84. end
  85. def setup
  86. @table = TableHelper::CollectionTable.new([], Post)
  87. end
  88. def test_should_have_klass
  89. assert_equal Post, @table.klass
  90. end
  91. def test_should_have_object_name
  92. assert_equal 'post', @table.object_name
  93. end
  94. def test_should_include_pluralized_object_name_in_css_class
  95. assert_equal 'posts ui-collection', @table[:class]
  96. end
  97. end
  98. class CollectionTableWithEmptyCollectionTest < Test::Unit::TestCase
  99. def setup
  100. @table = TableHelper::CollectionTable.new([])
  101. end
  102. def test_should_be_empty
  103. assert @table.empty?
  104. end
  105. def test_should_build_html
  106. expected = <<-end_str
  107. <table cellpadding="0" cellspacing="0" class="ui-collection">
  108. <tbody>
  109. <tr class="ui-collection-empty"><td>No matches found.</td></tr>
  110. </tbody>
  111. </table>
  112. end_str
  113. assert_html_equal expected, @table.html
  114. end
  115. end
  116. class CollectionTableWithObjectsTest < Test::Unit::TestCase
  117. def setup
  118. @table = TableHelper::CollectionTable.new([Object.new])
  119. end
  120. def test_should_not_be_empty
  121. assert !@table.empty?
  122. end
  123. end
  124. class CollectionTableHeaderTest < Test::Unit::TestCase
  125. def setup
  126. @table = TableHelper::CollectionTable.new([Object.new])
  127. end
  128. def test_should_not_include_in_html_if_none_specified
  129. expected = <<-end_str
  130. <table cellpadding="0" cellspacing="0" class="objects ui-collection">
  131. <tbody>
  132. <tr class="object ui-collection-result"></tr>
  133. </tbody>
  134. </table>
  135. end_str
  136. assert_html_equal expected, @table.html
  137. end
  138. def test_should_include_in_html_if_specified
  139. @table.header :name, :title
  140. expected = <<-end_str
  141. <table cellpadding="0" cellspacing="0" class="objects ui-collection">
  142. <thead>
  143. <tr>
  144. <th class="object-name" scope="col">Name</th>
  145. <th class="object-title" scope="col">Title</th>
  146. </tr>
  147. </thead>
  148. <tbody>
  149. <tr class="object ui-collection-result">
  150. <td class="object-name ui-state-empty"></td>
  151. <td class="object-title ui-state-empty"></td>
  152. </tr>
  153. </tbody>
  154. </table>
  155. end_str
  156. assert_html_equal expected, @table.html
  157. end
  158. end
  159. class CollectionTableFooterTest < Test::Unit::TestCase
  160. def setup
  161. @table = TableHelper::CollectionTable.new([Object.new])
  162. end
  163. def test_should_not_include_in_html_if_none_specified
  164. expected = <<-end_str
  165. <table cellpadding="0" cellspacing="0" class="objects ui-collection">
  166. <tbody>
  167. <tr class="object ui-collection-result"></tr>
  168. </tbody>
  169. </table>
  170. end_str
  171. assert_html_equal expected, @table.html
  172. end
  173. def test_should_include_in_html_if_specified
  174. @table.footer :total, 1
  175. expected = <<-end_str
  176. <table cellpadding="0" cellspacing="0" class="objects ui-collection">
  177. <tbody>
  178. <tr class="object ui-collection-result"></tr>
  179. </tbody>
  180. <tfoot>
  181. <tr>
  182. <td class="object-total">1</td>
  183. </tr>
  184. </tfoot>
  185. </table>
  186. end_str
  187. assert_html_equal expected, @table.html
  188. end
  189. end