require 'rails_helper' describe 'DryCrud::Table::Builder' do include FormatHelper include UtilityHelper let(:entries) { %w[foo bahr] } let(:table) { DryCrud::Table::Builder.new(entries, self) } def format_size(obj) #:nodoc: "#{obj.size} chars" end specify '#html_header' do table.attrs :upcase, :size dom = 'UpcaseSize' assert_dom_equal dom, table.send(:html_header) end specify 'single attr row' do table.attrs :upcase, :size dom = 'FOO3 chars' assert_dom_equal dom, table.send(:html_row, entries.first) end specify '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, entries.first) end context 'attr col' do let(:col) { table.cols.first } context 'output' do before { table.attrs :upcase } it { expect(col.html_header).to eq('Upcase') } it { expect(col.content('foo')).to eq('FOO') } it { expect(col.html_cell('foo')).to eq('FOO') } end context 'content with custom format_size method' do before { table.attrs :size } it { expect(col.content('abcd')).to eq('4 chars') } end end specify '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 specify '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 specify 'empty entries collection renders empty table' do dom = <<-FIN
headUpcaseSize
FIN dom.gsub!(/[\n\t]/, '').gsub!(/\s{2,}/, '') table = DryCrud::Table::Builder.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