PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/body_test.rb

https://github.com/eeng/table_helper
Ruby | 299 lines | 254 code | 45 blank | 0 comment | 0 complexity | ac64f54ec74eae1f2b6aa7279f1a8869 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class BodyByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @table = TableHelper::CollectionTable.new([])
  5. @body = TableHelper::Body.new(@table)
  6. end
  7. def test_should_have_a_table
  8. assert_equal @table, @body.table
  9. end
  10. def test_should_not_alternate
  11. assert_nil @body.alternate
  12. end
  13. def test_should_have_an_empty_caption
  14. assert_equal 'No matches found.', @body.empty_caption
  15. end
  16. end
  17. class BodyTest < Test::Unit::TestCase
  18. def setup
  19. @table = TableHelper::CollectionTable.new([])
  20. @body = TableHelper::Body.new(@table)
  21. end
  22. def test_should_raise_exception_if_invalid_alternate_specified
  23. assert_raise(ArgumentError) {@body.alternate = :invalid}
  24. end
  25. def test_should_not_raise_exception_for_odd_alternate
  26. assert_nothing_raised {@body.alternate = :odd}
  27. assert_equal :odd, @body.alternate
  28. end
  29. def test_should_not_raise_exception_for_even_alternate
  30. assert_nothing_raised {@body.alternate = :even}
  31. assert_equal :even, @body.alternate
  32. end
  33. end
  34. class BodyWithEmptyCollectionTest < Test::Unit::TestCase
  35. def setup
  36. @table = TableHelper::CollectionTable.new([])
  37. @body = TableHelper::Body.new(@table)
  38. end
  39. def test_should_have_no_content_if_no_empty_caption
  40. @body.empty_caption = nil
  41. assert_html_equal '<tbody></tbody>', @body.html
  42. end
  43. def test_should_show_content_if_empty_caption
  44. expected = <<-end_str
  45. <tbody>
  46. <tr class="ui-collection-empty">
  47. <td>No matches found.</td>
  48. </tr>
  49. </tbody>
  50. end_str
  51. assert_html_equal expected, @body.html
  52. end
  53. def test_should_use_custom_empty_caption_class_if_specified
  54. original_empty_caption_class = TableHelper::Body.empty_caption_class
  55. TableHelper::Body.empty_caption_class = 'ui-collection-empty_caption'
  56. expected = <<-end_str
  57. <tbody>
  58. <tr class="ui-collection-empty_caption">
  59. <td>No matches found.</td>
  60. </tr>
  61. </tbody>
  62. end_str
  63. assert_html_equal expected, @body.html
  64. ensure
  65. TableHelper::Body.empty_caption_class = original_empty_caption_class
  66. end
  67. def test_should_set_colspan_if_header_has_multiple_columns
  68. @table.header :title, :author_name
  69. expected = <<-end_str
  70. <tbody>
  71. <tr class="ui-collection-empty">
  72. <td colspan="2">No matches found.</td>
  73. </tr>
  74. </tbody>
  75. end_str
  76. assert_html_equal expected, @body.html
  77. end
  78. end
  79. class BodyWithCollectionTest < Test::Unit::TestCase
  80. class Post
  81. attr_accessor :title
  82. def initialize(title)
  83. @title = title
  84. end
  85. end
  86. def setup
  87. @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
  88. @table = TableHelper::CollectionTable.new(@collection)
  89. @table.header :title
  90. @body = TableHelper::Body.new(@table)
  91. end
  92. def test_should_build_row_using_object_location_for_default_index
  93. @build_post = nil
  94. @index = nil
  95. @body.each {|row, build_post, index| @build_post, @index = build_post, index}
  96. @collection.each do |post|
  97. html = @body.build_row(post)
  98. assert_equal post, @build_post
  99. assert_equal @collection.index(post), @index
  100. expected = <<-end_str
  101. <tr class="post ui-collection-result">
  102. <td class="post-title">#{post.title}</td>
  103. </tr>
  104. end_str
  105. assert_html_equal expected, html
  106. end
  107. end
  108. def test_should_build_row_using_custom_value_for_index
  109. @post = nil
  110. @index = nil
  111. @body.each {|row, post, index| @post, @index = post, index}
  112. html = @body.build_row(@collection.first, 1)
  113. assert_equal @collection.first, @post
  114. assert_equal 1, @index
  115. expected = <<-end_str
  116. <tr class="post ui-collection-result">
  117. <td class="post-title">first</td>
  118. </tr>
  119. end_str
  120. assert_html_equal expected, html
  121. end
  122. def test_should_build_row_with_missing_cells
  123. header = @table.header :author_name
  124. expected = <<-end_str
  125. <tr class="post ui-collection-result">
  126. <td class="post-title">first</td>
  127. <td class="post-author_name ui-state-empty"></td>
  128. </tr>
  129. end_str
  130. assert_html_equal expected, @body.build_row(@collection.first)
  131. end
  132. def test_should_build_html
  133. expected = <<-end_str
  134. <tbody>
  135. <tr class="post ui-collection-result">
  136. <td class="post-title">first</td>
  137. </tr>
  138. <tr class="post ui-collection-result">
  139. <td class="post-title">second</td>
  140. </tr>
  141. <tr class="post ui-collection-result">
  142. <td class="post-title">last</td>
  143. </tr>
  144. </tbody>
  145. end_str
  146. assert_html_equal expected, @body.html
  147. end
  148. def test_should_include_custom_attributes_in_body_tag
  149. @body[:class] = 'pretty'
  150. expected = <<-end_str
  151. <tbody class="pretty">
  152. <tr class="post ui-collection-result">
  153. <td class="post-title">first</td>
  154. </tr>
  155. <tr class="post ui-collection-result">
  156. <td class="post-title">second</td>
  157. </tr>
  158. <tr class="post ui-collection-result">
  159. <td class="post-title">last</td>
  160. </tr>
  161. </tbody>
  162. end_str
  163. assert_html_equal expected, @body.html
  164. end
  165. end
  166. class BodyWithCustomBuilderTest < Test::Unit::TestCase
  167. def setup
  168. @collection = [Object.new, Object.new, Object.new]
  169. @table = TableHelper::CollectionTable.new(@collection)
  170. @table.header :index
  171. @body = TableHelper::Body.new(@table)
  172. @body.each do |row, object, index|
  173. row.index index.to_s
  174. end
  175. end
  176. def test_should_use_custom_builder
  177. expected = <<-end_str
  178. <tbody>
  179. <tr class="object ui-collection-result">
  180. <td class="object-index">0</td>
  181. </tr>
  182. <tr class="object ui-collection-result">
  183. <td class="object-index">1</td>
  184. </tr>
  185. <tr class="object ui-collection-result">
  186. <td class="object-index">2</td>
  187. </tr>
  188. </tbody>
  189. end_str
  190. assert_html_equal expected, @body.html
  191. end
  192. end
  193. class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
  194. class Post
  195. attr_accessor :title
  196. def initialize(title)
  197. @title = title
  198. end
  199. end
  200. def setup
  201. @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
  202. table = TableHelper::CollectionTable.new(@collection)
  203. table.header :title
  204. @body = TableHelper::Body.new(table)
  205. @body.alternate = :even
  206. end
  207. def test_should_alternate_even_row
  208. expected = <<-end_str
  209. <tr class="post ui-collection-result ui-state-alternate">
  210. <td class="post-title">first</td>
  211. </tr>
  212. end_str
  213. assert_html_equal expected, @body.build_row(@collection.first)
  214. end
  215. def test_should_not_alternate_odd_row
  216. expected = <<-end_str
  217. <tr class="post ui-collection-result">
  218. <td class="post-title">second</td>
  219. </tr>
  220. end_str
  221. assert_html_equal expected, @body.build_row(@collection[1])
  222. end
  223. end
  224. class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
  225. class Post
  226. attr_accessor :title
  227. def initialize(title)
  228. @title = title
  229. end
  230. end
  231. def setup
  232. @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
  233. table = TableHelper::CollectionTable.new(@collection)
  234. table.header :title
  235. @body = TableHelper::Body.new(table)
  236. @body.alternate = :odd
  237. end
  238. def test_should_alternate_odd_row
  239. expected = <<-end_str
  240. <tr class="post ui-collection-result ui-state-alternate">
  241. <td class="post-title">second</td>
  242. </tr>
  243. end_str
  244. assert_html_equal expected, @body.build_row(@collection[1])
  245. end
  246. def test_should_not_alternate_even_row
  247. expected = <<-end_str
  248. <tr class="post ui-collection-result">
  249. <td class="post-title">first</td>
  250. </tr>
  251. end_str
  252. assert_html_equal expected, @body.build_row(@collection.first)
  253. end
  254. end