require 'test_helper' class StandardTableBuilderTest < ActionView::TestCase # set dummy helper class for ActionView::TestCase self.helper_class = StandardHelper attr_reader :table def setup @table = StandardTableBuilder.new(["foo", "bahr"], self) end def format_size(obj) "#{obj.size} chars" end test "html header" do table.attrs :upcase, :size dom = 'UpcaseSize' assert_dom_equal dom, table.send(:html_header) end test "single attr row" do table.attrs :upcase, :size dom = 'FOO3 chars' assert_dom_equal dom, table.send(:html_row, "foo") end test "custom row" do table.col("Header", :class => 'hula') {|e| "Weights #{e.size} kg" } dom = 'Weights 3 kg' assert_dom_equal dom, table.send(:html_row, "foo") end test "attr col output" do table.attrs :upcase col = table.cols.first assert_equal "Upcase", col.html_header assert_equal "FOO", col.content("foo") assert_equal "FOO", col.html_cell("foo") end test "attr col content with custom format_size method" do table.attrs :size col = table.cols.first assert_equal "4 chars", col.content("abcd") end test "two x two table" do dom = <<-FIN
UpcaseSize
FOO3 chars
BAHR4 chars
FIN dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "") table.attrs :upcase, :size assert_dom_equal dom, table.to_html end test "table with before and after cells" do dom = <<-FIN
headUpcaseSize
foo FOO 3 chars Never foo
bahr BAHR 4 chars Never bahr
FIN dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "") table.col('head', :class => 'left') { |e| link_to e, "/" } table.attrs :upcase, :size table.col { |e| "Never #{e}" } assert_dom_equal dom, table.to_html end test "empty entries collection renders empty table" do dom = <<-FIN
headUpcaseSize
FIN dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "") table = StandardTableBuilder.new([], self) table.col('head', :class => 'left') { |e| link_to e, "/" } table.attrs :upcase, :size table.col { |e| "Never #{e}" } assert_dom_equal dom, table.to_html end end