require 'test_helper' module PaginatedTable describe TableRenderer do let(:view) { stub("view") } let(:description) { stub("description") } let(:data) { stub("data") } let(:page) { stub("page", :sort_column => 'title', :sort_direction => 'asc') } let(:data_page) { stub("data_page", :data => data, :page => page) } let(:link_renderer) { stub("link_renderer") } let(:table) { TableRenderer.new(view, description, data_page, link_renderer) } describe "#initialize" do it "creates a new instance with the view, description, and data_page" do table end end describe "#render" do it "makes a div.paginated_table with the table and a pagination header and footer" do table.stubs(:render_pagination_area).returns("") table.stubs(:render_table).returns("") view.expects(:content_tag).with('div', "
", :class => 'paginated_table') table.render end end describe "#render_pagination_area" do it "makes a div.header with the pagination info and links" do table.stubs(:render_pagination_info).returns("") table.stubs(:render_pagination_links).returns("") view.expects(:content_tag).with('div', "", :class => 'header') table.render_pagination_area end end describe "#render_pagination_info" do it "makes a div.info with the page_entries_info from will_paginate" do view.stubs(:page_entries_info).with(data).returns("") view.expects(:content_tag).with('div', "", :class => 'info') table.render_pagination_info end end describe "#render_pagination_links" do it "makes a div.links with the will_paginate links from will_paginate" do view.stubs(:will_paginate). with(data, :renderer => link_renderer). returns("") view.expects(:content_tag).with('div', "", :class => 'links') table.render_pagination_links end end describe "#render_table" do it "makes a table.paginated with the table header and body" do table.stubs(:render_table_header).returns("
") table.stubs(:render_table_body).returns("") view.expects(:content_tag).with('table', "
", :class => 'paginated') table.render_table end end describe "#render_table_header" do it "makes a thead with the table header rows" do table.stubs(:render_table_header_rows).returns("
") view.expects(:content_tag).with('thead', "
") table.render_table_header end end describe "#render_table_header_rows" do it "concatenates the table headers for the rows with :header titles" do rows = [ stub("row", :title => :header), stub("row", :title => false), stub("row", :title => :header) ] description.stubs(:rows).returns(rows) table.stubs(:render_table_header_row).with(rows.first).returns("") table.stubs(:render_table_header_row).with(rows.last).returns("") table.render_table_header_rows.must_equal "" end end describe "#render_table_header_row" do it "makes a tr with th columns" do columns = [stub("column1"), stub("column2")] row = stub("row", :columns => columns) table.stubs(:render_table_header_column).with(columns.first).returns("") table.stubs(:render_table_header_column).with(columns.last).returns("") view.expects(:content_tag).with('tr', "") table.render_table_header_row(row) end end describe "#render_table_header_column" do it "makes a th with the render_header from the column" do column = stub("column", :name => 'foo', :sortable? => false) table.stubs(:render_table_header_column_content).with(column).returns("
") view.expects(:content_tag).with('th', "
", {}) table.render_table_header_column(column) end describe "when the table is sorted on the column ascending" do it "makes a th with css class 'sortable sorted_asc'" do column = stub("column", :name => 'title', :sortable? => true) table.stubs(:render_table_header_column_content).with(column).returns("
") view.expects(:content_tag). with('th', "
", :class => 'sortable sorted_asc') table.render_table_header_column(column) end end describe "when the table is sorted on the column descending" do it "makes a th with css class 'sortable sorted_asc'" do column = stub("column", :name => 'title', :sortable? => true) page.stubs(:sort_direction => 'desc') table.stubs(:render_table_header_column_content).with(column).returns("
") view.expects(:content_tag). with('th', "
", :class => 'sortable sorted_desc') table.render_table_header_column(column) end end end describe "#render_table_header_column_content" do describe "with a sortable column" do let(:column) { stub("column", :name => :foo, :render_header => '
', :sortable? => true) } it "asks the link renderer to render a link to sort the column" do result = stub("result") link_renderer.stubs(:sort_link).with("
", 'foo').returns(result) table.render_table_header_column_content(column).must_equal result end end describe "with an unsortable column" do let(:column) { stub("column", :render_header => '
', :sortable? => false) } it "simply renders the column's header" do table.render_table_header_column_content(column).must_equal '
' end end end describe "#render_table_body" do it "makes a tbody with the table body rows" do data = [stub("datum1"), stub("datum2")] data_page = stub("data_page", :data => data) table = TableRenderer.new(view, description, data_page, link_renderer) table.stubs(:render_table_body_rows).with(data.first).returns("") table.stubs(:render_table_body_rows).with(data.last).returns("") view.expects(:content_tag).with('tbody', "") table.render_table_body end end describe "#render_table_body_rows" do it "concatenates the interleaved table body rows for the rows" do datum = stub("datum") rows = [stub("row"), stub("row")] description.stubs(:rows).returns(rows) table.stubs(:render_table_body_row).with(rows.first, datum).returns("1") table.stubs(:render_table_body_row).with(rows.last, datum).returns("2") table.render_table_body_rows(datum).must_equal "12" end end describe "#render_table_body_row" do let(:datum) { stub("datum") } let(:columns) { [stub("column1"), stub("column2")] } let(:dom_id) { stub("dom_id") } let(:row) { stub("row", :columns => columns, :cycle => false, :hidden => false, :data_type => false ) } before do view.stubs(:dom_id).with(datum).returns(dom_id) table.stubs(:render_table_body_cell). with(datum, columns.first).returns("") table.stubs(:render_table_body_cell). with(datum, columns.last).returns("") end it "makes a tr with the table body cells" do view.expects(:content_tag). with('tr', "", :"data-datum-id" => dom_id) table.render_table_body_row(row, datum) end it "makes a tr with a css cycle for cycle rows" do css = stub("css") view.stubs(:cycle).with('foo', 'bar').returns(css) row.stubs(:cycle => %w(foo bar)) view.expects(:content_tag). with('tr', "", :class => css, :"data-datum-id" => dom_id) table.render_table_body_row(row, datum) end it "makes a tr with css style display: none for hidden rows" do row.stubs(:hidden => true) view.expects(:content_tag). with('tr', "", :style => 'display: none', :"data-datum-id" => dom_id ) table.render_table_body_row(row, datum) end it "makes a tr with a data-type attribute for data_type rows" do data_type = stub("data_type") row.stubs(:data_type => data_type) view.expects(:content_tag). with('tr', "", :"data-datum-id" => dom_id, :"data-type" => data_type ) table.render_table_body_row(row, datum) end end describe "#render_table_body_cell" do it "makes a td with the render_cell from the column" do datum = stub("datum") column = stub("column") column.stubs(:render_cell).with(datum).returns("") attributes = stub("attributes") column.stubs(:html_attributes).with().returns(attributes) view.expects(:content_tag).with('td', "", attributes) table.render_table_body_cell(datum, column) end end end end